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 "NewPMDriver.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/Bitcode/ReaderWriter.h"
24 #include "llvm/CodeGen/CommandFlags.h"
25 #include "llvm/DebugInfo.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/IRPrintingPasses.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IRReader/IRReader.h"
31 #include "llvm/LinkAllIR.h"
32 #include "llvm/LinkAllPasses.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/PassManager.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/PassNameParser.h"
38 #include "llvm/Support/PluginLoader.h"
39 #include "llvm/Support/PrettyStackTrace.h"
40 #include "llvm/Support/Signals.h"
41 #include "llvm/Support/SourceMgr.h"
42 #include "llvm/Support/SystemUtils.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/ToolOutputFile.h"
46 #include "llvm/Target/TargetLibraryInfo.h"
47 #include "llvm/Target/TargetMachine.h"
48 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
53 // The OptimizationList is automatically populated with registered Passes by the
56 static cl::list<const PassInfo*, bool, PassNameParser>
57 PassList(cl::desc("Optimizations available:"));
59 // This flag specifies a textual description of the optimization pass pipeline
60 // to run over the module. This flag switches opt to use the new pass manager
61 // infrastructure, completely disabling all of the flags specific to the old
63 static cl::opt<std::string> PassPipeline(
65 cl::desc("A textual description of the pass pipeline for optimizing"),
68 // Other command line options...
70 static cl::opt<std::string>
71 InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
72 cl::init("-"), cl::value_desc("filename"));
74 static cl::opt<std::string>
75 OutputFilename("o", cl::desc("Override output filename"),
76 cl::value_desc("filename"));
79 Force("f", cl::desc("Enable binary output on terminals"));
82 PrintEachXForm("p", cl::desc("Print module after each transformation"));
85 NoOutput("disable-output",
86 cl::desc("Do not write result bitcode file"), cl::Hidden);
89 OutputAssembly("S", cl::desc("Write output as LLVM assembly"));
92 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden);
95 VerifyEach("verify-each", cl::desc("Verify after each transform"));
98 StripDebug("strip-debug",
99 cl::desc("Strip debugger symbol info from translation unit"));
102 DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
105 DisableOptimizations("disable-opt",
106 cl::desc("Do not run any optimization passes"));
109 DisableInternalize("disable-internalize",
110 cl::desc("Do not mark all symbols as internal"));
113 StandardCompileOpts("std-compile-opts",
114 cl::desc("Include the standard compile time optimizations"));
117 StandardLinkOpts("std-link-opts",
118 cl::desc("Include the standard link time optimizations"));
122 cl::desc("Optimization level 1. Similar to clang -O1"));
126 cl::desc("Optimization level 2. Similar to clang -O2"));
130 cl::desc("Like -O2 with extra optimizations for size. Similar to clang -Os"));
134 cl::desc("Like -Os but reduces code size further. Similar to clang -Oz"));
138 cl::desc("Optimization level 3. Similar to clang -O3"));
140 static cl::opt<std::string>
141 TargetTriple("mtriple", cl::desc("Override target triple for module"));
144 UnitAtATime("funit-at-a-time",
145 cl::desc("Enable IPO. This is same as llvm-gcc's -funit-at-a-time"),
149 DisableLoopUnrolling("disable-loop-unrolling",
150 cl::desc("Disable loop unrolling in all relevant passes"),
153 DisableLoopVectorization("disable-loop-vectorization",
154 cl::desc("Disable the loop vectorization pass"),
158 DisableSLPVectorization("disable-slp-vectorization",
159 cl::desc("Disable the slp vectorization pass"),
164 DisableSimplifyLibCalls("disable-simplify-libcalls",
165 cl::desc("Disable simplify-libcalls"));
168 Quiet("q", cl::desc("Obsolete option"), cl::Hidden);
171 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
174 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
177 PrintBreakpoints("print-breakpoints-for-testing",
178 cl::desc("Print select breakpoints location for testing"));
180 static cl::opt<std::string>
181 DefaultDataLayout("default-data-layout",
182 cl::desc("data layout string to use if not specified by module"),
183 cl::value_desc("layout-string"), cl::init(""));
185 // ---------- Define Printers for module and function passes ------------
188 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
190 const PassInfo *PassToPrint;
192 std::string PassName;
194 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) :
195 CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {
196 std::string PassToPrintName = PassToPrint->getPassName();
197 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
200 virtual bool runOnSCC(CallGraphSCC &SCC) {
202 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
204 // Get and print pass...
205 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
206 Function *F = (*I)->getFunction();
208 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
214 virtual const char *getPassName() const { return PassName.c_str(); }
216 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
217 AU.addRequiredID(PassToPrint->getTypeInfo());
218 AU.setPreservesAll();
222 char CallGraphSCCPassPrinter::ID = 0;
224 struct ModulePassPrinter : public ModulePass {
226 const PassInfo *PassToPrint;
228 std::string PassName;
230 ModulePassPrinter(const PassInfo *PI, raw_ostream &out)
231 : ModulePass(ID), PassToPrint(PI), Out(out) {
232 std::string PassToPrintName = PassToPrint->getPassName();
233 PassName = "ModulePass Printer: " + PassToPrintName;
236 virtual bool runOnModule(Module &M) {
238 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
240 // Get and print pass...
241 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
245 virtual const char *getPassName() const { return PassName.c_str(); }
247 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
248 AU.addRequiredID(PassToPrint->getTypeInfo());
249 AU.setPreservesAll();
253 char ModulePassPrinter::ID = 0;
254 struct FunctionPassPrinter : public FunctionPass {
255 const PassInfo *PassToPrint;
258 std::string PassName;
260 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out)
261 : FunctionPass(ID), PassToPrint(PI), Out(out) {
262 std::string PassToPrintName = PassToPrint->getPassName();
263 PassName = "FunctionPass Printer: " + PassToPrintName;
266 virtual bool runOnFunction(Function &F) {
268 Out << "Printing analysis '" << PassToPrint->getPassName()
269 << "' for function '" << F.getName() << "':\n";
271 // Get and print pass...
272 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
277 virtual const char *getPassName() const { return PassName.c_str(); }
279 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
280 AU.addRequiredID(PassToPrint->getTypeInfo());
281 AU.setPreservesAll();
285 char FunctionPassPrinter::ID = 0;
287 struct LoopPassPrinter : public LoopPass {
289 const PassInfo *PassToPrint;
291 std::string PassName;
293 LoopPassPrinter(const PassInfo *PI, raw_ostream &out) :
294 LoopPass(ID), PassToPrint(PI), Out(out) {
295 std::string PassToPrintName = PassToPrint->getPassName();
296 PassName = "LoopPass Printer: " + PassToPrintName;
300 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
302 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
304 // Get and print pass...
305 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
306 L->getHeader()->getParent()->getParent());
310 virtual const char *getPassName() const { return PassName.c_str(); }
312 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
313 AU.addRequiredID(PassToPrint->getTypeInfo());
314 AU.setPreservesAll();
318 char LoopPassPrinter::ID = 0;
320 struct RegionPassPrinter : public RegionPass {
322 const PassInfo *PassToPrint;
324 std::string PassName;
326 RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
327 PassToPrint(PI), Out(out) {
328 std::string PassToPrintName = PassToPrint->getPassName();
329 PassName = "RegionPass Printer: " + PassToPrintName;
332 virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
334 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
335 << "region: '" << R->getNameStr() << "' in function '"
336 << R->getEntry()->getParent()->getName() << "':\n";
338 // Get and print pass...
339 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
340 R->getEntry()->getParent()->getParent());
344 virtual const char *getPassName() const { return PassName.c_str(); }
346 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
347 AU.addRequiredID(PassToPrint->getTypeInfo());
348 AU.setPreservesAll();
352 char RegionPassPrinter::ID = 0;
354 struct BasicBlockPassPrinter : public BasicBlockPass {
355 const PassInfo *PassToPrint;
358 std::string PassName;
360 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out)
361 : BasicBlockPass(ID), PassToPrint(PI), Out(out) {
362 std::string PassToPrintName = PassToPrint->getPassName();
363 PassName = "BasicBlockPass Printer: " + PassToPrintName;
366 virtual bool runOnBasicBlock(BasicBlock &BB) {
368 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
369 << "': Pass " << PassToPrint->getPassName() << ":\n";
371 // Get and print pass...
372 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
373 BB.getParent()->getParent());
377 virtual const char *getPassName() const { return PassName.c_str(); }
379 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
380 AU.addRequiredID(PassToPrint->getTypeInfo());
381 AU.setPreservesAll();
385 char BasicBlockPassPrinter::ID = 0;
387 struct BreakpointPrinter : public ModulePass {
390 DITypeIdentifierMap TypeIdentifierMap;
392 BreakpointPrinter(raw_ostream &out)
393 : ModulePass(ID), Out(out) {
396 void getContextName(DIDescriptor Context, std::string &N) {
397 if (Context.isNameSpace()) {
398 DINameSpace NS(Context);
399 if (!NS.getName().empty()) {
400 getContextName(NS.getContext(), N);
401 N = N + NS.getName().str() + "::";
403 } else if (Context.isType()) {
405 if (!TY.getName().empty()) {
406 getContextName(TY.getContext().resolve(TypeIdentifierMap), N);
407 N = N + TY.getName().str() + "::";
412 virtual bool runOnModule(Module &M) {
413 TypeIdentifierMap.clear();
414 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
416 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
418 StringSet<> Processed;
419 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
420 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
422 DISubprogram SP(NMD->getOperand(i));
423 assert((!SP || SP.isSubprogram()) &&
424 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
427 getContextName(SP.getContext().resolve(TypeIdentifierMap), Name);
428 Name = Name + SP.getDisplayName().str();
429 if (!Name.empty() && Processed.insert(Name)) {
436 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
437 AU.setPreservesAll();
441 } // anonymous namespace
443 char BreakpointPrinter::ID = 0;
445 static inline void addPass(PassManagerBase &PM, Pass *P) {
446 // Add the pass to the pass manager...
449 // If we are verifying all of the intermediate steps, add the verifier...
450 if (VerifyEach) PM.add(createVerifierPass());
453 /// AddOptimizationPasses - This routine adds optimization passes
454 /// based on selected optimization level, OptLevel. This routine
455 /// duplicates llvm-gcc behaviour.
457 /// OptLevel - Optimization Level
458 static void AddOptimizationPasses(PassManagerBase &MPM,FunctionPassManager &FPM,
459 unsigned OptLevel, unsigned SizeLevel) {
460 FPM.add(createVerifierPass()); // Verify that input is correct
462 PassManagerBuilder Builder;
463 Builder.OptLevel = OptLevel;
464 Builder.SizeLevel = SizeLevel;
468 } else if (OptLevel > 1) {
469 unsigned Threshold = 225;
470 if (SizeLevel == 1) // -Os
472 else if (SizeLevel == 2) // -Oz
476 Builder.Inliner = createFunctionInliningPass(Threshold);
478 Builder.Inliner = createAlwaysInlinerPass();
480 Builder.DisableUnitAtATime = !UnitAtATime;
481 Builder.DisableUnrollLoops = (DisableLoopUnrolling.getNumOccurrences() > 0) ?
482 DisableLoopUnrolling : OptLevel == 0;
484 // This is final, unless there is a #pragma vectorize enable
485 if (DisableLoopVectorization)
486 Builder.LoopVectorize = false;
487 // If option wasn't forced via cmd line (-vectorize-loops, -loop-vectorize)
488 else if (!Builder.LoopVectorize)
489 Builder.LoopVectorize = OptLevel > 1 && SizeLevel < 2;
491 // When #pragma vectorize is on for SLP, do the same as above
492 Builder.SLPVectorize =
493 DisableSLPVectorization ? false : OptLevel > 1 && SizeLevel < 2;
495 Builder.populateFunctionPassManager(FPM);
496 Builder.populateModulePassManager(MPM);
499 static void AddStandardCompilePasses(PassManagerBase &PM) {
500 PM.add(createVerifierPass()); // Verify that input is correct
502 // If the -strip-debug command line option was specified, do it.
504 addPass(PM, createStripSymbolsPass(true));
506 if (DisableOptimizations) return;
508 // -std-compile-opts adds the same module passes as -O3.
509 PassManagerBuilder Builder;
511 Builder.Inliner = createFunctionInliningPass();
512 Builder.OptLevel = 3;
513 Builder.populateModulePassManager(PM);
516 static void AddStandardLinkPasses(PassManagerBase &PM) {
517 PM.add(createVerifierPass()); // Verify that input is correct
519 // If the -strip-debug command line option was specified, do it.
521 addPass(PM, createStripSymbolsPass(true));
523 if (DisableOptimizations) return;
525 PassManagerBuilder Builder;
526 Builder.populateLTOPassManager(PM, /*Internalize=*/ !DisableInternalize,
527 /*RunInliner=*/ !DisableInline);
530 //===----------------------------------------------------------------------===//
531 // CodeGen-related helper functions.
533 static TargetOptions GetTargetOptions() {
534 TargetOptions Options;
535 Options.LessPreciseFPMADOption = EnableFPMAD;
536 Options.NoFramePointerElim = DisableFPElim;
537 Options.AllowFPOpFusion = FuseFPOps;
538 Options.UnsafeFPMath = EnableUnsafeFPMath;
539 Options.NoInfsFPMath = EnableNoInfsFPMath;
540 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
541 Options.HonorSignDependentRoundingFPMathOption =
542 EnableHonorSignDependentRoundingFPMath;
543 Options.UseSoftFloat = GenerateSoftFloatCalls;
544 if (FloatABIForCalls != FloatABI::Default)
545 Options.FloatABIType = FloatABIForCalls;
546 Options.NoZerosInBSS = DontPlaceZerosInBSS;
547 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
548 Options.DisableTailCalls = DisableTailCalls;
549 Options.StackAlignmentOverride = OverrideStackAlignment;
550 Options.TrapFuncName = TrapFuncName;
551 Options.PositionIndependentExecutable = EnablePIE;
552 Options.EnableSegmentedStacks = SegmentedStacks;
553 Options.UseInitArray = UseInitArray;
557 CodeGenOpt::Level GetCodeGenOptLevel() {
559 return CodeGenOpt::Less;
561 return CodeGenOpt::Default;
563 return CodeGenOpt::Aggressive;
564 return CodeGenOpt::None;
567 // Returns the TargetMachine instance or zero if no triple is provided.
568 static TargetMachine* GetTargetMachine(Triple TheTriple) {
570 const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
572 // Some modules don't specify a triple, and this is okay.
577 // Package up features to be passed to target/subtarget
578 std::string FeaturesStr;
580 SubtargetFeatures Features;
581 for (unsigned i = 0; i != MAttrs.size(); ++i)
582 Features.AddFeature(MAttrs[i]);
583 FeaturesStr = Features.getString();
586 return TheTarget->createTargetMachine(TheTriple.getTriple(),
587 MCPU, FeaturesStr, GetTargetOptions(),
589 GetCodeGenOptLevel());
592 //===----------------------------------------------------------------------===//
595 int main(int argc, char **argv) {
596 sys::PrintStackTraceOnErrorSignal();
597 llvm::PrettyStackTraceProgram X(argc, argv);
599 // Enable debug stream buffering.
600 EnableDebugBuffering = true;
602 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
603 LLVMContext &Context = getGlobalContext();
605 InitializeAllTargets();
606 InitializeAllTargetMCs();
609 PassRegistry &Registry = *PassRegistry::getPassRegistry();
610 initializeCore(Registry);
611 initializeDebugIRPass(Registry);
612 initializeScalarOpts(Registry);
613 initializeObjCARCOpts(Registry);
614 initializeVectorization(Registry);
615 initializeIPO(Registry);
616 initializeAnalysis(Registry);
617 initializeIPA(Registry);
618 initializeTransformUtils(Registry);
619 initializeInstCombine(Registry);
620 initializeInstrumentation(Registry);
621 initializeTarget(Registry);
623 cl::ParseCommandLineOptions(argc, argv,
624 "llvm .bc -> .bc modular optimizer and analysis printer\n");
626 if (AnalyzeOnly && NoOutput) {
627 errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
633 // Load the input module...
635 M.reset(ParseIRFile(InputFilename, Err, Context));
638 Err.print(argv[0], errs());
642 // If we are supposed to override the target triple, do so now.
643 if (!TargetTriple.empty())
644 M->setTargetTriple(Triple::normalize(TargetTriple));
646 // Figure out what stream we are supposed to write to...
647 OwningPtr<tool_output_file> Out;
649 if (!OutputFilename.empty())
650 errs() << "WARNING: The -o (output filename) option is ignored when\n"
651 "the --disable-output option is used.\n";
653 // Default to standard output.
654 if (OutputFilename.empty())
655 OutputFilename = "-";
657 std::string ErrorInfo;
658 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
660 if (!ErrorInfo.empty()) {
661 errs() << ErrorInfo << '\n';
666 // If the output is set to be emitted to standard out, and standard out is a
667 // console, print out a warning message and refuse to do it. We don't
668 // impress anyone by spewing tons of binary goo to a terminal.
669 if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly)
670 if (CheckBitcodeOutputToConsole(Out->os(), !Quiet))
673 if (PassPipeline.getNumOccurrences() > 0)
674 // The user has asked to use the new pass manager and provided a pipeline
675 // string. Hand off the rest of the functionality to the new code for that
677 return runPassPipeline(argv[0], Context, *M.get(), Out.get(), PassPipeline,
682 // Create a PassManager to hold and optimize the collection of passes we are
687 // Add an appropriate TargetLibraryInfo pass for the module's triple.
688 TargetLibraryInfo *TLI = new TargetLibraryInfo(Triple(M->getTargetTriple()));
690 // The -disable-simplify-libcalls flag actually disables all builtin optzns.
691 if (DisableSimplifyLibCalls)
692 TLI->disableAllFunctions();
695 // Add an appropriate DataLayout instance for this module.
697 const std::string &ModuleDataLayout = M.get()->getDataLayout();
698 if (!ModuleDataLayout.empty())
699 TD = new DataLayout(ModuleDataLayout);
700 else if (!DefaultDataLayout.empty())
701 TD = new DataLayout(DefaultDataLayout);
706 Triple ModuleTriple(M->getTargetTriple());
707 TargetMachine *Machine = 0;
708 if (ModuleTriple.getArch())
709 Machine = GetTargetMachine(Triple(ModuleTriple));
710 OwningPtr<TargetMachine> TM(Machine);
712 // Add internal analysis passes from the target machine.
714 TM->addAnalysisPasses(Passes);
716 OwningPtr<FunctionPassManager> FPasses;
717 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
718 FPasses.reset(new FunctionPassManager(M.get()));
720 FPasses->add(new DataLayout(*TD));
722 TM->addAnalysisPasses(*FPasses);
726 if (PrintBreakpoints) {
727 // Default to standard output.
729 if (OutputFilename.empty())
730 OutputFilename = "-";
732 std::string ErrorInfo;
733 Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
735 if (!ErrorInfo.empty()) {
736 errs() << ErrorInfo << '\n';
740 Passes.add(new BreakpointPrinter(Out->os()));
744 // If the -strip-debug command line option was specified, add it. If
745 // -std-compile-opts was also specified, it will handle StripDebug.
746 if (StripDebug && !StandardCompileOpts)
747 addPass(Passes, createStripSymbolsPass(true));
749 // Create a new optimization pass for each one specified on the command line
750 for (unsigned i = 0; i < PassList.size(); ++i) {
751 // Check to see if -std-compile-opts was specified before this option. If
753 if (StandardCompileOpts &&
754 StandardCompileOpts.getPosition() < PassList.getPosition(i)) {
755 AddStandardCompilePasses(Passes);
756 StandardCompileOpts = false;
759 if (StandardLinkOpts &&
760 StandardLinkOpts.getPosition() < PassList.getPosition(i)) {
761 AddStandardLinkPasses(Passes);
762 StandardLinkOpts = false;
765 if (OptLevelO1 && OptLevelO1.getPosition() < PassList.getPosition(i)) {
766 AddOptimizationPasses(Passes, *FPasses, 1, 0);
770 if (OptLevelO2 && OptLevelO2.getPosition() < PassList.getPosition(i)) {
771 AddOptimizationPasses(Passes, *FPasses, 2, 0);
775 if (OptLevelOs && OptLevelOs.getPosition() < PassList.getPosition(i)) {
776 AddOptimizationPasses(Passes, *FPasses, 2, 1);
780 if (OptLevelOz && OptLevelOz.getPosition() < PassList.getPosition(i)) {
781 AddOptimizationPasses(Passes, *FPasses, 2, 2);
785 if (OptLevelO3 && OptLevelO3.getPosition() < PassList.getPosition(i)) {
786 AddOptimizationPasses(Passes, *FPasses, 3, 0);
790 const PassInfo *PassInf = PassList[i];
792 if (PassInf->getNormalCtor())
793 P = PassInf->getNormalCtor()();
795 errs() << argv[0] << ": cannot create pass: "
796 << PassInf->getPassName() << "\n";
798 PassKind Kind = P->getPassKind();
804 Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
807 Passes.add(new RegionPassPrinter(PassInf, Out->os()));
810 Passes.add(new LoopPassPrinter(PassInf, Out->os()));
813 Passes.add(new FunctionPassPrinter(PassInf, Out->os()));
815 case PT_CallGraphSCC:
816 Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os()));
819 Passes.add(new ModulePassPrinter(PassInf, Out->os()));
826 Passes.add(createPrintModulePass(&errs()));
829 // If -std-compile-opts was specified at the end of the pass list, add them.
830 if (StandardCompileOpts) {
831 AddStandardCompilePasses(Passes);
832 StandardCompileOpts = false;
835 if (StandardLinkOpts) {
836 AddStandardLinkPasses(Passes);
837 StandardLinkOpts = false;
841 AddOptimizationPasses(Passes, *FPasses, 1, 0);
844 AddOptimizationPasses(Passes, *FPasses, 2, 0);
847 AddOptimizationPasses(Passes, *FPasses, 2, 1);
850 AddOptimizationPasses(Passes, *FPasses, 2, 2);
853 AddOptimizationPasses(Passes, *FPasses, 3, 0);
855 if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
856 FPasses->doInitialization();
857 for (Module::iterator F = M->begin(), E = M->end(); F != E; ++F)
859 FPasses->doFinalization();
862 // Check that the module is well formed on completion of optimization
863 if (!NoVerify && !VerifyEach)
864 Passes.add(createVerifierPass());
866 // Write bitcode or assembly to the output as the last step...
867 if (!NoOutput && !AnalyzeOnly) {
869 Passes.add(createPrintModulePass(&Out->os()));
871 Passes.add(createBitcodeWriterPass(Out->os()));
874 // Before executing passes, print the final values of the LLVM options.
875 cl::PrintOptionValues();
877 // Now that we have all of the passes ready, run them.
878 Passes.run(*M.get());
881 if (!NoOutput || PrintBreakpoints)