blob: 311f67173f3761af9c71eb672b0634251d02eb17 [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 Lattner3dab7152009-08-31 00:19:58 +0000144 virtual bool runOnSCC(std::vector<CallGraphNode *>&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
148 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
149 Function *F = SCC[i]->getFunction();
Dan Gohmanb714fab2009-07-16 15:30:09 +0000150 if (F) {
Chris Lattner397f4562009-08-23 06:03:38 +0000151 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanb714fab2009-07-16 15:30:09 +0000152 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 }
154 }
155 // Get and print pass...
156 return false;
157 }
Eric Christopherdc019192009-08-21 23:29:40 +0000158
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 virtual const char *getPassName() const { return "'Pass' Printer"; }
160
161 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
162 AU.addRequiredID(PassToPrint);
163 AU.setPreservesAll();
164 }
165};
166
167char CallGraphSCCPassPrinter::ID = 0;
168
169struct ModulePassPrinter : public ModulePass {
170 static char ID;
171 const PassInfo *PassToPrint;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000172 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 PassToPrint(PI) {}
174
175 virtual bool runOnModule(Module &M) {
176 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000177 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000178 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 }
180
181 // Get and print pass...
182 return false;
183 }
184
185 virtual const char *getPassName() const { return "'Pass' Printer"; }
186
187 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
188 AU.addRequiredID(PassToPrint);
189 AU.setPreservesAll();
190 }
191};
192
193char ModulePassPrinter::ID = 0;
194struct FunctionPassPrinter : public FunctionPass {
195 const PassInfo *PassToPrint;
196 static char ID;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000197 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 PassToPrint(PI) {}
199
200 virtual bool runOnFunction(Function &F) {
Eric Christopherdc019192009-08-21 23:29:40 +0000201 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000202 outs() << "Printing analysis '" << PassToPrint->getPassName()
203 << "' for function '" << F.getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000206 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 return false;
208 }
209
210 virtual const char *getPassName() const { return "FunctionPass Printer"; }
211
212 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
213 AU.addRequiredID(PassToPrint);
214 AU.setPreservesAll();
215 }
216};
217
218char FunctionPassPrinter::ID = 0;
219
220struct LoopPassPrinter : public LoopPass {
221 static char ID;
222 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000223 LoopPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000224 LoopPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225
226 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
227 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000228 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000229 getAnalysisID<Pass>(PassToPrint).print(outs(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 L->getHeader()->getParent()->getParent());
231 }
232 // Get and print pass...
233 return false;
234 }
Eric Christopherdc019192009-08-21 23:29:40 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 virtual const char *getPassName() const { return "'Pass' Printer"; }
237
238 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
239 AU.addRequiredID(PassToPrint);
240 AU.setPreservesAll();
241 }
242};
243
244char LoopPassPrinter::ID = 0;
245
246struct BasicBlockPassPrinter : public BasicBlockPass {
247 const PassInfo *PassToPrint;
248 static char ID;
Eric Christopherdc019192009-08-21 23:29:40 +0000249 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohmanc74a1972009-02-18 05:09:16 +0000250 : BasicBlockPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 virtual bool runOnBasicBlock(BasicBlock &BB) {
253 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000254 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
255 << "': Pass " << PassToPrint->getPassName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257
258 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000259 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 return false;
261 }
262
263 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
264
265 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
266 AU.addRequiredID(PassToPrint);
267 AU.setPreservesAll();
268 }
269};
270
271char BasicBlockPassPrinter::ID = 0;
272inline void addPass(PassManager &PM, Pass *P) {
273 // Add the pass to the pass manager...
274 PM.add(P);
275
276 // If we are verifying all of the intermediate steps, add the verifier...
277 if (VerifyEach) PM.add(createVerifierPass());
278}
279
Eric Christopherdc019192009-08-21 23:29:40 +0000280/// AddOptimizationPasses - This routine adds optimization passes
281/// based on selected optimization level, OptLevel. This routine
Devang Patel14d07602008-09-16 22:25:14 +0000282/// duplicates llvm-gcc behaviour.
283///
284/// OptLevel - Optimization Level
Zhongxing Xu42854c82008-11-26 02:57:24 +0000285void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
286 unsigned OptLevel) {
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000287 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel14d07602008-09-16 22:25:14 +0000288
Eli Friedmanabf44e32010-01-18 22:38:31 +0000289 llvm::Pass *InliningPass = 0;
290 if (DisableInline) {
291 // No inlining pass
292 } else if (OptLevel) {
293 unsigned Threshold = 200;
294 if (OptLevel > 2)
295 Threshold = 250;
296 InliningPass = createFunctionInliningPass(Threshold);
297 } else {
298 InliningPass = createAlwaysInlinerPass();
299 }
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000300 createStandardModulePasses(&MPM, OptLevel,
301 /*OptimizeSize=*/ false,
302 UnitAtATime,
303 /*UnrollLoops=*/ OptLevel > 1,
304 !DisableSimplifyLibCalls,
305 /*HaveExceptions=*/ true,
306 InliningPass);
Devang Patel14d07602008-09-16 22:25:14 +0000307}
308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309void AddStandardCompilePasses(PassManager &PM) {
310 PM.add(createVerifierPass()); // Verify that input is correct
311
312 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
313
314 // If the -strip-debug command line option was specified, do it.
315 if (StripDebug)
316 addPass(PM, createStripSymbolsPass(true));
317
318 if (DisableOptimizations) return;
319
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000320 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000322 // -std-compile-opts adds the same module passes as -O3.
Eric Christopherdc019192009-08-21 23:29:40 +0000323 createStandardModulePasses(&PM, 3,
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000324 /*OptimizeSize=*/ false,
325 /*UnitAtATime=*/ true,
326 /*UnrollLoops=*/ true,
327 /*SimplifyLibCalls=*/ true,
328 /*HaveExceptions=*/ true,
329 InliningPass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330}
331
Daniel Dunbar53459d72009-07-17 18:09:39 +0000332void AddStandardLinkPasses(PassManager &PM) {
333 PM.add(createVerifierPass()); // Verify that input is correct
334
335 // If the -strip-debug command line option was specified, do it.
336 if (StripDebug)
337 addPass(PM, createStripSymbolsPass(true));
338
339 if (DisableOptimizations) return;
340
341 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
342 /*RunInliner=*/ !DisableInline,
343 /*VerifyEach=*/ VerifyEach);
344}
345
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346} // anonymous namespace
347
348
349//===----------------------------------------------------------------------===//
350// main for opt
351//
352int main(int argc, char **argv) {
Chris Lattnercb8c7752009-12-09 00:41:28 +0000353 sys::PrintStackTraceOnErrorSignal();
354 llvm::PrettyStackTraceProgram X(argc, argv);
355
David Greene57873752010-01-05 01:30:32 +0000356 // Enable debug stream buffering.
357 EnableDebugBuffering = true;
358
Chris Lattnercb8c7752009-12-09 00:41:28 +0000359 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Andersone84b8b32009-07-15 22:16:10 +0000360 LLVMContext &Context = getGlobalContext();
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000361
362 cl::ParseCommandLineOptions(argc, argv,
363 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000365 // Allocate a full target machine description only if necessary.
366 // FIXME: The choice of target should be controllable on the command line.
367 std::auto_ptr<TargetMachine> target;
Devang Patelf6a112f2008-08-27 20:51:49 +0000368
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000369 SMDiagnostic Err;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000371 // Load the input module...
372 std::auto_ptr<Module> M;
373 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christopherdc019192009-08-21 23:29:40 +0000374
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000375 if (M.get() == 0) {
376 Err.Print(argv[0], errs());
377 return 1;
378 }
379
380 // Figure out what stream we are supposed to write to...
381 // FIXME: outs() is not binary!
382 raw_ostream *Out = &outs(); // Default to printing to stdout...
383 if (OutputFilename != "-") {
Dan Gohman63bf7682010-01-17 17:47:24 +0000384 if (NoOutput || AnalyzeOnly) {
385 errs() << "WARNING: The -o (output filename) option is ignored when\n"
386 "the --disable-output or --analyze options are used.\n";
387 } else {
388 // Make sure that the Output file gets unlinked from the disk if we get a
389 // SIGINT
390 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000391
Dan Gohman63bf7682010-01-17 17:47:24 +0000392 std::string ErrorInfo;
393 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
394 raw_fd_ostream::F_Binary);
395 if (!ErrorInfo.empty()) {
396 errs() << ErrorInfo << '\n';
397 delete Out;
398 return 1;
399 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000403 // If the output is set to be emitted to standard out, and standard out is a
404 // console, print out a warning message and refuse to do it. We don't
405 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman63bf7682010-01-17 17:47:24 +0000406 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000407 if (CheckBitcodeOutputToConsole(*Out, !Quiet))
408 NoOutput = true;
Dan Gohman31b20c72009-09-11 20:46:33 +0000409
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000410 // Create a PassManager to hold and optimize the collection of passes we are
411 // about to build...
412 //
413 PassManager Passes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000415 // Add an appropriate TargetData instance for this module...
416 TargetData *TD = 0;
417 const std::string &ModuleDataLayout = M.get()->getDataLayout();
418 if (!ModuleDataLayout.empty())
419 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksa092c122009-11-03 15:29:06 +0000420 else if (!DefaultDataLayout.empty())
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000421 TD = new TargetData(DefaultDataLayout);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000423 if (TD)
424 Passes.add(TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425
Jeffrey Yasskin12015bd2010-03-22 15:56:04 +0000426 OwningPtr<FunctionPassManager> FPasses;
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000427 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
Jeffrey Yasskin12015bd2010-03-22 15:56:04 +0000428 FPasses.reset(new FunctionPassManager(M.get()));
Chris Lattnercd0702f2009-10-22 00:44:10 +0000429 if (TD)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000430 FPasses->add(new TargetData(*TD));
431 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000433 // If the -strip-debug command line option was specified, add it. If
434 // -std-compile-opts was also specified, it will handle StripDebug.
435 if (StripDebug && !StandardCompileOpts)
436 addPass(Passes, createStripSymbolsPass(true));
Eric Christopherdc019192009-08-21 23:29:40 +0000437
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000438 // Create a new optimization pass for each one specified on the command line
439 for (unsigned i = 0; i < PassList.size(); ++i) {
440 // Check to see if -std-compile-opts was specified before this option. If
441 // so, handle it.
442 if (StandardCompileOpts &&
443 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000444 AddStandardCompilePasses(Passes);
445 StandardCompileOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000446 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000448 if (StandardLinkOpts &&
449 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000450 AddStandardLinkPasses(Passes);
451 StandardLinkOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000452 }
Daniel Dunbar53459d72009-07-17 18:09:39 +0000453
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000454 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000455 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000456 OptLevelO1 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000457 }
Devang Patel14d07602008-09-16 22:25:14 +0000458
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000459 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000460 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000461 OptLevelO2 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000462 }
Devang Patel14d07602008-09-16 22:25:14 +0000463
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000464 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000465 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000466 OptLevelO3 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000467 }
Devang Patel14d07602008-09-16 22:25:14 +0000468
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000469 const PassInfo *PassInf = PassList[i];
470 Pass *P = 0;
471 if (PassInf->getNormalCtor())
472 P = PassInf->getNormalCtor()();
473 else
474 errs() << argv[0] << ": cannot create pass: "
475 << PassInf->getPassName() << "\n";
476 if (P) {
Benjamin Kramerd6722cc2010-02-18 12:57:05 +0000477 PassKind Kind = P->getPassKind();
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000478 addPass(Passes, P);
479
480 if (AnalyzeOnly) {
Benjamin Kramerd6722cc2010-02-18 12:57:05 +0000481 switch (Kind) {
Chris Lattnerf6241902010-01-22 06:03:06 +0000482 case PT_BasicBlock:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000483 Passes.add(new BasicBlockPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000484 break;
485 case PT_Loop:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000486 Passes.add(new LoopPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000487 break;
488 case PT_Function:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000489 Passes.add(new FunctionPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000490 break;
491 case PT_CallGraphSCC:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000492 Passes.add(new CallGraphSCCPassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000493 break;
494 default:
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000495 Passes.add(new ModulePassPrinter(PassInf));
Chris Lattnerf6241902010-01-22 06:03:06 +0000496 break;
497 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000498 }
Devang Patel14d07602008-09-16 22:25:14 +0000499 }
500
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000501 if (PrintEachXForm)
502 Passes.add(createPrintModulePass(&errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000504
505 // If -std-compile-opts was specified at the end of the pass list, add them.
506 if (StandardCompileOpts) {
507 AddStandardCompilePasses(Passes);
508 StandardCompileOpts = false;
509 }
510
511 if (StandardLinkOpts) {
512 AddStandardLinkPasses(Passes);
513 StandardLinkOpts = false;
514 }
515
516 if (OptLevelO1)
517 AddOptimizationPasses(Passes, *FPasses, 1);
518
519 if (OptLevelO2)
520 AddOptimizationPasses(Passes, *FPasses, 2);
521
522 if (OptLevelO3)
523 AddOptimizationPasses(Passes, *FPasses, 3);
524
525 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
526 FPasses->doInitialization();
527 for (Module::iterator I = M.get()->begin(), E = M.get()->end();
528 I != E; ++I)
529 FPasses->run(*I);
530 }
531
532 // Check that the module is well formed on completion of optimization
533 if (!NoVerify && !VerifyEach)
534 Passes.add(createVerifierPass());
535
Dan Gohman63bf7682010-01-17 17:47:24 +0000536 // Write bitcode or assembly out to disk or outs() as the last step...
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000537 if (!NoOutput && !AnalyzeOnly) {
538 if (OutputAssembly)
539 Passes.add(createPrintModulePass(Out));
540 else
541 Passes.add(createBitcodeWriterPass(*Out));
542 }
543
544 // Now that we have all of the passes ready, run them.
545 Passes.run(*M.get());
546
547 // Delete the raw_fd_ostream.
548 if (Out != &outs())
549 delete Out;
550 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551}