LLVM  mainline
PassManagerBuilder.cpp
Go to the documentation of this file.
00001 //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PassManagerBuilder class, which is used to set up a
00011 // "standard" optimization sequence suitable for languages like C and C++.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 
00016 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
00017 #include "llvm-c/Transforms/PassManagerBuilder.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/Analysis/Passes.h"
00020 #include "llvm/IR/DataLayout.h"
00021 #include "llvm/IR/Verifier.h"
00022 #include "llvm/IR/LegacyPassManager.h"
00023 #include "llvm/Support/CommandLine.h"
00024 #include "llvm/Support/ManagedStatic.h"
00025 #include "llvm/Analysis/TargetLibraryInfo.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Transforms/IPO.h"
00028 #include "llvm/Transforms/Scalar.h"
00029 #include "llvm/Transforms/Vectorize.h"
00030 
00031 using namespace llvm;
00032 
00033 static cl::opt<bool>
00034 RunLoopVectorization("vectorize-loops", cl::Hidden,
00035                      cl::desc("Run the Loop vectorization passes"));
00036 
00037 static cl::opt<bool>
00038 RunSLPVectorization("vectorize-slp", cl::Hidden,
00039                     cl::desc("Run the SLP vectorization passes"));
00040 
00041 static cl::opt<bool>
00042 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
00043                     cl::desc("Run the BB vectorization passes"));
00044 
00045 static cl::opt<bool>
00046 UseGVNAfterVectorization("use-gvn-after-vectorization",
00047   cl::init(false), cl::Hidden,
00048   cl::desc("Run GVN instead of Early CSE after vectorization passes"));
00049 
00050 static cl::opt<bool> ExtraVectorizerPasses(
00051     "extra-vectorizer-passes", cl::init(false), cl::Hidden,
00052     cl::desc("Run cleanup optimization passes after vectorization."));
00053 
00054 static cl::opt<bool> UseNewSROA("use-new-sroa",
00055   cl::init(true), cl::Hidden,
00056   cl::desc("Enable the new, experimental SROA pass"));
00057 
00058 static cl::opt<bool>
00059 RunLoopRerolling("reroll-loops", cl::Hidden,
00060                  cl::desc("Run the loop rerolling pass"));
00061 
00062 static cl::opt<bool>
00063 RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
00064              cl::desc("Run the float2int (float demotion) pass"));
00065 
00066 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
00067                                     cl::Hidden,
00068                                     cl::desc("Run the load combining pass"));
00069 
00070 static cl::opt<bool>
00071 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
00072   cl::init(true), cl::Hidden,
00073   cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
00074            "vectorizer instead of before"));
00075 
00076 static cl::opt<bool> UseCFLAA("use-cfl-aa",
00077   cl::init(false), cl::Hidden,
00078   cl::desc("Enable the new, experimental CFL alias analysis"));
00079 
00080 static cl::opt<bool>
00081 EnableMLSM("mlsm", cl::init(true), cl::Hidden,
00082            cl::desc("Enable motion of merged load and store"));
00083 
00084 static cl::opt<bool> EnableLoopInterchange(
00085     "enable-loopinterchange", cl::init(false), cl::Hidden,
00086     cl::desc("Enable the new, experimental LoopInterchange Pass"));
00087 
00088 static cl::opt<bool> EnableLoopDistribute(
00089     "enable-loop-distribute", cl::init(false), cl::Hidden,
00090     cl::desc("Enable the new, experimental LoopDistribution Pass"));
00091 
00092 PassManagerBuilder::PassManagerBuilder() {
00093     OptLevel = 2;
00094     SizeLevel = 0;
00095     LibraryInfo = nullptr;
00096     Inliner = nullptr;
00097     DisableTailCalls = false;
00098     DisableUnitAtATime = false;
00099     DisableUnrollLoops = false;
00100     BBVectorize = RunBBVectorization;
00101     SLPVectorize = RunSLPVectorization;
00102     LoopVectorize = RunLoopVectorization;
00103     RerollLoops = RunLoopRerolling;
00104     LoadCombine = RunLoadCombine;
00105     DisableGVNLoadPRE = false;
00106     VerifyInput = false;
00107     VerifyOutput = false;
00108     MergeFunctions = false;
00109 }
00110 
00111 PassManagerBuilder::~PassManagerBuilder() {
00112   delete LibraryInfo;
00113   delete Inliner;
00114 }
00115 
00116 /// Set of global extensions, automatically added as part of the standard set.
00117 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
00118    PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
00119 
00120 void PassManagerBuilder::addGlobalExtension(
00121     PassManagerBuilder::ExtensionPointTy Ty,
00122     PassManagerBuilder::ExtensionFn Fn) {
00123   GlobalExtensions->push_back(std::make_pair(Ty, Fn));
00124 }
00125 
00126 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
00127   Extensions.push_back(std::make_pair(Ty, Fn));
00128 }
00129 
00130 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
00131                                            legacy::PassManagerBase &PM) const {
00132   for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
00133     if ((*GlobalExtensions)[i].first == ETy)
00134       (*GlobalExtensions)[i].second(*this, PM);
00135   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
00136     if (Extensions[i].first == ETy)
00137       Extensions[i].second(*this, PM);
00138 }
00139 
00140 void PassManagerBuilder::addInitialAliasAnalysisPasses(
00141     legacy::PassManagerBase &PM) const {
00142   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
00143   // BasicAliasAnalysis wins if they disagree. This is intended to help
00144   // support "obvious" type-punning idioms.
00145   if (UseCFLAA)
00146     PM.add(createCFLAliasAnalysisPass());
00147   PM.add(createTypeBasedAliasAnalysisPass());
00148   PM.add(createScopedNoAliasAAPass());
00149   PM.add(createBasicAliasAnalysisPass());
00150 }
00151 
00152 void PassManagerBuilder::populateFunctionPassManager(
00153     legacy::FunctionPassManager &FPM) {
00154   addExtensionsToPM(EP_EarlyAsPossible, FPM);
00155 
00156   // Add LibraryInfo if we have some.
00157   if (LibraryInfo)
00158     FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00159 
00160   if (OptLevel == 0) return;
00161 
00162   addInitialAliasAnalysisPasses(FPM);
00163 
00164   FPM.add(createCFGSimplificationPass());
00165   if (UseNewSROA)
00166     FPM.add(createSROAPass());
00167   else
00168     FPM.add(createScalarReplAggregatesPass());
00169   FPM.add(createEarlyCSEPass());
00170   FPM.add(createLowerExpectIntrinsicPass());
00171 }
00172 
00173 void PassManagerBuilder::populateModulePassManager(
00174     legacy::PassManagerBase &MPM) {
00175   // If all optimizations are disabled, just run the always-inline pass and,
00176   // if enabled, the function merging pass.
00177   if (OptLevel == 0) {
00178     if (Inliner) {
00179       MPM.add(Inliner);
00180       Inliner = nullptr;
00181     }
00182 
00183     // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
00184     // creates a CGSCC pass manager, but we don't want to add extensions into
00185     // that pass manager. To prevent this we insert a no-op module pass to reset
00186     // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
00187     // builds. The function merging pass is 
00188     if (MergeFunctions)
00189       MPM.add(createMergeFunctionsPass());
00190     else if (!GlobalExtensions->empty() || !Extensions.empty())
00191       MPM.add(createBarrierNoopPass());
00192 
00193     addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
00194     return;
00195   }
00196 
00197   // Add LibraryInfo if we have some.
00198   if (LibraryInfo)
00199     MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00200 
00201   addInitialAliasAnalysisPasses(MPM);
00202 
00203   if (!DisableUnitAtATime) {
00204     addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
00205 
00206     MPM.add(createIPSCCPPass());              // IP SCCP
00207     MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
00208 
00209     MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
00210 
00211     MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
00212     addExtensionsToPM(EP_Peephole, MPM);
00213     MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
00214   }
00215 
00216   // Start of CallGraph SCC passes.
00217   if (!DisableUnitAtATime)
00218     MPM.add(createPruneEHPass());             // Remove dead EH info
00219   if (Inliner) {
00220     MPM.add(Inliner);
00221     Inliner = nullptr;
00222   }
00223   if (!DisableUnitAtATime)
00224     MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
00225   if (OptLevel > 2)
00226     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
00227 
00228   // Start of function pass.
00229   // Break up aggregate allocas, using SSAUpdater.
00230   if (UseNewSROA)
00231     MPM.add(createSROAPass(/*RequiresDomTree*/ false));
00232   else
00233     MPM.add(createScalarReplAggregatesPass(-1, false));
00234   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
00235   MPM.add(createJumpThreadingPass());         // Thread jumps.
00236   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
00237   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00238   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
00239   addExtensionsToPM(EP_Peephole, MPM);
00240 
00241   if (!DisableTailCalls)
00242     MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
00243   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00244   MPM.add(createReassociatePass());           // Reassociate expressions
00245   // Rotate Loop - disable header duplication at -Oz
00246   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
00247   MPM.add(createLICMPass());                  // Hoist loop invariants
00248   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
00249   MPM.add(createInstructionCombiningPass());
00250   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
00251   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
00252   MPM.add(createLoopDeletionPass());          // Delete dead loops
00253   if (EnableLoopInterchange) {
00254     MPM.add(createLoopInterchangePass()); // Interchange loops
00255     MPM.add(createCFGSimplificationPass());
00256   }
00257   if (!DisableUnrollLoops)
00258     MPM.add(createSimpleLoopUnrollPass());    // Unroll small loops
00259   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
00260 
00261   if (OptLevel > 1) {
00262     if (EnableMLSM)
00263       MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
00264     MPM.add(createGVNPass(DisableGVNLoadPRE));  // Remove redundancies
00265   }
00266   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
00267   MPM.add(createSCCPPass());                  // Constant prop with SCCP
00268 
00269   // Delete dead bit computations (instcombine runs after to fold away the dead
00270   // computations, and then ADCE will run later to exploit any new DCE
00271   // opportunities that creates).
00272   MPM.add(createBitTrackingDCEPass());        // Delete dead bit computations
00273 
00274   // Run instcombine after redundancy elimination to exploit opportunities
00275   // opened up by them.
00276   MPM.add(createInstructionCombiningPass());
00277   addExtensionsToPM(EP_Peephole, MPM);
00278   MPM.add(createJumpThreadingPass());         // Thread jumps
00279   MPM.add(createCorrelatedValuePropagationPass());
00280   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
00281   MPM.add(createLICMPass());
00282 
00283   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
00284 
00285   if (RerollLoops)
00286     MPM.add(createLoopRerollPass());
00287   if (!RunSLPAfterLoopVectorization) {
00288     if (SLPVectorize)
00289       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
00290 
00291     if (BBVectorize) {
00292       MPM.add(createBBVectorizePass());
00293       MPM.add(createInstructionCombiningPass());
00294       addExtensionsToPM(EP_Peephole, MPM);
00295       if (OptLevel > 1 && UseGVNAfterVectorization)
00296         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
00297       else
00298         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
00299 
00300       // BBVectorize may have significantly shortened a loop body; unroll again.
00301       if (!DisableUnrollLoops)
00302         MPM.add(createLoopUnrollPass());
00303     }
00304   }
00305 
00306   if (LoadCombine)
00307     MPM.add(createLoadCombinePass());
00308 
00309   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
00310   MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
00311   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
00312   addExtensionsToPM(EP_Peephole, MPM);
00313 
00314   // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
00315   // pass manager that we are specifically trying to avoid. To prevent this
00316   // we must insert a no-op module pass to reset the pass manager.
00317   MPM.add(createBarrierNoopPass());
00318 
00319   if (RunFloat2Int)
00320     MPM.add(createFloat2IntPass());
00321 
00322   // Re-rotate loops in all our loop nests. These may have fallout out of
00323   // rotated form due to GVN or other transformations, and the vectorizer relies
00324   // on the rotated form.
00325   MPM.add(createLoopRotatePass());
00326 
00327   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
00328   // into separate loop that would otherwise inhibit vectorization.
00329   if (EnableLoopDistribute)
00330     MPM.add(createLoopDistributePass());
00331 
00332   MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
00333   // FIXME: Because of #pragma vectorize enable, the passes below are always
00334   // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
00335   // on -O1 and no #pragma is found). Would be good to have these two passes
00336   // as function calls, so that we can only pass them when the vectorizer
00337   // changed the code.
00338   MPM.add(createInstructionCombiningPass());
00339   if (OptLevel > 1 && ExtraVectorizerPasses) {
00340     // At higher optimization levels, try to clean up any runtime overlap and
00341     // alignment checks inserted by the vectorizer. We want to track correllated
00342     // runtime checks for two inner loops in the same outer loop, fold any
00343     // common computations, hoist loop-invariant aspects out of any outer loop,
00344     // and unswitch the runtime checks if possible. Once hoisted, we may have
00345     // dead (or speculatable) control flows or more combining opportunities.
00346     MPM.add(createEarlyCSEPass());
00347     MPM.add(createCorrelatedValuePropagationPass());
00348     MPM.add(createInstructionCombiningPass());
00349     MPM.add(createLICMPass());
00350     MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
00351     MPM.add(createCFGSimplificationPass());
00352     MPM.add(createInstructionCombiningPass());
00353   }
00354 
00355   if (RunSLPAfterLoopVectorization) {
00356     if (SLPVectorize) {
00357       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
00358       if (OptLevel > 1 && ExtraVectorizerPasses) {
00359         MPM.add(createEarlyCSEPass());
00360       }
00361     }
00362 
00363     if (BBVectorize) {
00364       MPM.add(createBBVectorizePass());
00365       MPM.add(createInstructionCombiningPass());
00366       addExtensionsToPM(EP_Peephole, MPM);
00367       if (OptLevel > 1 && UseGVNAfterVectorization)
00368         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
00369       else
00370         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
00371 
00372       // BBVectorize may have significantly shortened a loop body; unroll again.
00373       if (!DisableUnrollLoops)
00374         MPM.add(createLoopUnrollPass());
00375     }
00376   }
00377 
00378   addExtensionsToPM(EP_Peephole, MPM);
00379   MPM.add(createCFGSimplificationPass());
00380   MPM.add(createInstructionCombiningPass());
00381 
00382   if (!DisableUnrollLoops) {
00383     MPM.add(createLoopUnrollPass());    // Unroll small loops
00384 
00385     // LoopUnroll may generate some redundency to cleanup.
00386     MPM.add(createInstructionCombiningPass());
00387 
00388     // Runtime unrolling will introduce runtime check in loop prologue. If the
00389     // unrolled loop is a inner loop, then the prologue will be inside the
00390     // outer loop. LICM pass can help to promote the runtime check out if the
00391     // checked value is loop invariant.
00392     MPM.add(createLICMPass());
00393   }
00394 
00395   // After vectorization and unrolling, assume intrinsics may tell us more
00396   // about pointer alignments.
00397   MPM.add(createAlignmentFromAssumptionsPass());
00398 
00399   if (!DisableUnitAtATime) {
00400     // FIXME: We shouldn't bother with this anymore.
00401     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
00402 
00403     // GlobalOpt already deletes dead functions and globals, at -O2 try a
00404     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
00405     if (OptLevel > 1) {
00406       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
00407       MPM.add(createConstantMergePass());     // Merge dup global constants
00408     }
00409   }
00410 
00411   if (MergeFunctions)
00412     MPM.add(createMergeFunctionsPass());
00413 
00414   addExtensionsToPM(EP_OptimizerLast, MPM);
00415 }
00416 
00417 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
00418   // Provide AliasAnalysis services for optimizations.
00419   addInitialAliasAnalysisPasses(PM);
00420 
00421   // Propagate constants at call sites into the functions they call.  This
00422   // opens opportunities for globalopt (and inlining) by substituting function
00423   // pointers passed as arguments to direct uses of functions.
00424   PM.add(createIPSCCPPass());
00425 
00426   // Now that we internalized some globals, see if we can hack on them!
00427   PM.add(createGlobalOptimizerPass());
00428 
00429   // Linking modules together can lead to duplicated global constants, only
00430   // keep one copy of each constant.
00431   PM.add(createConstantMergePass());
00432 
00433   // Remove unused arguments from functions.
00434   PM.add(createDeadArgEliminationPass());
00435 
00436   // Reduce the code after globalopt and ipsccp.  Both can open up significant
00437   // simplification opportunities, and both can propagate functions through
00438   // function pointers.  When this happens, we often have to resolve varargs
00439   // calls, etc, so let instcombine do this.
00440   PM.add(createInstructionCombiningPass());
00441   addExtensionsToPM(EP_Peephole, PM);
00442 
00443   // Inline small functions
00444   bool RunInliner = Inliner;
00445   if (RunInliner) {
00446     PM.add(Inliner);
00447     Inliner = nullptr;
00448   }
00449 
00450   PM.add(createPruneEHPass());   // Remove dead EH info.
00451 
00452   // Optimize globals again if we ran the inliner.
00453   if (RunInliner)
00454     PM.add(createGlobalOptimizerPass());
00455   PM.add(createGlobalDCEPass()); // Remove dead functions.
00456 
00457   // If we didn't decide to inline a function, check to see if we can
00458   // transform it to pass arguments by value instead of by reference.
00459   PM.add(createArgumentPromotionPass());
00460 
00461   // The IPO passes may leave cruft around.  Clean up after them.
00462   PM.add(createInstructionCombiningPass());
00463   addExtensionsToPM(EP_Peephole, PM);
00464   PM.add(createJumpThreadingPass());
00465 
00466   // Break up allocas
00467   if (UseNewSROA)
00468     PM.add(createSROAPass());
00469   else
00470     PM.add(createScalarReplAggregatesPass());
00471 
00472   // Run a few AA driven optimizations here and now, to cleanup the code.
00473   PM.add(createFunctionAttrsPass()); // Add nocapture.
00474   PM.add(createGlobalsModRefPass()); // IP alias analysis.
00475 
00476   PM.add(createLICMPass());                 // Hoist loop invariants.
00477   if (EnableMLSM)
00478     PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
00479   PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
00480   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
00481 
00482   // Nuke dead stores.
00483   PM.add(createDeadStoreEliminationPass());
00484 
00485   // More loops are countable; try to optimize them.
00486   PM.add(createIndVarSimplifyPass());
00487   PM.add(createLoopDeletionPass());
00488   if (EnableLoopInterchange)
00489     PM.add(createLoopInterchangePass());
00490 
00491   PM.add(createLoopVectorizePass(true, LoopVectorize));
00492 
00493   // More scalar chains could be vectorized due to more alias information
00494   if (RunSLPAfterLoopVectorization)
00495     if (SLPVectorize)
00496       PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
00497 
00498   // After vectorization, assume intrinsics may tell us more about pointer
00499   // alignments.
00500   PM.add(createAlignmentFromAssumptionsPass());
00501 
00502   if (LoadCombine)
00503     PM.add(createLoadCombinePass());
00504 
00505   // Cleanup and simplify the code after the scalar optimizations.
00506   PM.add(createInstructionCombiningPass());
00507   addExtensionsToPM(EP_Peephole, PM);
00508 
00509   PM.add(createJumpThreadingPass());
00510 }
00511 
00512 void PassManagerBuilder::addLateLTOOptimizationPasses(
00513     legacy::PassManagerBase &PM) {
00514   // Delete basic blocks, which optimization passes may have killed.
00515   PM.add(createCFGSimplificationPass());
00516 
00517   // Now that we have optimized the program, discard unreachable functions.
00518   PM.add(createGlobalDCEPass());
00519 
00520   // FIXME: this is profitable (for compiler time) to do at -O0 too, but
00521   // currently it damages debug info.
00522   if (MergeFunctions)
00523     PM.add(createMergeFunctionsPass());
00524 }
00525 
00526 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
00527   if (LibraryInfo)
00528     PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00529 
00530   if (VerifyInput)
00531     PM.add(createVerifierPass());
00532 
00533   if (OptLevel > 1)
00534     addLTOOptimizationPasses(PM);
00535 
00536   // Lower bit sets to globals. This pass supports Clang's control flow
00537   // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
00538   // is enabled. The pass does nothing if CFI is disabled.
00539   PM.add(createLowerBitSetsPass());
00540 
00541   if (OptLevel != 0)
00542     addLateLTOOptimizationPasses(PM);
00543 
00544   if (VerifyOutput)
00545     PM.add(createVerifierPass());
00546 }
00547 
00548 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
00549     return reinterpret_cast<PassManagerBuilder*>(P);
00550 }
00551 
00552 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
00553   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
00554 }
00555 
00556 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
00557   PassManagerBuilder *PMB = new PassManagerBuilder();
00558   return wrap(PMB);
00559 }
00560 
00561 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
00562   PassManagerBuilder *Builder = unwrap(PMB);
00563   delete Builder;
00564 }
00565 
00566 void
00567 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
00568                                   unsigned OptLevel) {
00569   PassManagerBuilder *Builder = unwrap(PMB);
00570   Builder->OptLevel = OptLevel;
00571 }
00572 
00573 void
00574 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
00575                                    unsigned SizeLevel) {
00576   PassManagerBuilder *Builder = unwrap(PMB);
00577   Builder->SizeLevel = SizeLevel;
00578 }
00579 
00580 void
00581 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
00582                                             LLVMBool Value) {
00583   PassManagerBuilder *Builder = unwrap(PMB);
00584   Builder->DisableUnitAtATime = Value;
00585 }
00586 
00587 void
00588 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
00589                                             LLVMBool Value) {
00590   PassManagerBuilder *Builder = unwrap(PMB);
00591   Builder->DisableUnrollLoops = Value;
00592 }
00593 
00594 void
00595 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
00596                                                  LLVMBool Value) {
00597   // NOTE: The simplify-libcalls pass has been removed.
00598 }
00599 
00600 void
00601 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
00602                                               unsigned Threshold) {
00603   PassManagerBuilder *Builder = unwrap(PMB);
00604   Builder->Inliner = createFunctionInliningPass(Threshold);
00605 }
00606 
00607 void
00608 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
00609                                                   LLVMPassManagerRef PM) {
00610   PassManagerBuilder *Builder = unwrap(PMB);
00611   legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
00612   Builder->populateFunctionPassManager(*FPM);
00613 }
00614 
00615 void
00616 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
00617                                                 LLVMPassManagerRef PM) {
00618   PassManagerBuilder *Builder = unwrap(PMB);
00619   legacy::PassManagerBase *MPM = unwrap(PM);
00620   Builder->populateModulePassManager(*MPM);
00621 }
00622 
00623 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
00624                                                   LLVMPassManagerRef PM,
00625                                                   LLVMBool Internalize,
00626                                                   LLVMBool RunInliner) {
00627   PassManagerBuilder *Builder = unwrap(PMB);
00628   legacy::PassManagerBase *LPM = unwrap(PM);
00629 
00630   // A small backwards compatibility hack. populateLTOPassManager used to take
00631   // an RunInliner option.
00632   if (RunInliner && !Builder->Inliner)
00633     Builder->Inliner = createFunctionInliningPass();
00634 
00635   Builder->populateLTOPassManager(*LPM);
00636 }