1 //===- opt.cpp - The LLVM Modular Optimizer -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Optimizations may be specified an arbitrary number of times on the command
11 // line, They are run in the order specified.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/Analysis/CallGraphSCCPass.h"
20 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/Analysis/RegionPass.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/Assembly/PrintModulePass.h"
24 #include "llvm/Bitcode/ReaderWriter.h"
25 #include "llvm/CodeGen/CommandFlags.h"
26 #include "llvm/DebugInfo.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IRReader/IRReader.h"
30 #include "llvm/LinkAllIR.h"
31 #include "llvm/LinkAllPasses.h"
32 #include "llvm/MC/SubtargetFeature.h"
33 #include "llvm/PassManager.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/PassNameParser.h"
37 #include "llvm/Support/PluginLoader.h"
38 #include "llvm/Support/PrettyStackTrace.h"
39 #include "llvm/Support/Signals.h"
40 #include "llvm/Support/SourceMgr.h"
41 #include "llvm/Support/SystemUtils.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/TargetSelect.h"
44 #include "llvm/Support/ToolOutputFile.h"
45 #include "llvm/Target/TargetLibraryInfo.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
52 // The OptimizationList is automatically populated with registered Passes by the
55 static cl::list<const PassInfo*, bool, PassNameParser>
56 PassList(cl::desc("Optimizations available:"));
58 // Other command line options...
60 static cl::opt<std::string>
61 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
62 cl::init("-"), cl::value_desc("filename"));
64 static cl::opt<std::string>
65 OutputFilename("o", cl::desc("Override output filename"),
66 cl::value_desc("filename"));
69 Force("f", cl::desc("Enable binary output on terminals"));
72 PrintEachXForm("p", cl::desc("Print module after each transformation"));
75 NoOutput("disable-output",
76 cl::desc("Do not write result bitcode file"), cl::Hidden);
79 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
82 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
85 VerifyEach("verify-each", cl::desc("Verify after each transform"));
88 StripDebug("strip-debug",
89 cl::desc("Strip debugger symbol info from translation unit"));
92 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
95 DisableOptimizations("disable-opt",
96 cl::desc("Do not run any optimization passes"));
99 DisableInternalize("disable-internalize",
100 cl::desc("Do not mark all symbols as internal"));
103 StandardCompileOpts("std-compile-opts",
104 cl::desc("Include the standard compile time optimizations"));
107 StandardLinkOpts("std-link-opts",
108 cl::desc("Include the standard link time optimizations"));
112 cl::desc("Optimization level 1. Similar to clang -O1"));
116 cl::desc("Optimization level 2. Similar to clang -O2"));
120 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
124 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
128 cl::desc("Optimization level 3. Similar to clang -O3"));
130 static cl::opt<std::string>
131 TargetTriple("mtriple", cl::desc("Override target triple for module"));
134 UnitAtATime("funit-at-a-time",
135 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
139 DisableLoopUnrolling("disable-loop-unrolling",
140 cl::desc("Disable loop unrolling in all relevant passes"),
143 DisableLoopVectorization("disable-loop-vectorization",
144 cl::desc("Disable the loop vectorization pass"),
148 DisableSLPVectorization("disable-slp-vectorization",
149 cl::desc("Disable the slp vectorization pass"),
154 DisableSimplifyLibCalls("disable-simplify-libcalls",
155 cl::desc("Disable simplify-libcalls"));
158 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
161 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
164 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
167 PrintBreakpoints("print-breakpoints-for-testing",
168 cl::desc("Print select breakpoints location for testing"));
170 static cl::opt<std::string>
171 DefaultDataLayout("default-data-layout",
172 cl::desc("data layout string to use if not specified by module"),
173 cl::value_desc("layout-string"), cl::init(""));
175 // ---------- Define Printers for module and function passes ------------
178 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
180 const PassInfo *PassToPrint;
182 std::string PassName;
184 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
185 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
186 std::string PassToPrintName = PassToPrint->getPassName();
187 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
190 virtual bool runOnSCC(CallGraphSCC &SCC) {
192 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
194 // Get and print pass...
195 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
196 Function *F = (*I)->getFunction();
198 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
204 virtual const char *getPassName() const { return PassName.c_str(); }
206 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
207 AU.addRequiredID(PassToPrint->getTypeInfo());
208 AU.setPreservesAll();
212 char CallGraphSCCPassPrinter::ID = 0;
214 struct ModulePassPrinter : public ModulePass {
216 const PassInfo *PassToPrint;
218 std::string PassName;
220 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
221 : ModulePass(ID), PassToPrint(PI), Out(out) {
222 std::string PassToPrintName = PassToPrint->getPassName();
223 PassName = "ModulePass Printer: " + PassToPrintName;
226 virtual bool runOnModule(Module &M) {
228 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
230 // Get and print pass...
231 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
235 virtual const char *getPassName() const { return PassName.c_str(); }
237 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
238 AU.addRequiredID(PassToPrint->getTypeInfo());
239 AU.setPreservesAll();
243 char ModulePassPrinter::ID = 0;
244 struct FunctionPassPrinter : public FunctionPass {
245 const PassInfo *PassToPrint;
248 std::string PassName;
250 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
251 : FunctionPass(ID), PassToPrint(PI), Out(out) {
252 std::string PassToPrintName = PassToPrint->getPassName();
253 PassName = "FunctionPass Printer: " + PassToPrintName;
256 virtual bool runOnFunction(Function &F) {
258 Out << "Printing analysis '" << PassToPrint->getPassName()
259 << "' for function '" << F.getName() << "':\n";
261 // Get and print pass...
262 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
267 virtual const char *getPassName() const { return PassName.c_str(); }
269 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
270 AU.addRequiredID(PassToPrint->getTypeInfo());
271 AU.setPreservesAll();
275 char FunctionPassPrinter::ID = 0;
277 struct LoopPassPrinter : public LoopPass {
279 const PassInfo *PassToPrint;
281 std::string PassName;
283 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
284 LoopPass(ID), PassToPrint(PI), Out(out) {
285 std::string PassToPrintName = PassToPrint->getPassName();
286 PassName = "LoopPass Printer: " + PassToPrintName;
290 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
292 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
294 // Get and print pass...
295 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
296 L->getHeader()->getParent()->getParent());
300 virtual const char *getPassName() const { return PassName.c_str(); }
302 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
303 AU.addRequiredID(PassToPrint->getTypeInfo());
304 AU.setPreservesAll();
308 char LoopPassPrinter::ID = 0;
310 struct RegionPassPrinter : public RegionPass {
312 const PassInfo *PassToPrint;
314 std::string PassName;
316 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
317 PassToPrint(PI), Out(out) {
318 std::string PassToPrintName = PassToPrint->getPassName();
319 PassName = "RegionPass Printer: " + PassToPrintName;
322 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
324 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
325 << "region: '" << R->getNameStr() << "' in function '"
326 << R->getEntry()->getParent()->getName() << "':\n";
328 // Get and print pass...
329 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
330 R->getEntry()->getParent()->getParent());
334 virtual const char *getPassName() const { return PassName.c_str(); }
336 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
337 AU.addRequiredID(PassToPrint->getTypeInfo());
338 AU.setPreservesAll();
342 char RegionPassPrinter::ID = 0;
344 struct BasicBlockPassPrinter : public BasicBlockPass {
345 const PassInfo *PassToPrint;
348 std::string PassName;
350 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
351 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
352 std::string PassToPrintName = PassToPrint->getPassName();
353 PassName = "BasicBlockPass Printer: " + PassToPrintName;
356 virtual bool runOnBasicBlock(BasicBlock &BB) {
358 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
359 << "': Pass " << PassToPrint->getPassName() << ":\n";
361 // Get and print pass...
362 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
363 BB.getParent()->getParent());
367 virtual const char *getPassName() const { return PassName.c_str(); }
369 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
370 AU.addRequiredID(PassToPrint->getTypeInfo());
371 AU.setPreservesAll();
375 char BasicBlockPassPrinter::ID = 0;
377 struct BreakpointPrinter : public ModulePass {
380 DITypeIdentifierMap TypeIdentifierMap;
382 BreakpointPrinter(raw_ostream &out)
383 : ModulePass(ID), Out(out) {
386 void getContextName(DIDescriptor Context, std::string &N) {
387 if (Context.isNameSpace()) {
388 DINameSpace NS(Context);
389 if (!NS.getName().empty()) {
390 getContextName(NS.getContext(), N);
391 N = N + NS.getName().str() + "::";
393 } else if (Context.isType()) {
395 if (!TY.getName().empty()) {
396 getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
397 N = N + TY.getName().str() + "::";
402 virtual bool runOnModule(Module &M) {
403 TypeIdentifierMap.clear();
404 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
406 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
408 StringSet<> Processed;
409 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
410 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
412 DISubprogram SP(NMD->getOperand(i));
413 assert((!SP || SP.isSubprogram()) &&
414 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
417 getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
418 Name = Name + SP.getDisplayName().str();
419 if (!Name.empty() && Processed.insert(Name)) {
426 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
427 AU.setPreservesAll();
431 } // anonymous namespace
433 char BreakpointPrinter::ID = 0;
435 static inline void addPass(PassManagerBase &PM, Pass *P) {
436 // Add the pass to the pass manager...
439 // If we are verifying all of the intermediate steps, add the verifier...
440 if (VerifyEach) PM.add(createVerifierPass());
443 /// AddOptimizationPasses - This routine adds optimization passes
444 /// based on selected optimization level, OptLevel. This routine
445 /// duplicates llvm-gcc behaviour.
447 /// OptLevel - Optimization Level
448 static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
449 unsigned OptLevel, unsigned SizeLevel) {
450 FPM.add(createVerifierPass()); // Verify that input is correct
452 PassManagerBuilder Builder;
453 Builder.OptLevel = OptLevel;
454 Builder.SizeLevel = SizeLevel;
458 } else if (OptLevel > 1) {
459 unsigned Threshold = 225;
460 if (SizeLevel == 1) // -Os
462 else if (SizeLevel == 2) // -Oz
466 Builder.Inliner = createFunctionInliningPass(Threshold);
468 Builder.Inliner = createAlwaysInlinerPass();
470 Builder.DisableUnitAtATime = !UnitAtATime;
471 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
472 DisableLoopUnrolling : OptLevel == 0;
474 // This is final, unless there is a #pragma vectorize enable
475 if (DisableLoopVectorization)
476 Builder.LoopVectorize = false;
477 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
478 else if (!Builder.LoopVectorize)
479 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
481 // When #pragma vectorize is on for SLP, do the same as above
482 Builder.SLPVectorize =
483 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
485 Builder.populateFunctionPassManager(FPM);
486 Builder.populateModulePassManager(MPM);
489 static void AddStandardCompilePasses(PassManagerBase &PM) {
490 PM.add(createVerifierPass()); // Verify that input is correct
492 // If the -strip-debug command line option was specified, do it.
494 addPass(PM, createStripSymbolsPass(true));
496 if (DisableOptimizations) return;
498 // -std-compile-opts adds the same module passes as -O3.
499 PassManagerBuilder Builder;
501 Builder.Inliner = createFunctionInliningPass();
502 Builder.OptLevel = 3;
503 Builder.populateModulePassManager(PM);
506 static void AddStandardLinkPasses(PassManagerBase &PM) {
507 PM.add(createVerifierPass()); // Verify that input is correct
509 // If the -strip-debug command line option was specified, do it.
511 addPass(PM, createStripSymbolsPass(true));
513 if (DisableOptimizations) return;
515 PassManagerBuilder Builder;
516 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
517 /*RunInliner=*/ !DisableInline);
520 //===----------------------------------------------------------------------===//
521 // CodeGen-related helper functions.
523 static TargetOptions GetTargetOptions() {
524 TargetOptions Options;
525 Options.LessPreciseFPMADOption = EnableFPMAD;
526 Options.NoFramePointerElim = DisableFPElim;
527 Options.AllowFPOpFusion = FuseFPOps;
528 Options.UnsafeFPMath = EnableUnsafeFPMath;
529 Options.NoInfsFPMath = EnableNoInfsFPMath;
530 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
531 Options.HonorSignDependentRoundingFPMathOption =
532 EnableHonorSignDependentRoundingFPMath;
533 Options.UseSoftFloat = GenerateSoftFloatCalls;
534 if (FloatABIForCalls != FloatABI::Default)
535 Options.FloatABIType = FloatABIForCalls;
536 Options.NoZerosInBSS = DontPlaceZerosInBSS;
537 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
538 Options.DisableTailCalls = DisableTailCalls;
539 Options.StackAlignmentOverride = OverrideStackAlignment;
540 Options.TrapFuncName = TrapFuncName;
541 Options.PositionIndependentExecutable = EnablePIE;
542 Options.EnableSegmentedStacks = SegmentedStacks;
543 Options.UseInitArray = UseInitArray;
547 CodeGenOpt::Level GetCodeGenOptLevel() {
549 return CodeGenOpt::Less;
551 return CodeGenOpt::Default;
553 return CodeGenOpt::Aggressive;
554 return CodeGenOpt::None;
557 // Returns the TargetMachine instance or zero if no triple is provided.
558 static TargetMachine* GetTargetMachine(Triple TheTriple) {
560 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
562 // Some modules don't specify a triple, and this is okay.
567 // Package up features to be passed to target/subtarget
568 std::string FeaturesStr;
570 SubtargetFeatures Features;
571 for (unsigned i = 0; i != MAttrs.size(); ++i)
572 Features.AddFeature(MAttrs[i]);
573 FeaturesStr = Features.getString();
576 return TheTarget->createTargetMachine(TheTriple.getTriple(),
577 MCPU, FeaturesStr, GetTargetOptions(),
579 GetCodeGenOptLevel());
582 //===----------------------------------------------------------------------===//
585 int main(int argc, char **argv) {
586 sys::PrintStackTraceOnErrorSignal();
587 llvm::PrettyStackTraceProgram X(argc, argv);
589 // Enable debug stream buffering.
590 EnableDebugBuffering = true;
592 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
593 LLVMContext &Context = getGlobalContext();
595 InitializeAllTargets();
596 InitializeAllTargetMCs();
599 PassRegistry &Registry = *PassRegistry::getPassRegistry();
600 initializeCore(Registry);
601 initializeDebugIRPass(Registry);
602 initializeScalarOpts(Registry);
603 initializeObjCARCOpts(Registry);
604 initializeVectorization(Registry);
605 initializeIPO(Registry);
606 initializeAnalysis(Registry);
607 initializeIPA(Registry);
608 initializeTransformUtils(Registry);
609 initializeInstCombine(Registry);
610 initializeInstrumentation(Registry);
611 initializeTarget(Registry);
613 cl::ParseCommandLineOptions(argc, argv,
614 "llvm .bc -> .bc modular optimizer and analysis printer\n");
616 if (AnalyzeOnly && NoOutput) {
617 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
623 // Load the input module...
625 M.reset(ParseIRFile(InputFilename, Err, Context));
628 Err.print(argv[0], errs());
632 // If we are supposed to override the target triple, do so now.
633 if (!TargetTriple.empty())
634 M->setTargetTriple(Triple::normalize(TargetTriple));
636 // Figure out what stream we are supposed to write to...
637 OwningPtr<tool_output_file> Out;
639 if (!OutputFilename.empty())
640 errs() << "WARNING: The -o (output filename) option is ignored when\n"
641 "the --disable-output option is used.\n";
643 // Default to standard output.
644 if (OutputFilename.empty())
645 OutputFilename = "-";
647 std::string ErrorInfo;
648 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
650 if (!ErrorInfo.empty()) {
651 errs() << ErrorInfo << '\n';
656 // If the output is set to be emitted to standard out, and standard out is a
657 // console, print out a warning message and refuse to do it. We don't
658 // impress anyone by spewing tons of binary goo to a terminal.
659 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
660 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
663 // Create a PassManager to hold and optimize the collection of passes we are
668 // Add an appropriate TargetLibraryInfo pass for the module's triple.
669 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
671 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
672 if (DisableSimplifyLibCalls)
673 TLI->disableAllFunctions();
676 // Add an appropriate DataLayout instance for this module.
678 const std::string &ModuleDataLayout = M.get()->getDataLayout();
679 if (!ModuleDataLayout.empty())
680 TD = new DataLayout(ModuleDataLayout);
681 else if (!DefaultDataLayout.empty())
682 TD = new DataLayout(DefaultDataLayout);
687 Triple ModuleTriple(M->getTargetTriple());
688 TargetMachine *Machine = 0;
689 if (ModuleTriple.getArch())
690 Machine = GetTargetMachine(Triple(ModuleTriple));
691 OwningPtr<TargetMachine> TM(Machine);
693 // Add internal analysis passes from the target machine.
695 TM->addAnalysisPasses(Passes);
697 OwningPtr<FunctionPassManager> FPasses;
698 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
699 FPasses.reset(new FunctionPassManager(M.get()));
701 FPasses->add(new DataLayout(*TD));
703 TM->addAnalysisPasses(*FPasses);
707 if (PrintBreakpoints) {
708 // Default to standard output.
710 if (OutputFilename.empty())
711 OutputFilename = "-";
713 std::string ErrorInfo;
714 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
716 if (!ErrorInfo.empty()) {
717 errs() << ErrorInfo << '\n';
721 Passes.add(new BreakpointPrinter(Out->os()));
725 // If the -strip-debug command line option was specified, add it. If
726 // -std-compile-opts was also specified, it will handle StripDebug.
727 if (StripDebug && !StandardCompileOpts)
728 addPass(Passes, createStripSymbolsPass(true));
730 // Create a new optimization pass for each one specified on the command line
731 for (unsigned i = 0; i < PassList.size(); ++i) {
732 // Check to see if -std-compile-opts was specified before this option. If
734 if (StandardCompileOpts &&
735 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
736 AddStandardCompilePasses(Passes);
737 StandardCompileOpts = false;
740 if (StandardLinkOpts &&
741 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
742 AddStandardLinkPasses(Passes);
743 StandardLinkOpts = false;
746 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
747 AddOptimizationPasses(Passes, *FPasses, 1, 0);
751 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
752 AddOptimizationPasses(Passes, *FPasses, 2, 0);
756 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
757 AddOptimizationPasses(Passes, *FPasses, 2, 1);
761 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
762 AddOptimizationPasses(Passes, *FPasses, 2, 2);
766 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
767 AddOptimizationPasses(Passes, *FPasses, 3, 0);
771 const PassInfo *PassInf = PassList[i];
773 if (PassInf->getNormalCtor())
774 P = PassInf->getNormalCtor()();
776 errs() << argv[0] << ": cannot create pass: "
777 << PassInf->getPassName() << "\n";
779 PassKind Kind = P->getPassKind();
785 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
788 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
791 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
794 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
796 case PT_CallGraphSCC:
797 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
800 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
807 Passes.add(createPrintModulePass(&errs()));
810 // If -std-compile-opts was specified at the end of the pass list, add them.
811 if (StandardCompileOpts) {
812 AddStandardCompilePasses(Passes);
813 StandardCompileOpts = false;
816 if (StandardLinkOpts) {
817 AddStandardLinkPasses(Passes);
818 StandardLinkOpts = false;
822 AddOptimizationPasses(Passes, *FPasses, 1, 0);
825 AddOptimizationPasses(Passes, *FPasses, 2, 0);
828 AddOptimizationPasses(Passes, *FPasses, 2, 1);
831 AddOptimizationPasses(Passes, *FPasses, 2, 2);
834 AddOptimizationPasses(Passes, *FPasses, 3, 0);
836 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
837 FPasses->doInitialization();
838 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
840 FPasses->doFinalization();
843 // Check that the module is well formed on completion of optimization
844 if (!NoVerify && !VerifyEach)
845 Passes.add(createVerifierPass());
847 // Write bitcode or assembly to the output as the last step...
848 if (!NoOutput && !AnalyzeOnly) {
850 Passes.add(createPrintModulePass(&Out->os()));
852 Passes.add(createBitcodeWriterPass(Out->os()));
855 // Before executing passes, print the final values of the LLVM options.
856 cl::PrintOptionValues();
858 // Now that we have all of the passes ready, run them.
859 Passes.run(*M.get());
862 if (!NoOutput || PrintBreakpoints)