add a way to disable all builtins, wire it up to opt's -disable-simplifylibcalls...
[oota-llvm.git] / tools / opt / opt.cpp
1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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
15 #include "llvm/LLVMContext.h"
16 #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/DebugInfo.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/Analysis/LoopPass.h"
24 #include "llvm/Analysis/RegionPass.h"
25 #include "llvm/Analysis/CallGraph.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetLibraryInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/Support/PassNameParser.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/IRReader.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/PluginLoader.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Support/StandardPasses.h"
38 #include "llvm/Support/SystemUtils.h"
39 #include "llvm/Support/ToolOutputFile.h"
40 #include "llvm/LinkAllPasses.h"
41 #include "llvm/LinkAllVMCore.h"
42 #include <memory>
43 #include <algorithm>
44 using namespace llvm;
45
46 // The OptimizationList is automatically populated with registered Passes by the
47 // PassNameParser.
48 //
49 static cl::list<const PassInfo*, bool, PassNameParser>
50 PassList(cl::desc("Optimizations available:"));
51
52 // Other command line options...
53 //
54 static cl::opt<std::string>
55 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
56     cl::init("-"), cl::value_desc("filename"));
57
58 static cl::opt<std::string>
59 OutputFilename("o", cl::desc("Override output filename"),
60                cl::value_desc("filename"));
61
62 static cl::opt<bool>
63 Force("f", cl::desc("Enable binary output on terminals"));
64
65 static cl::opt<bool>
66 PrintEachXForm("p", cl::desc("Print module after each transformation"));
67
68 static cl::opt<bool>
69 NoOutput("disable-output",
70          cl::desc("Do not write result bitcode file"), cl::Hidden);
71
72 static cl::opt<bool>
73 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
74
75 static cl::opt<bool>
76 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
77
78 static cl::opt<bool>
79 VerifyEach("verify-each", cl::desc("Verify after each transform"));
80
81 static cl::opt<bool>
82 StripDebug("strip-debug",
83            cl::desc("Strip debugger symbol info from translation unit"));
84
85 static cl::opt<bool>
86 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
87
88 static cl::opt<bool>
89 DisableOptimizations("disable-opt",
90                      cl::desc("Do not run any optimization passes"));
91
92 static cl::opt<bool>
93 DisableInternalize("disable-internalize",
94                    cl::desc("Do not mark all symbols as internal"));
95
96 static cl::opt<bool>
97 StandardCompileOpts("std-compile-opts",
98                    cl::desc("Include the standard compile time optimizations"));
99
100 static cl::opt<bool>
101 StandardLinkOpts("std-link-opts",
102                  cl::desc("Include the standard link time optimizations"));
103
104 static cl::opt<bool>
105 OptLevelO1("O1",
106            cl::desc("Optimization level 1. Similar to llvm-gcc -O1"));
107
108 static cl::opt<bool>
109 OptLevelO2("O2",
110            cl::desc("Optimization level 2. Similar to llvm-gcc -O2"));
111
112 static cl::opt<bool>
113 OptLevelO3("O3",
114            cl::desc("Optimization level 3. Similar to llvm-gcc -O3"));
115
116 static cl::opt<bool>
117 UnitAtATime("funit-at-a-time",
118             cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
119             cl::init(true));
120
121 static cl::opt<bool>
122 DisableSimplifyLibCalls("disable-simplify-libcalls",
123                         cl::desc("Disable simplify-libcalls"));
124
125 static cl::opt<bool>
126 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
127
128 static cl::alias
129 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
130
131 static cl::opt<bool>
132 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
133
134 static cl::opt<bool>
135 PrintBreakpoints("print-breakpoints-for-testing", 
136                  cl::desc("Print select breakpoints location for testing"));
137
138 static cl::opt<std::string>
139 DefaultDataLayout("default-data-layout", 
140           cl::desc("data layout string to use if not specified by module"),
141           cl::value_desc("layout-string"), cl::init(""));
142
143 // ---------- Define Printers for module and function passes ------------
144 namespace {
145
146 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
147   static char ID;
148   const PassInfo *PassToPrint;
149   raw_ostream &Out;
150   std::string PassName;
151
152   CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
153     CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
154       std::string PassToPrintName =  PassToPrint->getPassName();
155       PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
156     }
157
158   virtual bool runOnSCC(CallGraphSCC &SCC) {
159     if (!Quiet)
160       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
161
162     // Get and print pass...
163     for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
164       Function *F = (*I)->getFunction();
165       if (F)
166         getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
167                                                               F->getParent());
168     }
169     return false;
170   }
171
172   virtual const char *getPassName() const { return PassName.c_str(); }
173
174   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
175     AU.addRequiredID(PassToPrint->getTypeInfo());
176     AU.setPreservesAll();
177   }
178 };
179
180 char CallGraphSCCPassPrinter::ID = 0;
181
182 struct ModulePassPrinter : public ModulePass {
183   static char ID;
184   const PassInfo *PassToPrint;
185   raw_ostream &Out;
186   std::string PassName;
187
188   ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
189     : ModulePass(ID), PassToPrint(PI), Out(out) {
190       std::string PassToPrintName =  PassToPrint->getPassName();
191       PassName = "ModulePass Printer: " + PassToPrintName;
192     }
193
194   virtual bool runOnModule(Module &M) {
195     if (!Quiet)
196       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
197
198     // Get and print pass...
199     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
200     return false;
201   }
202
203   virtual const char *getPassName() const { return PassName.c_str(); }
204
205   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
206     AU.addRequiredID(PassToPrint->getTypeInfo());
207     AU.setPreservesAll();
208   }
209 };
210
211 char ModulePassPrinter::ID = 0;
212 struct FunctionPassPrinter : public FunctionPass {
213   const PassInfo *PassToPrint;
214   raw_ostream &Out;
215   static char ID;
216   std::string PassName;
217
218   FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
219     : FunctionPass(ID), PassToPrint(PI), Out(out) {
220       std::string PassToPrintName =  PassToPrint->getPassName();
221       PassName = "FunctionPass Printer: " + PassToPrintName;
222     }
223
224   virtual bool runOnFunction(Function &F) {
225     if (!Quiet)
226       Out << "Printing analysis '" << PassToPrint->getPassName()
227           << "' for function '" << F.getName() << "':\n";
228
229     // Get and print pass...
230     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
231             F.getParent());
232     return false;
233   }
234
235   virtual const char *getPassName() const { return PassName.c_str(); }
236
237   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
238     AU.addRequiredID(PassToPrint->getTypeInfo());
239     AU.setPreservesAll();
240   }
241 };
242
243 char FunctionPassPrinter::ID = 0;
244
245 struct LoopPassPrinter : public LoopPass {
246   static char ID;
247   const PassInfo *PassToPrint;
248   raw_ostream &Out;
249   std::string PassName;
250
251   LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
252     LoopPass(ID), PassToPrint(PI), Out(out) {
253       std::string PassToPrintName =  PassToPrint->getPassName();
254       PassName = "LoopPass Printer: " + PassToPrintName;
255     }
256
257
258   virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
259     if (!Quiet)
260       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
261
262     // Get and print pass...
263     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
264                         L->getHeader()->getParent()->getParent());
265     return false;
266   }
267
268   virtual const char *getPassName() const { return PassName.c_str(); }
269
270   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
271     AU.addRequiredID(PassToPrint->getTypeInfo());
272     AU.setPreservesAll();
273   }
274 };
275
276 char LoopPassPrinter::ID = 0;
277
278 struct RegionPassPrinter : public RegionPass {
279   static char ID;
280   const PassInfo *PassToPrint;
281   raw_ostream &Out;
282   std::string PassName;
283
284   RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
285     PassToPrint(PI), Out(out) {
286     std::string PassToPrintName =  PassToPrint->getPassName();
287     PassName = "RegionPass Printer: " + PassToPrintName;
288   }
289
290   virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
291     if (!Quiet) {
292       Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
293         << "region: '" << R->getNameStr() << "' in function '"
294         << R->getEntry()->getParent()->getNameStr() << "':\n";
295     }
296     // Get and print pass...
297    getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
298                        R->getEntry()->getParent()->getParent());
299     return false;
300   }
301
302   virtual const char *getPassName() const { return PassName.c_str(); }
303
304   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
305     AU.addRequiredID(PassToPrint->getTypeInfo());
306     AU.setPreservesAll();
307   }
308 };
309
310 char RegionPassPrinter::ID = 0;
311
312 struct BasicBlockPassPrinter : public BasicBlockPass {
313   const PassInfo *PassToPrint;
314   raw_ostream &Out;
315   static char ID;
316   std::string PassName;
317
318   BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
319     : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
320       std::string PassToPrintName =  PassToPrint->getPassName();
321       PassName = "BasicBlockPass Printer: " + PassToPrintName;
322     }
323
324   virtual bool runOnBasicBlock(BasicBlock &BB) {
325     if (!Quiet)
326       Out << "Printing Analysis info for BasicBlock '" << BB.getName()
327           << "': Pass " << PassToPrint->getPassName() << ":\n";
328
329     // Get and print pass...
330     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, 
331             BB.getParent()->getParent());
332     return false;
333   }
334
335   virtual const char *getPassName() const { return PassName.c_str(); }
336
337   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
338     AU.addRequiredID(PassToPrint->getTypeInfo());
339     AU.setPreservesAll();
340   }
341 };
342
343 char BasicBlockPassPrinter::ID = 0;
344
345 struct BreakpointPrinter : public FunctionPass {
346   raw_ostream &Out;
347   static char ID;
348
349   BreakpointPrinter(raw_ostream &out)
350     : FunctionPass(ID), Out(out) {
351     }
352
353   virtual bool runOnFunction(Function &F) {
354     BasicBlock &EntryBB = F.getEntryBlock();
355     BasicBlock::const_iterator BI = EntryBB.end();
356     --BI;
357     do {
358       const Instruction *In = BI;
359       const DebugLoc DL = In->getDebugLoc();
360       if (!DL.isUnknown()) {
361         DIScope S(DL.getScope(getGlobalContext()));
362         Out << S.getFilename() << " " << DL.getLine() << "\n";
363         break;
364       }
365       --BI;
366     } while (BI != EntryBB.begin());
367     return false;
368   }
369
370   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
371     AU.setPreservesAll();
372   }
373 };
374
375 char BreakpointPrinter::ID = 0;
376
377 inline void addPass(PassManagerBase &PM, Pass *P) {
378   // Add the pass to the pass manager...
379   PM.add(P);
380
381   // If we are verifying all of the intermediate steps, add the verifier...
382   if (VerifyEach) PM.add(createVerifierPass());
383 }
384
385 /// AddOptimizationPasses - This routine adds optimization passes
386 /// based on selected optimization level, OptLevel. This routine
387 /// duplicates llvm-gcc behaviour.
388 ///
389 /// OptLevel - Optimization Level
390 void AddOptimizationPasses(PassManagerBase &MPM, PassManagerBase &FPM,
391                            unsigned OptLevel) {
392   createStandardFunctionPasses(&FPM, OptLevel);
393
394   llvm::Pass *InliningPass = 0;
395   if (DisableInline) {
396     // No inlining pass
397   } else if (OptLevel) {
398     unsigned Threshold = 225;
399     if (OptLevel > 2)
400       Threshold = 275;
401     InliningPass = createFunctionInliningPass(Threshold);
402   } else {
403     InliningPass = createAlwaysInlinerPass();
404   }
405   createStandardModulePasses(&MPM, OptLevel,
406                              /*OptimizeSize=*/ false,
407                              UnitAtATime,
408                              /*UnrollLoops=*/ OptLevel > 1,
409                              !DisableSimplifyLibCalls,
410                              /*HaveExceptions=*/ true,
411                              InliningPass);
412 }
413
414 void AddStandardCompilePasses(PassManagerBase &PM) {
415   PM.add(createVerifierPass());                  // Verify that input is correct
416
417   addPass(PM, createLowerSetJmpPass());          // Lower llvm.setjmp/.longjmp
418
419   // If the -strip-debug command line option was specified, do it.
420   if (StripDebug)
421     addPass(PM, createStripSymbolsPass(true));
422
423   if (DisableOptimizations) return;
424
425   llvm::Pass *InliningPass = !DisableInline ? createFunctionInliningPass() : 0;
426
427   // -std-compile-opts adds the same module passes as -O3.
428   createStandardModulePasses(&PM, 3,
429                              /*OptimizeSize=*/ false,
430                              /*UnitAtATime=*/ true,
431                              /*UnrollLoops=*/ true,
432                              !DisableSimplifyLibCalls,
433                              /*HaveExceptions=*/ true,
434                              InliningPass);
435 }
436
437 void AddStandardLinkPasses(PassManagerBase &PM) {
438   PM.add(createVerifierPass());                  // Verify that input is correct
439
440   // If the -strip-debug command line option was specified, do it.
441   if (StripDebug)
442     addPass(PM, createStripSymbolsPass(true));
443
444   if (DisableOptimizations) return;
445
446   createStandardLTOPasses(&PM, /*Internalize=*/ !DisableInternalize,
447                           /*RunInliner=*/ !DisableInline,
448                           /*VerifyEach=*/ VerifyEach);
449 }
450
451 } // anonymous namespace
452
453
454 //===----------------------------------------------------------------------===//
455 // main for opt
456 //
457 int main(int argc, char **argv) {
458   sys::PrintStackTraceOnErrorSignal();
459   llvm::PrettyStackTraceProgram X(argc, argv);
460
461   // Enable debug stream buffering.
462   EnableDebugBuffering = true;
463
464   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
465   LLVMContext &Context = getGlobalContext();
466   
467   // Initialize passes
468   PassRegistry &Registry = *PassRegistry::getPassRegistry();
469   initializeCore(Registry);
470   initializeScalarOpts(Registry);
471   initializeIPO(Registry);
472   initializeAnalysis(Registry);
473   initializeIPA(Registry);
474   initializeTransformUtils(Registry);
475   initializeInstCombine(Registry);
476   initializeInstrumentation(Registry);
477   initializeTarget(Registry);
478   
479   cl::ParseCommandLineOptions(argc, argv,
480     "llvm .bc -> .bc modular optimizer and analysis printer\n");
481
482   if (AnalyzeOnly && NoOutput) {
483     errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
484     return 1;
485   }
486
487   // Allocate a full target machine description only if necessary.
488   // FIXME: The choice of target should be controllable on the command line.
489   std::auto_ptr<TargetMachine> target;
490
491   SMDiagnostic Err;
492
493   // Load the input module...
494   std::auto_ptr<Module> M;
495   M.reset(ParseIRFile(InputFilename, Err, Context));
496
497   if (M.get() == 0) {
498     Err.Print(argv[0], errs());
499     return 1;
500   }
501
502   // Figure out what stream we are supposed to write to...
503   OwningPtr<tool_output_file> Out;
504   if (NoOutput) {
505     if (!OutputFilename.empty())
506       errs() << "WARNING: The -o (output filename) option is ignored when\n"
507                 "the --disable-output option is used.\n";
508   } else {
509     // Default to standard output.
510     if (OutputFilename.empty())
511       OutputFilename = "-";
512
513     std::string ErrorInfo;
514     Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
515                                    raw_fd_ostream::F_Binary));
516     if (!ErrorInfo.empty()) {
517       errs() << ErrorInfo << '\n';
518       return 1;
519     }
520   }
521
522   // If the output is set to be emitted to standard out, and standard out is a
523   // console, print out a warning message and refuse to do it.  We don't
524   // impress anyone by spewing tons of binary goo to a terminal.
525   if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
526     if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
527       NoOutput = true;
528
529   // Create a PassManager to hold and optimize the collection of passes we are
530   // about to build.
531   //
532   PassManager Passes;
533
534   // Add an appropriate TargetLibraryInfo pass for the module's triple.
535   TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
536   
537   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
538   if (DisableSimplifyLibCalls)
539     TLI->disableAllFunctions();
540   Passes.add(TLI);
541   
542   // Add an appropriate TargetData instance for this module.
543   TargetData *TD = 0;
544   const std::string &ModuleDataLayout = M.get()->getDataLayout();
545   if (!ModuleDataLayout.empty())
546     TD = new TargetData(ModuleDataLayout);
547   else if (!DefaultDataLayout.empty())
548     TD = new TargetData(DefaultDataLayout);
549
550   if (TD)
551     Passes.add(TD);
552
553   OwningPtr<PassManager> FPasses;
554   if (OptLevelO1 || OptLevelO2 || OptLevelO3) {
555     FPasses.reset(new PassManager());
556     if (TD)
557       FPasses->add(new TargetData(*TD));
558   }
559
560   if (PrintBreakpoints) {
561     // Default to standard output.
562     if (!Out) {
563       if (OutputFilename.empty())
564         OutputFilename = "-";
565       
566       std::string ErrorInfo;
567       Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
568                                      raw_fd_ostream::F_Binary));
569       if (!ErrorInfo.empty()) {
570         errs() << ErrorInfo << '\n';
571         return 1;
572       }
573     }
574     Passes.add(new BreakpointPrinter(Out->os()));
575     NoOutput = true;
576   }
577
578   // If the -strip-debug command line option was specified, add it.  If
579   // -std-compile-opts was also specified, it will handle StripDebug.
580   if (StripDebug && !StandardCompileOpts)
581     addPass(Passes, createStripSymbolsPass(true));
582
583   // Create a new optimization pass for each one specified on the command line
584   for (unsigned i = 0; i < PassList.size(); ++i) {
585     // Check to see if -std-compile-opts was specified before this option.  If
586     // so, handle it.
587     if (StandardCompileOpts &&
588         StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
589       AddStandardCompilePasses(Passes);
590       StandardCompileOpts = false;
591     }
592
593     if (StandardLinkOpts &&
594         StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
595       AddStandardLinkPasses(Passes);
596       StandardLinkOpts = false;
597     }
598
599     if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
600       AddOptimizationPasses(Passes, *FPasses, 1);
601       OptLevelO1 = false;
602     }
603
604     if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
605       AddOptimizationPasses(Passes, *FPasses, 2);
606       OptLevelO2 = false;
607     }
608
609     if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
610       AddOptimizationPasses(Passes, *FPasses, 3);
611       OptLevelO3 = false;
612     }
613
614     const PassInfo *PassInf = PassList[i];
615     Pass *P = 0;
616     if (PassInf->getNormalCtor())
617       P = PassInf->getNormalCtor()();
618     else
619       errs() << argv[0] << ": cannot create pass: "
620              << PassInf->getPassName() << "\n";
621     if (P) {
622       PassKind Kind = P->getPassKind();
623       addPass(Passes, P);
624
625       if (AnalyzeOnly) {
626         switch (Kind) {
627         case PT_BasicBlock:
628           Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
629           break;
630         case PT_Region:
631           Passes.add(new RegionPassPrinter(PassInf, Out->os()));
632           break;
633         case PT_Loop:
634           Passes.add(new LoopPassPrinter(PassInf, Out->os()));
635           break;
636         case PT_Function:
637           Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
638           break;
639         case PT_CallGraphSCC:
640           Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
641           break;
642         default:
643           Passes.add(new ModulePassPrinter(PassInf, Out->os()));
644           break;
645         }
646       }
647     }
648
649     if (PrintEachXForm)
650       Passes.add(createPrintModulePass(&errs()));
651   }
652
653   // If -std-compile-opts was specified at the end of the pass list, add them.
654   if (StandardCompileOpts) {
655     AddStandardCompilePasses(Passes);
656     StandardCompileOpts = false;
657   }
658
659   if (StandardLinkOpts) {
660     AddStandardLinkPasses(Passes);
661     StandardLinkOpts = false;
662   }
663
664   if (OptLevelO1)
665     AddOptimizationPasses(Passes, *FPasses, 1);
666
667   if (OptLevelO2)
668     AddOptimizationPasses(Passes, *FPasses, 2);
669
670   if (OptLevelO3)
671     AddOptimizationPasses(Passes, *FPasses, 3);
672
673   if (OptLevelO1 || OptLevelO2 || OptLevelO3)
674     FPasses->run(*M.get());
675
676   // Check that the module is well formed on completion of optimization
677   if (!NoVerify && !VerifyEach)
678     Passes.add(createVerifierPass());
679
680   // Write bitcode or assembly to the output as the last step...
681   if (!NoOutput && !AnalyzeOnly) {
682     if (OutputAssembly)
683       Passes.add(createPrintModulePass(&Out->os()));
684     else
685       Passes.add(createBitcodeWriterPass(Out->os()));
686   }
687
688   // Now that we have all of the passes ready, run them.
689   Passes.run(*M.get());
690
691   // Declare success.
692   if (!NoOutput || PrintBreakpoints)
693     Out->keep();
694
695   return 0;
696 }