blob: fe0e03649ddc911f4fcd4742ec62cf17c1ebc743 [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"
Devang Patel14d07602008-09-16 22:25:14 +000017#include "llvm/ModuleProvider.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/PassManager.h"
19#include "llvm/CallGraphSCCPass.h"
20#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/Assembly/PrintModulePass.h"
22#include "llvm/Analysis/Verifier.h"
23#include "llvm/Analysis/LoopPass.h"
24#include "llvm/Analysis/CallGraph.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/PassNameParser.h"
28#include "llvm/System/Signals.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"
31#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/PluginLoader.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>
Daniel Dunbar1a34ea62009-09-05 11:34:53 +000069OutputAssembly("S",
70 cl::desc("Write output as LLVM assembly"), cl::Hidden);
71
72static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
74
75static cl::opt<bool>
76VerifyEach("verify-each", cl::desc("Verify after each transform"));
77
78static cl::opt<bool>
79StripDebug("strip-debug",
80 cl::desc("Strip debugger symbol info from translation unit"));
81
82static cl::opt<bool>
83DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
84
Eric Christopherdc019192009-08-21 23:29:40 +000085static cl::opt<bool>
86DisableOptimizations("disable-opt",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 cl::desc("Do not run any optimization passes"));
88
Eric Christopherdc019192009-08-21 23:29:40 +000089static cl::opt<bool>
Daniel Dunbar53459d72009-07-17 18:09:39 +000090DisableInternalize("disable-internalize",
91 cl::desc("Do not mark all symbols as internal"));
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000094StandardCompileOpts("std-compile-opts",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 cl::desc("Include the standard compile time optimizations"));
96
97static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000098StandardLinkOpts("std-link-opts",
Daniel Dunbar53459d72009-07-17 18:09:39 +000099 cl::desc("Include the standard link time optimizations"));
100
101static cl::opt<bool>
Devang Patel14d07602008-09-16 22:25:14 +0000102OptLevelO1("O1",
103 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
104
105static cl::opt<bool>
106OptLevelO2("O2",
Devang Pateld02ad902008-09-17 00:01:04 +0000107 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel14d07602008-09-16 22:25:14 +0000108
109static cl::opt<bool>
110OptLevelO3("O3",
Devang Pateld02ad902008-09-17 00:01:04 +0000111 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel14d07602008-09-16 22:25:14 +0000112
113static cl::opt<bool>
114UnitAtATime("funit-at-a-time",
Eric Christopher8ad4add2009-08-21 23:30:30 +0000115 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
116 cl::init(true));
Devang Patel14d07602008-09-16 22:25:14 +0000117
118static cl::opt<bool>
119DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel8dba36c2008-09-17 16:01:39 +0000120 cl::desc("Disable simplify-libcalls"));
Devang Patel14d07602008-09-16 22:25:14 +0000121
122static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
124
125static cl::alias
126QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
127
128static cl::opt<bool>
129AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
130
131// ---------- Define Printers for module and function passes ------------
132namespace {
133
134struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
135 static char ID;
136 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000137 CallGraphSCCPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000138 CallGraphSCCPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
Chris Lattner3dab7152009-08-31 00:19:58 +0000140 virtual bool runOnSCC(std::vector<CallGraphNode *>&SCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000142 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
144 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
145 Function *F = SCC[i]->getFunction();
Dan Gohmanb714fab2009-07-16 15:30:09 +0000146 if (F) {
Chris Lattner397f4562009-08-23 06:03:38 +0000147 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanb714fab2009-07-16 15:30:09 +0000148 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150 }
151 // Get and print pass...
152 return false;
153 }
Eric Christopherdc019192009-08-21 23:29:40 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 virtual const char *getPassName() const { return "'Pass' Printer"; }
156
157 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
158 AU.addRequiredID(PassToPrint);
159 AU.setPreservesAll();
160 }
161};
162
163char CallGraphSCCPassPrinter::ID = 0;
164
165struct ModulePassPrinter : public ModulePass {
166 static char ID;
167 const PassInfo *PassToPrint;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000168 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 PassToPrint(PI) {}
170
171 virtual bool runOnModule(Module &M) {
172 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000173 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000174 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 }
176
177 // Get and print pass...
178 return false;
179 }
180
181 virtual const char *getPassName() const { return "'Pass' Printer"; }
182
183 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
184 AU.addRequiredID(PassToPrint);
185 AU.setPreservesAll();
186 }
187};
188
189char ModulePassPrinter::ID = 0;
190struct FunctionPassPrinter : public FunctionPass {
191 const PassInfo *PassToPrint;
192 static char ID;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000193 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 PassToPrint(PI) {}
195
196 virtual bool runOnFunction(Function &F) {
Eric Christopherdc019192009-08-21 23:29:40 +0000197 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000198 outs() << "Printing analysis '" << PassToPrint->getPassName()
199 << "' for function '" << F.getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 }
201 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000202 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 return false;
204 }
205
206 virtual const char *getPassName() const { return "FunctionPass Printer"; }
207
208 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.addRequiredID(PassToPrint);
210 AU.setPreservesAll();
211 }
212};
213
214char FunctionPassPrinter::ID = 0;
215
216struct LoopPassPrinter : public LoopPass {
217 static char ID;
218 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000219 LoopPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000220 LoopPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221
222 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
223 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000224 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000225 getAnalysisID<Pass>(PassToPrint).print(outs(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 L->getHeader()->getParent()->getParent());
227 }
228 // Get and print pass...
229 return false;
230 }
Eric Christopherdc019192009-08-21 23:29:40 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 virtual const char *getPassName() const { return "'Pass' Printer"; }
233
234 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
235 AU.addRequiredID(PassToPrint);
236 AU.setPreservesAll();
237 }
238};
239
240char LoopPassPrinter::ID = 0;
241
242struct BasicBlockPassPrinter : public BasicBlockPass {
243 const PassInfo *PassToPrint;
244 static char ID;
Eric Christopherdc019192009-08-21 23:29:40 +0000245 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohmanc74a1972009-02-18 05:09:16 +0000246 : BasicBlockPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
248 virtual bool runOnBasicBlock(BasicBlock &BB) {
249 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000250 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
251 << "': Pass " << PassToPrint->getPassName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 }
253
254 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000255 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 return false;
257 }
258
259 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
260
261 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
262 AU.addRequiredID(PassToPrint);
263 AU.setPreservesAll();
264 }
265};
266
267char BasicBlockPassPrinter::ID = 0;
268inline void addPass(PassManager &PM, Pass *P) {
269 // Add the pass to the pass manager...
270 PM.add(P);
271
272 // If we are verifying all of the intermediate steps, add the verifier...
273 if (VerifyEach) PM.add(createVerifierPass());
274}
275
Eric Christopherdc019192009-08-21 23:29:40 +0000276/// AddOptimizationPasses - This routine adds optimization passes
277/// based on selected optimization level, OptLevel. This routine
Devang Patel14d07602008-09-16 22:25:14 +0000278/// duplicates llvm-gcc behaviour.
279///
280/// OptLevel - Optimization Level
Zhongxing Xu42854c82008-11-26 02:57:24 +0000281void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
282 unsigned OptLevel) {
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000283 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel14d07602008-09-16 22:25:14 +0000284
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000285 llvm::Pass *InliningPass = OptLevel > 1 ? createFunctionInliningPass() : 0;
286 createStandardModulePasses(&MPM, OptLevel,
287 /*OptimizeSize=*/ false,
288 UnitAtATime,
289 /*UnrollLoops=*/ OptLevel > 1,
290 !DisableSimplifyLibCalls,
291 /*HaveExceptions=*/ true,
292 InliningPass);
Devang Patel14d07602008-09-16 22:25:14 +0000293}
294
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295void AddStandardCompilePasses(PassManager &PM) {
296 PM.add(createVerifierPass()); // Verify that input is correct
297
298 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
299
300 // If the -strip-debug command line option was specified, do it.
301 if (StripDebug)
302 addPass(PM, createStripSymbolsPass(true));
303
304 if (DisableOptimizations) return;
305
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000306 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000308 // -std-compile-opts adds the same module passes as -O3.
Eric Christopherdc019192009-08-21 23:29:40 +0000309 createStandardModulePasses(&PM, 3,
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000310 /*OptimizeSize=*/ false,
311 /*UnitAtATime=*/ true,
312 /*UnrollLoops=*/ true,
313 /*SimplifyLibCalls=*/ true,
314 /*HaveExceptions=*/ true,
315 InliningPass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316}
317
Daniel Dunbar53459d72009-07-17 18:09:39 +0000318void AddStandardLinkPasses(PassManager &PM) {
319 PM.add(createVerifierPass()); // Verify that input is correct
320
321 // If the -strip-debug command line option was specified, do it.
322 if (StripDebug)
323 addPass(PM, createStripSymbolsPass(true));
324
325 if (DisableOptimizations) return;
326
327 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
328 /*RunInliner=*/ !DisableInline,
329 /*VerifyEach=*/ VerifyEach);
330}
331
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332} // anonymous namespace
333
334
335//===----------------------------------------------------------------------===//
336// main for opt
337//
338int main(int argc, char **argv) {
339 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Owen Andersone84b8b32009-07-15 22:16:10 +0000340 LLVMContext &Context = getGlobalContext();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 try {
342 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +0000343 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 sys::PrintStackTraceOnErrorSignal();
345
Devang Patelf6a112f2008-08-27 20:51:49 +0000346 // Allocate a full target machine description only if necessary.
347 // FIXME: The choice of target should be controllable on the command line.
348 std::auto_ptr<TargetMachine> target;
349
Dan Gohmanfb174702009-09-03 16:00:08 +0000350 SMDiagnostic Err;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351
352 // Load the input module...
353 std::auto_ptr<Module> M;
Dan Gohmanfb174702009-09-03 16:00:08 +0000354 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christopherdc019192009-08-21 23:29:40 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 if (M.get() == 0) {
Dan Gohmanfb174702009-09-03 16:00:08 +0000357 Err.Print(argv[0], errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 return 1;
359 }
360
361 // Figure out what stream we are supposed to write to...
Dan Gohmanb714fab2009-07-16 15:30:09 +0000362 // FIXME: outs() is not binary!
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000363 raw_ostream *Out = &outs(); // Default to printing to stdout...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 if (OutputFilename != "-") {
Dan Gohman31b20c72009-09-11 20:46:33 +0000365 // Make sure that the Output file gets unlinked from the disk if we get a
366 // SIGINT
367 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
368
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000369 std::string ErrorInfo;
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000370 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
Dan Gohman176426d2009-08-25 15:34:52 +0000371 raw_fd_ostream::F_Binary);
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000372 if (!ErrorInfo.empty()) {
373 errs() << ErrorInfo << '\n';
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000374 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 return 1;
376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 }
378
379 // If the output is set to be emitted to standard out, and standard out is a
380 // console, print out a warning message and refuse to do it. We don't
381 // impress anyone by spewing tons of binary goo to a terminal.
Daniel Dunbar1a34ea62009-09-05 11:34:53 +0000382 if (!Force && !NoOutput && !OutputAssembly)
383 if (CheckBitcodeOutputToConsole(*Out, !Quiet))
384 NoOutput = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 // Create a PassManager to hold and optimize the collection of passes we are
387 // about to build...
388 //
389 PassManager Passes;
390
391 // Add an appropriate TargetData instance for this module...
392 Passes.add(new TargetData(M.get()));
393
Devang Patel14d07602008-09-16 22:25:14 +0000394 FunctionPassManager *FPasses = NULL;
395 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
396 FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get()));
397 FPasses->add(new TargetData(M.get()));
398 }
Eric Christopherdc019192009-08-21 23:29:40 +0000399
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000400 // If the -strip-debug command line option was specified, add it. If
401 // -std-compile-opts was also specified, it will handle StripDebug.
402 if (StripDebug && !StandardCompileOpts)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 addPass(Passes, createStripSymbolsPass(true));
404
405 // Create a new optimization pass for each one specified on the command line
406 for (unsigned i = 0; i < PassList.size(); ++i) {
Duncan Sands54327b02008-07-13 20:14:38 +0000407 // Check to see if -std-compile-opts was specified before this option. If
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000408 // so, handle it.
Eric Christopherdc019192009-08-21 23:29:40 +0000409 if (StandardCompileOpts &&
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000410 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
411 AddStandardCompilePasses(Passes);
412 StandardCompileOpts = false;
413 }
Eric Christopherdc019192009-08-21 23:29:40 +0000414
415 if (StandardLinkOpts &&
Daniel Dunbar53459d72009-07-17 18:09:39 +0000416 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
417 AddStandardLinkPasses(Passes);
418 StandardLinkOpts = false;
419 }
Eric Christopherdc019192009-08-21 23:29:40 +0000420
Devang Patel14d07602008-09-16 22:25:14 +0000421 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
422 AddOptimizationPasses(Passes, *FPasses, 1);
423 OptLevelO1 = false;
424 }
425
426 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
427 AddOptimizationPasses(Passes, *FPasses, 2);
428 OptLevelO2 = false;
429 }
430
431 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
432 AddOptimizationPasses(Passes, *FPasses, 3);
433 OptLevelO3 = false;
434 }
435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 const PassInfo *PassInf = PassList[i];
437 Pass *P = 0;
438 if (PassInf->getNormalCtor())
439 P = PassInf->getNormalCtor()();
440 else
Dan Gohmanb714fab2009-07-16 15:30:09 +0000441 errs() << argv[0] << ": cannot create pass: "
442 << PassInf->getPassName() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 if (P) {
Nuno Lopese1801a62008-11-04 23:03:58 +0000444 bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
445 bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
446 bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0;
447 bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0;
448
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 addPass(Passes, P);
Nuno Lopese1801a62008-11-04 23:03:58 +0000450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 if (AnalyzeOnly) {
Nuno Lopese1801a62008-11-04 23:03:58 +0000452 if (isBBPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 Passes.add(new BasicBlockPassPrinter(PassInf));
Nuno Lopese1801a62008-11-04 23:03:58 +0000454 else if (isLPass)
455 Passes.add(new LoopPassPrinter(PassInf));
456 else if (isFPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 Passes.add(new FunctionPassPrinter(PassInf));
Nuno Lopese1801a62008-11-04 23:03:58 +0000458 else if (isCGSCCPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 Passes.add(new CallGraphSCCPassPrinter(PassInf));
460 else
461 Passes.add(new ModulePassPrinter(PassInf));
462 }
463 }
Eric Christopherdc019192009-08-21 23:29:40 +0000464
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 if (PrintEachXForm)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000466 Passes.add(createPrintModulePass(&errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
Eric Christopherdc019192009-08-21 23:29:40 +0000468
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000469 // If -std-compile-opts was specified at the end of the pass list, add them.
470 if (StandardCompileOpts) {
471 AddStandardCompilePasses(Passes);
472 StandardCompileOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000473 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474
Daniel Dunbar53459d72009-07-17 18:09:39 +0000475 if (StandardLinkOpts) {
476 AddStandardLinkPasses(Passes);
477 StandardLinkOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000478 }
Daniel Dunbar53459d72009-07-17 18:09:39 +0000479
Devang Patel14d07602008-09-16 22:25:14 +0000480 if (OptLevelO1) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000481 AddOptimizationPasses(Passes, *FPasses, 1);
482 }
Devang Patel14d07602008-09-16 22:25:14 +0000483
484 if (OptLevelO2) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000485 AddOptimizationPasses(Passes, *FPasses, 2);
486 }
Devang Patel14d07602008-09-16 22:25:14 +0000487
488 if (OptLevelO3) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000489 AddOptimizationPasses(Passes, *FPasses, 3);
490 }
Devang Patel14d07602008-09-16 22:25:14 +0000491
492 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Bob Wilsona5b5faf2009-07-17 19:05:13 +0000493 FPasses->doInitialization();
Devang Patel14d07602008-09-16 22:25:14 +0000494 for (Module::iterator I = M.get()->begin(), E = M.get()->end();
495 I != E; ++I)
496 FPasses->run(*I);
497 }
498
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 // Check that the module is well formed on completion of optimization
500 if (!NoVerify && !VerifyEach)
501 Passes.add(createVerifierPass());
502
Daniel Dunbar1a34ea62009-09-05 11:34:53 +0000503 // Write bitcode or assembly out to disk or outs() as the last step...
504 if (!NoOutput && !AnalyzeOnly) {
505 if (OutputAssembly)
506 Passes.add(createPrintModulePass(Out));
507 else
508 Passes.add(createBitcodeWriterPass(*Out));
509 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510
511 // Now that we have all of the passes ready, run them.
512 Passes.run(*M.get());
513
Dan Gohmanb714fab2009-07-16 15:30:09 +0000514 // Delete the raw_fd_ostream.
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000515 if (Out != &outs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 delete Out;
517 return 0;
518
519 } catch (const std::string& msg) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000520 errs() << argv[0] << ": " << msg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 } catch (...) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000522 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 }
524 llvm_shutdown();
525 return 1;
526}