blob: d8bfd61c232779641e69bb3f37ad675ca8af22b6 [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"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/PluginLoader.h"
Daniel Dunbarcae51cb2009-06-03 18:22:15 +000032#include "llvm/Support/StandardPasses.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Support/SystemUtils.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000034#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/LinkAllPasses.h"
36#include "llvm/LinkAllVMCore.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include <memory>
38#include <algorithm>
39using namespace llvm;
40
41// The OptimizationList is automatically populated with registered Passes by the
42// PassNameParser.
43//
44static cl::list<const PassInfo*, bool, PassNameParser>
45PassList(cl::desc("Optimizations available:"));
46
47// Other command line options...
48//
49static cl::opt<std::string>
Eric Christopherdc019192009-08-21 23:29:40 +000050InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 cl::init("-"), cl::value_desc("filename"));
52
53static cl::opt<std::string>
54OutputFilename("o", cl::desc("Override output filename"),
55 cl::value_desc("filename"), cl::init("-"));
56
57static cl::opt<bool>
Dan Gohman176426d2009-08-25 15:34:52 +000058Force("f", cl::desc("Enable binary output on terminals"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60static cl::opt<bool>
61PrintEachXForm("p", cl::desc("Print module after each transformation"));
62
63static cl::opt<bool>
64NoOutput("disable-output",
65 cl::desc("Do not write result bitcode file"), cl::Hidden);
66
67static cl::opt<bool>
68NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
69
70static cl::opt<bool>
71VerifyEach("verify-each", cl::desc("Verify after each transform"));
72
73static cl::opt<bool>
74StripDebug("strip-debug",
75 cl::desc("Strip debugger symbol info from translation unit"));
76
77static cl::opt<bool>
78DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
79
Eric Christopherdc019192009-08-21 23:29:40 +000080static cl::opt<bool>
81DisableOptimizations("disable-opt",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 cl::desc("Do not run any optimization passes"));
83
Eric Christopherdc019192009-08-21 23:29:40 +000084static cl::opt<bool>
Daniel Dunbar53459d72009-07-17 18:09:39 +000085DisableInternalize("disable-internalize",
86 cl::desc("Do not mark all symbols as internal"));
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000089StandardCompileOpts("std-compile-opts",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 cl::desc("Include the standard compile time optimizations"));
91
92static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000093StandardLinkOpts("std-link-opts",
Daniel Dunbar53459d72009-07-17 18:09:39 +000094 cl::desc("Include the standard link time optimizations"));
95
96static cl::opt<bool>
Devang Patel14d07602008-09-16 22:25:14 +000097OptLevelO1("O1",
98 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
99
100static cl::opt<bool>
101OptLevelO2("O2",
Devang Pateld02ad902008-09-17 00:01:04 +0000102 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel14d07602008-09-16 22:25:14 +0000103
104static cl::opt<bool>
105OptLevelO3("O3",
Devang Pateld02ad902008-09-17 00:01:04 +0000106 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel14d07602008-09-16 22:25:14 +0000107
108static cl::opt<bool>
109UnitAtATime("funit-at-a-time",
Eric Christopher8ad4add2009-08-21 23:30:30 +0000110 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
111 cl::init(true));
Devang Patel14d07602008-09-16 22:25:14 +0000112
113static cl::opt<bool>
114DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel8dba36c2008-09-17 16:01:39 +0000115 cl::desc("Disable simplify-libcalls"));
Devang Patel14d07602008-09-16 22:25:14 +0000116
117static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
119
120static cl::alias
121QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
122
123static cl::opt<bool>
124AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
125
126// ---------- Define Printers for module and function passes ------------
127namespace {
128
129struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
130 static char ID;
131 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000132 CallGraphSCCPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000133 CallGraphSCCPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
Chris Lattner3dab7152009-08-31 00:19:58 +0000135 virtual bool runOnSCC(std::vector<CallGraphNode *>&SCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000137 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
140 Function *F = SCC[i]->getFunction();
Dan Gohmanb714fab2009-07-16 15:30:09 +0000141 if (F) {
Chris Lattner397f4562009-08-23 06:03:38 +0000142 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanb714fab2009-07-16 15:30:09 +0000143 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 }
145 }
146 // Get and print pass...
147 return false;
148 }
Eric Christopherdc019192009-08-21 23:29:40 +0000149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 virtual const char *getPassName() const { return "'Pass' Printer"; }
151
152 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
153 AU.addRequiredID(PassToPrint);
154 AU.setPreservesAll();
155 }
156};
157
158char CallGraphSCCPassPrinter::ID = 0;
159
160struct ModulePassPrinter : public ModulePass {
161 static char ID;
162 const PassInfo *PassToPrint;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000163 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 PassToPrint(PI) {}
165
166 virtual bool runOnModule(Module &M) {
167 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000168 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000169 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 }
171
172 // Get and print pass...
173 return false;
174 }
175
176 virtual const char *getPassName() const { return "'Pass' Printer"; }
177
178 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
179 AU.addRequiredID(PassToPrint);
180 AU.setPreservesAll();
181 }
182};
183
184char ModulePassPrinter::ID = 0;
185struct FunctionPassPrinter : public FunctionPass {
186 const PassInfo *PassToPrint;
187 static char ID;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000188 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 PassToPrint(PI) {}
190
191 virtual bool runOnFunction(Function &F) {
Eric Christopherdc019192009-08-21 23:29:40 +0000192 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000193 outs() << "Printing analysis '" << PassToPrint->getPassName()
194 << "' for function '" << F.getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000197 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 return false;
199 }
200
201 virtual const char *getPassName() const { return "FunctionPass Printer"; }
202
203 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
204 AU.addRequiredID(PassToPrint);
205 AU.setPreservesAll();
206 }
207};
208
209char FunctionPassPrinter::ID = 0;
210
211struct LoopPassPrinter : public LoopPass {
212 static char ID;
213 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000214 LoopPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000215 LoopPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216
217 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
218 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000219 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000220 getAnalysisID<Pass>(PassToPrint).print(outs(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 L->getHeader()->getParent()->getParent());
222 }
223 // Get and print pass...
224 return false;
225 }
Eric Christopherdc019192009-08-21 23:29:40 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 virtual const char *getPassName() const { return "'Pass' Printer"; }
228
229 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
230 AU.addRequiredID(PassToPrint);
231 AU.setPreservesAll();
232 }
233};
234
235char LoopPassPrinter::ID = 0;
236
237struct BasicBlockPassPrinter : public BasicBlockPass {
238 const PassInfo *PassToPrint;
239 static char ID;
Eric Christopherdc019192009-08-21 23:29:40 +0000240 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohmanc74a1972009-02-18 05:09:16 +0000241 : BasicBlockPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242
243 virtual bool runOnBasicBlock(BasicBlock &BB) {
244 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000245 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
246 << "': Pass " << PassToPrint->getPassName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 }
248
249 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000250 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 return false;
252 }
253
254 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
255
256 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
257 AU.addRequiredID(PassToPrint);
258 AU.setPreservesAll();
259 }
260};
261
262char BasicBlockPassPrinter::ID = 0;
263inline void addPass(PassManager &PM, Pass *P) {
264 // Add the pass to the pass manager...
265 PM.add(P);
266
267 // If we are verifying all of the intermediate steps, add the verifier...
268 if (VerifyEach) PM.add(createVerifierPass());
269}
270
Eric Christopherdc019192009-08-21 23:29:40 +0000271/// AddOptimizationPasses - This routine adds optimization passes
272/// based on selected optimization level, OptLevel. This routine
Devang Patel14d07602008-09-16 22:25:14 +0000273/// duplicates llvm-gcc behaviour.
274///
275/// OptLevel - Optimization Level
Zhongxing Xu42854c82008-11-26 02:57:24 +0000276void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
277 unsigned OptLevel) {
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000278 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel14d07602008-09-16 22:25:14 +0000279
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000280 llvm::Pass *InliningPass = OptLevel > 1 ? createFunctionInliningPass() : 0;
281 createStandardModulePasses(&MPM, OptLevel,
282 /*OptimizeSize=*/ false,
283 UnitAtATime,
284 /*UnrollLoops=*/ OptLevel > 1,
285 !DisableSimplifyLibCalls,
286 /*HaveExceptions=*/ true,
287 InliningPass);
Devang Patel14d07602008-09-16 22:25:14 +0000288}
289
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290void AddStandardCompilePasses(PassManager &PM) {
291 PM.add(createVerifierPass()); // Verify that input is correct
292
293 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
294
295 // If the -strip-debug command line option was specified, do it.
296 if (StripDebug)
297 addPass(PM, createStripSymbolsPass(true));
298
299 if (DisableOptimizations) return;
300
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000301 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000303 // -std-compile-opts adds the same module passes as -O3.
Eric Christopherdc019192009-08-21 23:29:40 +0000304 createStandardModulePasses(&PM, 3,
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000305 /*OptimizeSize=*/ false,
306 /*UnitAtATime=*/ true,
307 /*UnrollLoops=*/ true,
308 /*SimplifyLibCalls=*/ true,
309 /*HaveExceptions=*/ true,
310 InliningPass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311}
312
Daniel Dunbar53459d72009-07-17 18:09:39 +0000313void AddStandardLinkPasses(PassManager &PM) {
314 PM.add(createVerifierPass()); // Verify that input is correct
315
316 // If the -strip-debug command line option was specified, do it.
317 if (StripDebug)
318 addPass(PM, createStripSymbolsPass(true));
319
320 if (DisableOptimizations) return;
321
322 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
323 /*RunInliner=*/ !DisableInline,
324 /*VerifyEach=*/ VerifyEach);
325}
326
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327} // anonymous namespace
328
329
330//===----------------------------------------------------------------------===//
331// main for opt
332//
333int main(int argc, char **argv) {
334 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Owen Andersone84b8b32009-07-15 22:16:10 +0000335 LLVMContext &Context = getGlobalContext();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 try {
337 cl::ParseCommandLineOptions(argc, argv,
Dan Gohman6099df82007-10-08 15:45:12 +0000338 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 sys::PrintStackTraceOnErrorSignal();
340
Devang Patelf6a112f2008-08-27 20:51:49 +0000341 // Allocate a full target machine description only if necessary.
342 // FIXME: The choice of target should be controllable on the command line.
343 std::auto_ptr<TargetMachine> target;
344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 std::string ErrorMessage;
346
347 // Load the input module...
348 std::auto_ptr<Module> M;
349 if (MemoryBuffer *Buffer
350 = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
Owen Andersona148fdd2009-07-01 21:22:36 +0000351 M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 delete Buffer;
353 }
Eric Christopherdc019192009-08-21 23:29:40 +0000354
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 if (M.get() == 0) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000356 errs() << argv[0] << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 if (ErrorMessage.size())
Dan Gohmanb714fab2009-07-16 15:30:09 +0000358 errs() << ErrorMessage << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 else
Dan Gohmanb714fab2009-07-16 15:30:09 +0000360 errs() << "bitcode didn't read correctly.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 return 1;
362 }
363
364 // Figure out what stream we are supposed to write to...
Dan Gohmanb714fab2009-07-16 15:30:09 +0000365 // FIXME: outs() is not binary!
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000366 raw_ostream *Out = &outs(); // Default to printing to stdout...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 if (OutputFilename != "-") {
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000368 std::string ErrorInfo;
Chris Lattnerfdcd46e2009-08-23 02:51:22 +0000369 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
Dan Gohman176426d2009-08-25 15:34:52 +0000370 raw_fd_ostream::F_Binary);
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000371 if (!ErrorInfo.empty()) {
372 errs() << ErrorInfo << '\n';
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000373 delete Out;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 return 1;
375 }
376
377 // Make sure that the Output file gets unlinked from the disk if we get a
378 // SIGINT
379 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
380 }
381
382 // If the output is set to be emitted to standard out, and standard out is a
383 // console, print out a warning message and refuse to do it. We don't
384 // impress anyone by spewing tons of binary goo to a terminal.
Chris Lattner3a71a1a2009-08-23 21:36:09 +0000385 if (!Force && !NoOutput && CheckBitcodeOutputToConsole(*Out, !Quiet))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 NoOutput = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387
388 // Create a PassManager to hold and optimize the collection of passes we are
389 // about to build...
390 //
391 PassManager Passes;
392
393 // Add an appropriate TargetData instance for this module...
394 Passes.add(new TargetData(M.get()));
395
Devang Patel14d07602008-09-16 22:25:14 +0000396 FunctionPassManager *FPasses = NULL;
397 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
398 FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get()));
399 FPasses->add(new TargetData(M.get()));
400 }
Eric Christopherdc019192009-08-21 23:29:40 +0000401
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000402 // If the -strip-debug command line option was specified, add it. If
403 // -std-compile-opts was also specified, it will handle StripDebug.
404 if (StripDebug && !StandardCompileOpts)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 addPass(Passes, createStripSymbolsPass(true));
406
407 // Create a new optimization pass for each one specified on the command line
408 for (unsigned i = 0; i < PassList.size(); ++i) {
Duncan Sands54327b02008-07-13 20:14:38 +0000409 // Check to see if -std-compile-opts was specified before this option. If
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000410 // so, handle it.
Eric Christopherdc019192009-08-21 23:29:40 +0000411 if (StandardCompileOpts &&
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000412 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
413 AddStandardCompilePasses(Passes);
414 StandardCompileOpts = false;
415 }
Eric Christopherdc019192009-08-21 23:29:40 +0000416
417 if (StandardLinkOpts &&
Daniel Dunbar53459d72009-07-17 18:09:39 +0000418 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
419 AddStandardLinkPasses(Passes);
420 StandardLinkOpts = false;
421 }
Eric Christopherdc019192009-08-21 23:29:40 +0000422
Devang Patel14d07602008-09-16 22:25:14 +0000423 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
424 AddOptimizationPasses(Passes, *FPasses, 1);
425 OptLevelO1 = false;
426 }
427
428 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
429 AddOptimizationPasses(Passes, *FPasses, 2);
430 OptLevelO2 = false;
431 }
432
433 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
434 AddOptimizationPasses(Passes, *FPasses, 3);
435 OptLevelO3 = false;
436 }
437
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 const PassInfo *PassInf = PassList[i];
439 Pass *P = 0;
440 if (PassInf->getNormalCtor())
441 P = PassInf->getNormalCtor()();
442 else
Dan Gohmanb714fab2009-07-16 15:30:09 +0000443 errs() << argv[0] << ": cannot create pass: "
444 << PassInf->getPassName() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 if (P) {
Nuno Lopese1801a62008-11-04 23:03:58 +0000446 bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
447 bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
448 bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0;
449 bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0;
450
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 addPass(Passes, P);
Nuno Lopese1801a62008-11-04 23:03:58 +0000452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 if (AnalyzeOnly) {
Nuno Lopese1801a62008-11-04 23:03:58 +0000454 if (isBBPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 Passes.add(new BasicBlockPassPrinter(PassInf));
Nuno Lopese1801a62008-11-04 23:03:58 +0000456 else if (isLPass)
457 Passes.add(new LoopPassPrinter(PassInf));
458 else if (isFPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 Passes.add(new FunctionPassPrinter(PassInf));
Nuno Lopese1801a62008-11-04 23:03:58 +0000460 else if (isCGSCCPass)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 Passes.add(new CallGraphSCCPassPrinter(PassInf));
462 else
463 Passes.add(new ModulePassPrinter(PassInf));
464 }
465 }
Eric Christopherdc019192009-08-21 23:29:40 +0000466
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 if (PrintEachXForm)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000468 Passes.add(createPrintModulePass(&errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 }
Eric Christopherdc019192009-08-21 23:29:40 +0000470
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000471 // If -std-compile-opts was specified at the end of the pass list, add them.
472 if (StandardCompileOpts) {
473 AddStandardCompilePasses(Passes);
474 StandardCompileOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000475 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476
Daniel Dunbar53459d72009-07-17 18:09:39 +0000477 if (StandardLinkOpts) {
478 AddStandardLinkPasses(Passes);
479 StandardLinkOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000480 }
Daniel Dunbar53459d72009-07-17 18:09:39 +0000481
Devang Patel14d07602008-09-16 22:25:14 +0000482 if (OptLevelO1) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000483 AddOptimizationPasses(Passes, *FPasses, 1);
484 }
Devang Patel14d07602008-09-16 22:25:14 +0000485
486 if (OptLevelO2) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000487 AddOptimizationPasses(Passes, *FPasses, 2);
488 }
Devang Patel14d07602008-09-16 22:25:14 +0000489
490 if (OptLevelO3) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000491 AddOptimizationPasses(Passes, *FPasses, 3);
492 }
Devang Patel14d07602008-09-16 22:25:14 +0000493
494 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Bob Wilsona5b5faf2009-07-17 19:05:13 +0000495 FPasses->doInitialization();
Devang Patel14d07602008-09-16 22:25:14 +0000496 for (Module::iterator I = M.get()->begin(), E = M.get()->end();
497 I != E; ++I)
498 FPasses->run(*I);
499 }
500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 // Check that the module is well formed on completion of optimization
502 if (!NoVerify && !VerifyEach)
503 Passes.add(createVerifierPass());
504
Dan Gohmanb714fab2009-07-16 15:30:09 +0000505 // Write bitcode out to disk or outs() as the last step...
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 if (!NoOutput && !AnalyzeOnly)
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000507 Passes.add(createBitcodeWriterPass(*Out));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508
509 // Now that we have all of the passes ready, run them.
510 Passes.run(*M.get());
511
Dan Gohmanb714fab2009-07-16 15:30:09 +0000512 // Delete the raw_fd_ostream.
Dan Gohmandd7ca8e2009-07-15 17:29:42 +0000513 if (Out != &outs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 delete Out;
515 return 0;
516
517 } catch (const std::string& msg) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000518 errs() << argv[0] << ": " << msg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 } catch (...) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000520 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
522 llvm_shutdown();
523 return 1;
524}