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