blob: 292a42aab41f89e222f09079fcb406c0a6c48b44 [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"
David Greene57873752010-01-05 01:30:32 +000029#include "llvm/Support/Debug.h"
Dan Gohmanfb174702009-09-03 16:00:08 +000030#include "llvm/Support/IRReader.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Support/ManagedStatic.h"
32#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/PluginLoader.h"
Chris Lattnercb8c7752009-12-09 00:41:28 +000034#include "llvm/Support/PrettyStackTrace.h"
Daniel Dunbarcae51cb2009-06-03 18:22:15 +000035#include "llvm/Support/StandardPasses.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/SystemUtils.h"
Daniel Dunbar3b475e92008-10-22 03:25:22 +000037#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/LinkAllPasses.h"
39#include "llvm/LinkAllVMCore.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include <memory>
41#include <algorithm>
42using namespace llvm;
43
44// The OptimizationList is automatically populated with registered Passes by the
45// PassNameParser.
46//
47static cl::list<const PassInfo*, bool, PassNameParser>
48PassList(cl::desc("Optimizations available:"));
49
50// Other command line options...
51//
52static cl::opt<std::string>
Eric Christopherdc019192009-08-21 23:29:40 +000053InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 cl::init("-"), cl::value_desc("filename"));
55
56static cl::opt<std::string>
57OutputFilename("o", cl::desc("Override output filename"),
58 cl::value_desc("filename"), cl::init("-"));
59
60static cl::opt<bool>
Dan Gohman176426d2009-08-25 15:34:52 +000061Force("f", cl::desc("Enable binary output on terminals"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63static cl::opt<bool>
64PrintEachXForm("p", cl::desc("Print module after each transformation"));
65
66static cl::opt<bool>
67NoOutput("disable-output",
68 cl::desc("Do not write result bitcode file"), cl::Hidden);
69
70static cl::opt<bool>
Duncan Sandse3d726a2009-10-14 20:01:39 +000071OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
Daniel Dunbar1a34ea62009-09-05 11:34:53 +000072
73static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
75
76static cl::opt<bool>
77VerifyEach("verify-each", cl::desc("Verify after each transform"));
78
79static cl::opt<bool>
80StripDebug("strip-debug",
81 cl::desc("Strip debugger symbol info from translation unit"));
82
83static cl::opt<bool>
84DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
85
Eric Christopherdc019192009-08-21 23:29:40 +000086static cl::opt<bool>
87DisableOptimizations("disable-opt",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 cl::desc("Do not run any optimization passes"));
89
Eric Christopherdc019192009-08-21 23:29:40 +000090static cl::opt<bool>
Daniel Dunbar53459d72009-07-17 18:09:39 +000091DisableInternalize("disable-internalize",
92 cl::desc("Do not mark all symbols as internal"));
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000095StandardCompileOpts("std-compile-opts",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 cl::desc("Include the standard compile time optimizations"));
97
98static cl::opt<bool>
Eric Christopherdc019192009-08-21 23:29:40 +000099StandardLinkOpts("std-link-opts",
Daniel Dunbar53459d72009-07-17 18:09:39 +0000100 cl::desc("Include the standard link time optimizations"));
101
102static cl::opt<bool>
Devang Patel14d07602008-09-16 22:25:14 +0000103OptLevelO1("O1",
104 cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
105
106static cl::opt<bool>
107OptLevelO2("O2",
Devang Pateld02ad902008-09-17 00:01:04 +0000108 cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
Devang Patel14d07602008-09-16 22:25:14 +0000109
110static cl::opt<bool>
111OptLevelO3("O3",
Devang Pateld02ad902008-09-17 00:01:04 +0000112 cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
Devang Patel14d07602008-09-16 22:25:14 +0000113
114static cl::opt<bool>
115UnitAtATime("funit-at-a-time",
Eric Christopher8ad4add2009-08-21 23:30:30 +0000116 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
117 cl::init(true));
Devang Patel14d07602008-09-16 22:25:14 +0000118
119static cl::opt<bool>
120DisableSimplifyLibCalls("disable-simplify-libcalls",
Devang Patel8dba36c2008-09-17 16:01:39 +0000121 cl::desc("Disable simplify-libcalls"));
Devang Patel14d07602008-09-16 22:25:14 +0000122
123static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
125
126static cl::alias
127QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
128
129static cl::opt<bool>
130AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
131
Chris Lattnercd0702f2009-10-22 00:44:10 +0000132static cl::opt<std::string>
133DefaultDataLayout("default-data-layout",
134 cl::desc("data layout string to use if not specified by module"),
135 cl::value_desc("layout-string"), cl::init(""));
136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137// ---------- Define Printers for module and function passes ------------
138namespace {
139
140struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
141 static char ID;
142 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000143 CallGraphSCCPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000144 CallGraphSCCPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145
Chris Lattner3dab7152009-08-31 00:19:58 +0000146 virtual bool runOnSCC(std::vector<CallGraphNode *>&SCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000148 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149
150 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
151 Function *F = SCC[i]->getFunction();
Dan Gohmanb714fab2009-07-16 15:30:09 +0000152 if (F) {
Chris Lattner397f4562009-08-23 06:03:38 +0000153 getAnalysisID<Pass>(PassToPrint).print(outs(), F->getParent());
Dan Gohmanb714fab2009-07-16 15:30:09 +0000154 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 }
156 }
157 // Get and print pass...
158 return false;
159 }
Eric Christopherdc019192009-08-21 23:29:40 +0000160
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 virtual const char *getPassName() const { return "'Pass' Printer"; }
162
163 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
164 AU.addRequiredID(PassToPrint);
165 AU.setPreservesAll();
166 }
167};
168
169char CallGraphSCCPassPrinter::ID = 0;
170
171struct ModulePassPrinter : public ModulePass {
172 static char ID;
173 const PassInfo *PassToPrint;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000174 ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 PassToPrint(PI) {}
176
177 virtual bool runOnModule(Module &M) {
178 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000179 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000180 getAnalysisID<Pass>(PassToPrint).print(outs(), &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182
183 // Get and print pass...
184 return false;
185 }
186
187 virtual const char *getPassName() const { return "'Pass' Printer"; }
188
189 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
190 AU.addRequiredID(PassToPrint);
191 AU.setPreservesAll();
192 }
193};
194
195char ModulePassPrinter::ID = 0;
196struct FunctionPassPrinter : public FunctionPass {
197 const PassInfo *PassToPrint;
198 static char ID;
Dan Gohmanc74a1972009-02-18 05:09:16 +0000199 FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 PassToPrint(PI) {}
201
202 virtual bool runOnFunction(Function &F) {
Eric Christopherdc019192009-08-21 23:29:40 +0000203 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000204 outs() << "Printing analysis '" << PassToPrint->getPassName()
205 << "' for function '" << F.getName() << "':\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 }
207 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000208 getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 return false;
210 }
211
212 virtual const char *getPassName() const { return "FunctionPass Printer"; }
213
214 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
215 AU.addRequiredID(PassToPrint);
216 AU.setPreservesAll();
217 }
218};
219
220char FunctionPassPrinter::ID = 0;
221
222struct LoopPassPrinter : public LoopPass {
223 static char ID;
224 const PassInfo *PassToPrint;
Eric Christopherdc019192009-08-21 23:29:40 +0000225 LoopPassPrinter(const PassInfo *PI) :
Dan Gohmanc74a1972009-02-18 05:09:16 +0000226 LoopPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227
228 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
229 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000230 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
Chris Lattner397f4562009-08-23 06:03:38 +0000231 getAnalysisID<Pass>(PassToPrint).print(outs(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 L->getHeader()->getParent()->getParent());
233 }
234 // Get and print pass...
235 return false;
236 }
Eric Christopherdc019192009-08-21 23:29:40 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 virtual const char *getPassName() const { return "'Pass' Printer"; }
239
240 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
241 AU.addRequiredID(PassToPrint);
242 AU.setPreservesAll();
243 }
244};
245
246char LoopPassPrinter::ID = 0;
247
248struct BasicBlockPassPrinter : public BasicBlockPass {
249 const PassInfo *PassToPrint;
250 static char ID;
Eric Christopherdc019192009-08-21 23:29:40 +0000251 BasicBlockPassPrinter(const PassInfo *PI)
Dan Gohmanc74a1972009-02-18 05:09:16 +0000252 : BasicBlockPass(&ID), PassToPrint(PI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
254 virtual bool runOnBasicBlock(BasicBlock &BB) {
255 if (!Quiet) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000256 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
257 << "': Pass " << PassToPrint->getPassName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 }
259
260 // Get and print pass...
Chris Lattner397f4562009-08-23 06:03:38 +0000261 getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 return false;
263 }
264
265 virtual const char *getPassName() const { return "BasicBlockPass Printer"; }
266
267 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
268 AU.addRequiredID(PassToPrint);
269 AU.setPreservesAll();
270 }
271};
272
273char BasicBlockPassPrinter::ID = 0;
274inline void addPass(PassManager &PM, Pass *P) {
275 // Add the pass to the pass manager...
276 PM.add(P);
277
278 // If we are verifying all of the intermediate steps, add the verifier...
279 if (VerifyEach) PM.add(createVerifierPass());
280}
281
Eric Christopherdc019192009-08-21 23:29:40 +0000282/// AddOptimizationPasses - This routine adds optimization passes
283/// based on selected optimization level, OptLevel. This routine
Devang Patel14d07602008-09-16 22:25:14 +0000284/// duplicates llvm-gcc behaviour.
285///
286/// OptLevel - Optimization Level
Zhongxing Xu42854c82008-11-26 02:57:24 +0000287void AddOptimizationPasses(PassManager &MPM, FunctionPassManager &FPM,
288 unsigned OptLevel) {
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000289 createStandardFunctionPasses(&FPM, OptLevel);
Devang Patel14d07602008-09-16 22:25:14 +0000290
Eli Friedmanabf44e32010-01-18 22:38:31 +0000291 llvm::Pass *InliningPass = 0;
292 if (DisableInline) {
293 // No inlining pass
294 } else if (OptLevel) {
295 unsigned Threshold = 200;
296 if (OptLevel > 2)
297 Threshold = 250;
298 InliningPass = createFunctionInliningPass(Threshold);
299 } else {
300 InliningPass = createAlwaysInlinerPass();
301 }
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000302 createStandardModulePasses(&MPM, OptLevel,
303 /*OptimizeSize=*/ false,
304 UnitAtATime,
305 /*UnrollLoops=*/ OptLevel > 1,
306 !DisableSimplifyLibCalls,
307 /*HaveExceptions=*/ true,
308 InliningPass);
Devang Patel14d07602008-09-16 22:25:14 +0000309}
310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311void AddStandardCompilePasses(PassManager &PM) {
312 PM.add(createVerifierPass()); // Verify that input is correct
313
314 addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp
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
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000322 llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000324 // -std-compile-opts adds the same module passes as -O3.
Eric Christopherdc019192009-08-21 23:29:40 +0000325 createStandardModulePasses(&PM, 3,
Daniel Dunbarcae51cb2009-06-03 18:22:15 +0000326 /*OptimizeSize=*/ false,
327 /*UnitAtATime=*/ true,
328 /*UnrollLoops=*/ true,
329 /*SimplifyLibCalls=*/ true,
330 /*HaveExceptions=*/ true,
331 InliningPass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332}
333
Daniel Dunbar53459d72009-07-17 18:09:39 +0000334void AddStandardLinkPasses(PassManager &PM) {
335 PM.add(createVerifierPass()); // Verify that input is correct
336
337 // If the -strip-debug command line option was specified, do it.
338 if (StripDebug)
339 addPass(PM, createStripSymbolsPass(true));
340
341 if (DisableOptimizations) return;
342
343 createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
344 /*RunInliner=*/ !DisableInline,
345 /*VerifyEach=*/ VerifyEach);
346}
347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348} // anonymous namespace
349
350
351//===----------------------------------------------------------------------===//
352// main for opt
353//
354int main(int argc, char **argv) {
Chris Lattnercb8c7752009-12-09 00:41:28 +0000355 sys::PrintStackTraceOnErrorSignal();
356 llvm::PrettyStackTraceProgram X(argc, argv);
357
David Greene57873752010-01-05 01:30:32 +0000358 // Enable debug stream buffering.
359 EnableDebugBuffering = true;
360
Chris Lattnercb8c7752009-12-09 00:41:28 +0000361 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Owen Andersone84b8b32009-07-15 22:16:10 +0000362 LLVMContext &Context = getGlobalContext();
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000363
364 cl::ParseCommandLineOptions(argc, argv,
365 "llvm .bc -> .bc modular optimizer and analysis printer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000367 // Allocate a full target machine description only if necessary.
368 // FIXME: The choice of target should be controllable on the command line.
369 std::auto_ptr<TargetMachine> target;
Devang Patelf6a112f2008-08-27 20:51:49 +0000370
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000371 SMDiagnostic Err;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000373 // Load the input module...
374 std::auto_ptr<Module> M;
375 M.reset(ParseIRFile(InputFilename, Err, Context));
Eric Christopherdc019192009-08-21 23:29:40 +0000376
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000377 if (M.get() == 0) {
378 Err.Print(argv[0], errs());
379 return 1;
380 }
381
382 // Figure out what stream we are supposed to write to...
383 // FIXME: outs() is not binary!
384 raw_ostream *Out = &outs(); // Default to printing to stdout...
385 if (OutputFilename != "-") {
Dan Gohman63bf7682010-01-17 17:47:24 +0000386 if (NoOutput || AnalyzeOnly) {
387 errs() << "WARNING: The -o (output filename) option is ignored when\n"
388 "the --disable-output or --analyze options are used.\n";
389 } else {
390 // Make sure that the Output file gets unlinked from the disk if we get a
391 // SIGINT
392 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000393
Dan Gohman63bf7682010-01-17 17:47:24 +0000394 std::string ErrorInfo;
395 Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
396 raw_fd_ostream::F_Binary);
397 if (!ErrorInfo.empty()) {
398 errs() << ErrorInfo << '\n';
399 delete Out;
400 return 1;
401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 }
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000403 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000405 // If the output is set to be emitted to standard out, and standard out is a
406 // console, print out a warning message and refuse to do it. We don't
407 // impress anyone by spewing tons of binary goo to a terminal.
Dan Gohman63bf7682010-01-17 17:47:24 +0000408 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000409 if (CheckBitcodeOutputToConsole(*Out, !Quiet))
410 NoOutput = true;
Dan Gohman31b20c72009-09-11 20:46:33 +0000411
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000412 // Create a PassManager to hold and optimize the collection of passes we are
413 // about to build...
414 //
415 PassManager Passes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000417 // Add an appropriate TargetData instance for this module...
418 TargetData *TD = 0;
419 const std::string &ModuleDataLayout = M.get()->getDataLayout();
420 if (!ModuleDataLayout.empty())
421 TD = new TargetData(ModuleDataLayout);
Kenneth Uildriksa092c122009-11-03 15:29:06 +0000422 else if (!DefaultDataLayout.empty())
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000423 TD = new TargetData(DefaultDataLayout);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000425 if (TD)
426 Passes.add(TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000428 FunctionPassManager *FPasses = NULL;
429 if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
430 FPasses = new FunctionPassManager(new ExistingModuleProvider(M.get()));
Chris Lattnercd0702f2009-10-22 00:44:10 +0000431 if (TD)
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000432 FPasses->add(new TargetData(*TD));
433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000435 // If the -strip-debug command line option was specified, add it. If
436 // -std-compile-opts was also specified, it will handle StripDebug.
437 if (StripDebug && !StandardCompileOpts)
438 addPass(Passes, createStripSymbolsPass(true));
Eric Christopherdc019192009-08-21 23:29:40 +0000439
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000440 // Create a new optimization pass for each one specified on the command line
441 for (unsigned i = 0; i < PassList.size(); ++i) {
442 // Check to see if -std-compile-opts was specified before this option. If
443 // so, handle it.
444 if (StandardCompileOpts &&
445 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
Chris Lattnerfe3cc652008-07-13 19:35:21 +0000446 AddStandardCompilePasses(Passes);
447 StandardCompileOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000448 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000450 if (StandardLinkOpts &&
451 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000452 AddStandardLinkPasses(Passes);
453 StandardLinkOpts = false;
Eric Christopherdc019192009-08-21 23:29:40 +0000454 }
Daniel Dunbar53459d72009-07-17 18:09:39 +0000455
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000456 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000457 AddOptimizationPasses(Passes, *FPasses, 1);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000458 OptLevelO1 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000459 }
Devang Patel14d07602008-09-16 22:25:14 +0000460
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000461 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000462 AddOptimizationPasses(Passes, *FPasses, 2);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000463 OptLevelO2 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000464 }
Devang Patel14d07602008-09-16 22:25:14 +0000465
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000466 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
Daniel Dunbar53459d72009-07-17 18:09:39 +0000467 AddOptimizationPasses(Passes, *FPasses, 3);
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000468 OptLevelO3 = false;
Daniel Dunbar53459d72009-07-17 18:09:39 +0000469 }
Devang Patel14d07602008-09-16 22:25:14 +0000470
Chris Lattner96f3f0e2009-10-22 00:46:41 +0000471 const PassInfo *PassInf = PassList[i];
472 Pass *P = 0;
473 if (PassInf->getNormalCtor())
474 P = PassInf->getNormalCtor()();
475 else
476 errs() << argv[0] << ": cannot create pass: "
477 << PassInf->getPassName() << "\n";
478 if (P) {
479 bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
480 bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
481 bool isFPass = !isLPass && dynamic_cast<FunctionPass*>(P) != 0;
482 bool isCGSCCPass = !isFPass && dynamic_cast<CallGraphSCCPass*>(P) != 0;
483
484 addPass(Passes, P);
485
486 if (AnalyzeOnly) {
487 if (isBBPass)
488 Passes.add(new BasicBlockPassPrinter(PassInf));
489 else if (isLPass)
490 Passes.add(new LoopPassPrinter(PassInf));
491 else if (isFPass)
492 Passes.add(new FunctionPassPrinter(PassInf));
493 else if (isCGSCCPass)
494 Passes.add(new CallGraphSCCPassPrinter(PassInf));
495 else
496 Passes.add(new ModulePassPrinter(PassInf));
497 }
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}