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