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 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
00016 #include "llvm-c/Transforms/PassManagerBuilder.h"
00017 #include "llvm/ADT/SmallVector.h"
00018 #include "llvm/Analysis/BasicAliasAnalysis.h"
00019 #include "llvm/Analysis/CFLAliasAnalysis.h"
00020 #include "llvm/Analysis/GlobalsModRef.h"
00021 #include "llvm/Analysis/Passes.h"
00022 #include "llvm/Analysis/ScopedNoAliasAA.h"
00023 #include "llvm/Analysis/TargetLibraryInfo.h"
00024 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
00025 #include "llvm/IR/DataLayout.h"
00026 #include "llvm/IR/FunctionInfo.h"
00027 #include "llvm/IR/LegacyPassManager.h"
00028 #include "llvm/IR/Verifier.h"
00029 #include "llvm/Support/CommandLine.h"
00030 #include "llvm/Support/ManagedStatic.h"
00031 #include "llvm/Target/TargetMachine.h"
00032 #include "llvm/Transforms/IPO.h"
00033 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
00034 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
00035 #include "llvm/Transforms/Scalar.h"
00036 #include "llvm/Transforms/Vectorize.h"
00037 #include "llvm/Transforms/Instrumentation.h"
00038 
00039 using namespace llvm;
00040 
00041 static cl::opt<bool>
00042 RunLoopVectorization("vectorize-loops", cl::Hidden,
00043                      cl::desc("Run the Loop vectorization passes"));
00044 
00045 static cl::opt<bool>
00046 RunSLPVectorization("vectorize-slp", cl::Hidden,
00047                     cl::desc("Run the SLP vectorization passes"));
00048 
00049 static cl::opt<bool>
00050 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
00051                     cl::desc("Run the BB vectorization passes"));
00052 
00053 static cl::opt<bool>
00054 UseGVNAfterVectorization("use-gvn-after-vectorization",
00055   cl::init(false), cl::Hidden,
00056   cl::desc("Run GVN instead of Early CSE after vectorization passes"));
00057 
00058 static cl::opt<bool> ExtraVectorizerPasses(
00059     "extra-vectorizer-passes", cl::init(false), cl::Hidden,
00060     cl::desc("Run cleanup optimization passes after vectorization."));
00061 
00062 static cl::opt<bool> UseNewSROA("use-new-sroa",
00063   cl::init(true), cl::Hidden,
00064   cl::desc("Enable the new, experimental SROA pass"));
00065 
00066 static cl::opt<bool>
00067 RunLoopRerolling("reroll-loops", cl::Hidden,
00068                  cl::desc("Run the loop rerolling pass"));
00069 
00070 static cl::opt<bool>
00071 RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
00072              cl::desc("Run the float2int (float demotion) pass"));
00073 
00074 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
00075                                     cl::Hidden,
00076                                     cl::desc("Run the load combining pass"));
00077 
00078 static cl::opt<bool>
00079 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
00080   cl::init(true), cl::Hidden,
00081   cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
00082            "vectorizer instead of before"));
00083 
00084 static cl::opt<bool> UseCFLAA("use-cfl-aa",
00085   cl::init(false), cl::Hidden,
00086   cl::desc("Enable the new, experimental CFL alias analysis"));
00087 
00088 static cl::opt<bool>
00089 EnableMLSM("mlsm", cl::init(true), cl::Hidden,
00090            cl::desc("Enable motion of merged load and store"));
00091 
00092 static cl::opt<bool> EnableLoopInterchange(
00093     "enable-loopinterchange", cl::init(false), cl::Hidden,
00094     cl::desc("Enable the new, experimental LoopInterchange Pass"));
00095 
00096 static cl::opt<bool> EnableLoopDistribute(
00097     "enable-loop-distribute", cl::init(false), cl::Hidden,
00098     cl::desc("Enable the new, experimental LoopDistribution Pass"));
00099 
00100 static cl::opt<bool> EnableNonLTOGlobalsModRef(
00101     "enable-non-lto-gmr", cl::init(true), cl::Hidden,
00102     cl::desc(
00103         "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
00104 
00105 static cl::opt<bool> EnableLoopLoadElim(
00106     "enable-loop-load-elim", cl::init(false), cl::Hidden,
00107     cl::desc("Enable the new, experimental LoopLoadElimination Pass"));
00108 
00109 static cl::opt<std::string> RunPGOInstrGen(
00110     "profile-generate", cl::init(""), cl::Hidden,
00111     cl::desc("Enable generation phase of PGO instrumentation and specify the "
00112              "path of profile data file"));
00113 
00114 static cl::opt<std::string> RunPGOInstrUse(
00115     "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"),
00116     cl::desc("Enable use phase of PGO instrumentation and specify the path "
00117              "of profile data file"));
00118 
00119 PassManagerBuilder::PassManagerBuilder() {
00120     OptLevel = 2;
00121     SizeLevel = 0;
00122     LibraryInfo = nullptr;
00123     Inliner = nullptr;
00124     FunctionIndex = nullptr;
00125     DisableUnitAtATime = false;
00126     DisableUnrollLoops = false;
00127     BBVectorize = RunBBVectorization;
00128     SLPVectorize = RunSLPVectorization;
00129     LoopVectorize = RunLoopVectorization;
00130     RerollLoops = RunLoopRerolling;
00131     LoadCombine = RunLoadCombine;
00132     DisableGVNLoadPRE = false;
00133     VerifyInput = false;
00134     VerifyOutput = false;
00135     MergeFunctions = false;
00136     PrepareForLTO = false;
00137     PGOInstrGen = RunPGOInstrGen;
00138     PGOInstrUse = RunPGOInstrUse;
00139 }
00140 
00141 PassManagerBuilder::~PassManagerBuilder() {
00142   delete LibraryInfo;
00143   delete Inliner;
00144 }
00145 
00146 /// Set of global extensions, automatically added as part of the standard set.
00147 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
00148    PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
00149 
00150 void PassManagerBuilder::addGlobalExtension(
00151     PassManagerBuilder::ExtensionPointTy Ty,
00152     PassManagerBuilder::ExtensionFn Fn) {
00153   GlobalExtensions->push_back(std::make_pair(Ty, Fn));
00154 }
00155 
00156 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
00157   Extensions.push_back(std::make_pair(Ty, Fn));
00158 }
00159 
00160 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
00161                                            legacy::PassManagerBase &PM) const {
00162   for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
00163     if ((*GlobalExtensions)[i].first == ETy)
00164       (*GlobalExtensions)[i].second(*this, PM);
00165   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
00166     if (Extensions[i].first == ETy)
00167       Extensions[i].second(*this, PM);
00168 }
00169 
00170 void PassManagerBuilder::addInitialAliasAnalysisPasses(
00171     legacy::PassManagerBase &PM) const {
00172   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
00173   // BasicAliasAnalysis wins if they disagree. This is intended to help
00174   // support "obvious" type-punning idioms.
00175   if (UseCFLAA)
00176     PM.add(createCFLAAWrapperPass());
00177   PM.add(createTypeBasedAAWrapperPass());
00178   PM.add(createScopedNoAliasAAWrapperPass());
00179 }
00180 
00181 void PassManagerBuilder::populateFunctionPassManager(
00182     legacy::FunctionPassManager &FPM) {
00183   addExtensionsToPM(EP_EarlyAsPossible, FPM);
00184 
00185   // Add LibraryInfo if we have some.
00186   if (LibraryInfo)
00187     FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00188 
00189   if (OptLevel == 0) return;
00190 
00191   addInitialAliasAnalysisPasses(FPM);
00192 
00193   FPM.add(createCFGSimplificationPass());
00194   if (UseNewSROA)
00195     FPM.add(createSROAPass());
00196   else
00197     FPM.add(createScalarReplAggregatesPass());
00198   FPM.add(createEarlyCSEPass());
00199   FPM.add(createLowerExpectIntrinsicPass());
00200 }
00201 
00202 // Do PGO instrumentation generation or use pass as the option specified.
00203 void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
00204   if (!PGOInstrGen.empty()) {
00205     MPM.add(createPGOInstrumentationGenPass());
00206     // Add the profile lowering pass.
00207     InstrProfOptions Options;
00208     Options.InstrProfileOutput = PGOInstrGen;
00209     MPM.add(createInstrProfilingPass(Options));
00210   }
00211   if (!PGOInstrUse.empty())
00212     MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
00213 }
00214 
00215 void PassManagerBuilder::populateModulePassManager(
00216     legacy::PassManagerBase &MPM) {
00217   // Allow forcing function attributes as a debugging and tuning aid.
00218   MPM.add(createForceFunctionAttrsLegacyPass());
00219 
00220   // If all optimizations are disabled, just run the always-inline pass and,
00221   // if enabled, the function merging pass.
00222   if (OptLevel == 0) {
00223     addPGOInstrPasses(MPM);
00224     if (Inliner) {
00225       MPM.add(Inliner);
00226       Inliner = nullptr;
00227     }
00228 
00229     // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
00230     // creates a CGSCC pass manager, but we don't want to add extensions into
00231     // that pass manager. To prevent this we insert a no-op module pass to reset
00232     // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
00233     // builds. The function merging pass is 
00234     if (MergeFunctions)
00235       MPM.add(createMergeFunctionsPass());
00236     else if (!GlobalExtensions->empty() || !Extensions.empty())
00237       MPM.add(createBarrierNoopPass());
00238 
00239     addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
00240     return;
00241   }
00242 
00243   // Add LibraryInfo if we have some.
00244   if (LibraryInfo)
00245     MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00246 
00247   addInitialAliasAnalysisPasses(MPM);
00248 
00249   if (!DisableUnitAtATime) {
00250     // Infer attributes about declarations if possible.
00251     MPM.add(createInferFunctionAttrsLegacyPass());
00252 
00253     addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
00254 
00255     MPM.add(createIPSCCPPass());              // IP SCCP
00256     MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
00257     // Promote any localized global vars
00258     MPM.add(createPromoteMemoryToRegisterPass());
00259 
00260     MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
00261 
00262     MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
00263     addExtensionsToPM(EP_Peephole, MPM);
00264     MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
00265   }
00266 
00267   addPGOInstrPasses(MPM);
00268 
00269   if (EnableNonLTOGlobalsModRef)
00270     // We add a module alias analysis pass here. In part due to bugs in the
00271     // analysis infrastructure this "works" in that the analysis stays alive
00272     // for the entire SCC pass run below.
00273     MPM.add(createGlobalsAAWrapperPass());
00274 
00275   // Start of CallGraph SCC passes.
00276   if (!DisableUnitAtATime)
00277     MPM.add(createPruneEHPass());             // Remove dead EH info
00278   if (Inliner) {
00279     MPM.add(Inliner);
00280     Inliner = nullptr;
00281   }
00282   if (!DisableUnitAtATime)
00283     MPM.add(createPostOrderFunctionAttrsPass());
00284   if (OptLevel > 2)
00285     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
00286 
00287   // Start of function pass.
00288   // Break up aggregate allocas, using SSAUpdater.
00289   if (UseNewSROA)
00290     MPM.add(createSROAPass());
00291   else
00292     MPM.add(createScalarReplAggregatesPass(-1, false));
00293   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
00294   MPM.add(createJumpThreadingPass());         // Thread jumps.
00295   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
00296   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00297   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
00298   addExtensionsToPM(EP_Peephole, MPM);
00299 
00300   MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
00301   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00302   MPM.add(createReassociatePass());           // Reassociate expressions
00303   // Rotate Loop - disable header duplication at -Oz
00304   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
00305   MPM.add(createLICMPass());                  // Hoist loop invariants
00306   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
00307   MPM.add(createCFGSimplificationPass());
00308   MPM.add(createInstructionCombiningPass());
00309   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
00310   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
00311   MPM.add(createLoopDeletionPass());          // Delete dead loops
00312   if (EnableLoopInterchange) {
00313     MPM.add(createLoopInterchangePass()); // Interchange loops
00314     MPM.add(createCFGSimplificationPass());
00315   }
00316   if (!DisableUnrollLoops)
00317     MPM.add(createSimpleLoopUnrollPass());    // Unroll small loops
00318   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
00319 
00320   if (OptLevel > 1) {
00321     if (EnableMLSM)
00322       MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
00323     MPM.add(createGVNPass(DisableGVNLoadPRE));  // Remove redundancies
00324   }
00325   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
00326   MPM.add(createSCCPPass());                  // Constant prop with SCCP
00327 
00328   // Delete dead bit computations (instcombine runs after to fold away the dead
00329   // computations, and then ADCE will run later to exploit any new DCE
00330   // opportunities that creates).
00331   MPM.add(createBitTrackingDCEPass());        // Delete dead bit computations
00332 
00333   // Run instcombine after redundancy elimination to exploit opportunities
00334   // opened up by them.
00335   MPM.add(createInstructionCombiningPass());
00336   addExtensionsToPM(EP_Peephole, MPM);
00337   MPM.add(createJumpThreadingPass());         // Thread jumps
00338   MPM.add(createCorrelatedValuePropagationPass());
00339   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
00340   MPM.add(createLICMPass());
00341 
00342   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
00343 
00344   if (RerollLoops)
00345     MPM.add(createLoopRerollPass());
00346   if (!RunSLPAfterLoopVectorization) {
00347     if (SLPVectorize)
00348       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
00349 
00350     if (BBVectorize) {
00351       MPM.add(createBBVectorizePass());
00352       MPM.add(createInstructionCombiningPass());
00353       addExtensionsToPM(EP_Peephole, MPM);
00354       if (OptLevel > 1 && UseGVNAfterVectorization)
00355         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
00356       else
00357         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
00358 
00359       // BBVectorize may have significantly shortened a loop body; unroll again.
00360       if (!DisableUnrollLoops)
00361         MPM.add(createLoopUnrollPass());
00362     }
00363   }
00364 
00365   if (LoadCombine)
00366     MPM.add(createLoadCombinePass());
00367 
00368   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
00369   MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
00370   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
00371   addExtensionsToPM(EP_Peephole, MPM);
00372 
00373   // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
00374   // pass manager that we are specifically trying to avoid. To prevent this
00375   // we must insert a no-op module pass to reset the pass manager.
00376   MPM.add(createBarrierNoopPass());
00377 
00378   if (!DisableUnitAtATime)
00379     MPM.add(createReversePostOrderFunctionAttrsPass());
00380 
00381   if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) {
00382     // Remove avail extern fns and globals definitions if we aren't
00383     // compiling an object file for later LTO. For LTO we want to preserve
00384     // these so they are eligible for inlining at link-time. Note if they
00385     // are unreferenced they will be removed by GlobalDCE later, so
00386     // this only impacts referenced available externally globals.
00387     // Eventually they will be suppressed during codegen, but eliminating
00388     // here enables more opportunity for GlobalDCE as it may make
00389     // globals referenced by available external functions dead
00390     // and saves running remaining passes on the eliminated functions.
00391     MPM.add(createEliminateAvailableExternallyPass());
00392   }
00393 
00394   if (EnableNonLTOGlobalsModRef)
00395     // We add a fresh GlobalsModRef run at this point. This is particularly
00396     // useful as the above will have inlined, DCE'ed, and function-attr
00397     // propagated everything. We should at this point have a reasonably minimal
00398     // and richly annotated call graph. By computing aliasing and mod/ref
00399     // information for all local globals here, the late loop passes and notably
00400     // the vectorizer will be able to use them to help recognize vectorizable
00401     // memory operations.
00402     //
00403     // Note that this relies on a bug in the pass manager which preserves
00404     // a module analysis into a function pass pipeline (and throughout it) so
00405     // long as the first function pass doesn't invalidate the module analysis.
00406     // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
00407     // this to work. Fortunately, it is trivial to preserve AliasAnalysis
00408     // (doing nothing preserves it as it is required to be conservatively
00409     // correct in the face of IR changes).
00410     MPM.add(createGlobalsAAWrapperPass());
00411 
00412   if (RunFloat2Int)
00413     MPM.add(createFloat2IntPass());
00414 
00415   addExtensionsToPM(EP_VectorizerStart, MPM);
00416 
00417   // Re-rotate loops in all our loop nests. These may have fallout out of
00418   // rotated form due to GVN or other transformations, and the vectorizer relies
00419   // on the rotated form. Disable header duplication at -Oz.
00420   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
00421 
00422   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
00423   // into separate loop that would otherwise inhibit vectorization.
00424   if (EnableLoopDistribute)
00425     MPM.add(createLoopDistributePass());
00426 
00427   MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
00428 
00429   // Eliminate loads by forwarding stores from the previous iteration to loads
00430   // of the current iteration.
00431   if (EnableLoopLoadElim)
00432     MPM.add(createLoopLoadEliminationPass());
00433 
00434   // FIXME: Because of #pragma vectorize enable, the passes below are always
00435   // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
00436   // on -O1 and no #pragma is found). Would be good to have these two passes
00437   // as function calls, so that we can only pass them when the vectorizer
00438   // changed the code.
00439   MPM.add(createInstructionCombiningPass());
00440   if (OptLevel > 1 && ExtraVectorizerPasses) {
00441     // At higher optimization levels, try to clean up any runtime overlap and
00442     // alignment checks inserted by the vectorizer. We want to track correllated
00443     // runtime checks for two inner loops in the same outer loop, fold any
00444     // common computations, hoist loop-invariant aspects out of any outer loop,
00445     // and unswitch the runtime checks if possible. Once hoisted, we may have
00446     // dead (or speculatable) control flows or more combining opportunities.
00447     MPM.add(createEarlyCSEPass());
00448     MPM.add(createCorrelatedValuePropagationPass());
00449     MPM.add(createInstructionCombiningPass());
00450     MPM.add(createLICMPass());
00451     MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
00452     MPM.add(createCFGSimplificationPass());
00453     MPM.add(createInstructionCombiningPass());
00454   }
00455 
00456   if (RunSLPAfterLoopVectorization) {
00457     if (SLPVectorize) {
00458       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
00459       if (OptLevel > 1 && ExtraVectorizerPasses) {
00460         MPM.add(createEarlyCSEPass());
00461       }
00462     }
00463 
00464     if (BBVectorize) {
00465       MPM.add(createBBVectorizePass());
00466       MPM.add(createInstructionCombiningPass());
00467       addExtensionsToPM(EP_Peephole, MPM);
00468       if (OptLevel > 1 && UseGVNAfterVectorization)
00469         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
00470       else
00471         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
00472 
00473       // BBVectorize may have significantly shortened a loop body; unroll again.
00474       if (!DisableUnrollLoops)
00475         MPM.add(createLoopUnrollPass());
00476     }
00477   }
00478 
00479   addExtensionsToPM(EP_Peephole, MPM);
00480   MPM.add(createCFGSimplificationPass());
00481   MPM.add(createInstructionCombiningPass());
00482 
00483   if (!DisableUnrollLoops) {
00484     MPM.add(createLoopUnrollPass());    // Unroll small loops
00485 
00486     // LoopUnroll may generate some redundency to cleanup.
00487     MPM.add(createInstructionCombiningPass());
00488 
00489     // Runtime unrolling will introduce runtime check in loop prologue. If the
00490     // unrolled loop is a inner loop, then the prologue will be inside the
00491     // outer loop. LICM pass can help to promote the runtime check out if the
00492     // checked value is loop invariant.
00493     MPM.add(createLICMPass());
00494   }
00495 
00496   // After vectorization and unrolling, assume intrinsics may tell us more
00497   // about pointer alignments.
00498   MPM.add(createAlignmentFromAssumptionsPass());
00499 
00500   if (!DisableUnitAtATime) {
00501     // FIXME: We shouldn't bother with this anymore.
00502     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
00503 
00504     // GlobalOpt already deletes dead functions and globals, at -O2 try a
00505     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
00506     if (OptLevel > 1) {
00507       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
00508       MPM.add(createConstantMergePass());     // Merge dup global constants
00509     }
00510   }
00511 
00512   if (MergeFunctions)
00513     MPM.add(createMergeFunctionsPass());
00514 
00515   addExtensionsToPM(EP_OptimizerLast, MPM);
00516 }
00517 
00518 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
00519   // Provide AliasAnalysis services for optimizations.
00520   addInitialAliasAnalysisPasses(PM);
00521 
00522   if (FunctionIndex)
00523     PM.add(createFunctionImportPass(FunctionIndex));
00524 
00525   // Allow forcing function attributes as a debugging and tuning aid.
00526   PM.add(createForceFunctionAttrsLegacyPass());
00527 
00528   // Infer attributes about declarations if possible.
00529   PM.add(createInferFunctionAttrsLegacyPass());
00530 
00531   // Propagate constants at call sites into the functions they call.  This
00532   // opens opportunities for globalopt (and inlining) by substituting function
00533   // pointers passed as arguments to direct uses of functions.
00534   PM.add(createIPSCCPPass());
00535 
00536   // Now that we internalized some globals, see if we can hack on them!
00537   PM.add(createPostOrderFunctionAttrsPass());
00538   PM.add(createReversePostOrderFunctionAttrsPass());
00539   PM.add(createGlobalOptimizerPass());
00540   // Promote any localized global vars.
00541   PM.add(createPromoteMemoryToRegisterPass());
00542 
00543   // Linking modules together can lead to duplicated global constants, only
00544   // keep one copy of each constant.
00545   PM.add(createConstantMergePass());
00546 
00547   // Remove unused arguments from functions.
00548   PM.add(createDeadArgEliminationPass());
00549 
00550   // Reduce the code after globalopt and ipsccp.  Both can open up significant
00551   // simplification opportunities, and both can propagate functions through
00552   // function pointers.  When this happens, we often have to resolve varargs
00553   // calls, etc, so let instcombine do this.
00554   PM.add(createInstructionCombiningPass());
00555   addExtensionsToPM(EP_Peephole, PM);
00556 
00557   // Inline small functions
00558   bool RunInliner = Inliner;
00559   if (RunInliner) {
00560     PM.add(Inliner);
00561     Inliner = nullptr;
00562   }
00563 
00564   PM.add(createPruneEHPass());   // Remove dead EH info.
00565 
00566   // Optimize globals again if we ran the inliner.
00567   if (RunInliner)
00568     PM.add(createGlobalOptimizerPass());
00569   PM.add(createGlobalDCEPass()); // Remove dead functions.
00570 
00571   // If we didn't decide to inline a function, check to see if we can
00572   // transform it to pass arguments by value instead of by reference.
00573   PM.add(createArgumentPromotionPass());
00574 
00575   // The IPO passes may leave cruft around.  Clean up after them.
00576   PM.add(createInstructionCombiningPass());
00577   addExtensionsToPM(EP_Peephole, PM);
00578   PM.add(createJumpThreadingPass());
00579 
00580   // Break up allocas
00581   if (UseNewSROA)
00582     PM.add(createSROAPass());
00583   else
00584     PM.add(createScalarReplAggregatesPass());
00585 
00586   // Run a few AA driven optimizations here and now, to cleanup the code.
00587   PM.add(createPostOrderFunctionAttrsPass()); // Add nocapture.
00588   PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
00589 
00590   PM.add(createLICMPass());                 // Hoist loop invariants.
00591   if (EnableMLSM)
00592     PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
00593   PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
00594   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
00595 
00596   // Nuke dead stores.
00597   PM.add(createDeadStoreEliminationPass());
00598 
00599   // More loops are countable; try to optimize them.
00600   PM.add(createIndVarSimplifyPass());
00601   PM.add(createLoopDeletionPass());
00602   if (EnableLoopInterchange)
00603     PM.add(createLoopInterchangePass());
00604 
00605   if (!DisableUnrollLoops)
00606     PM.add(createSimpleLoopUnrollPass());   // Unroll small loops
00607   PM.add(createLoopVectorizePass(true, LoopVectorize));
00608   // The vectorizer may have significantly shortened a loop body; unroll again.
00609   if (!DisableUnrollLoops)
00610     PM.add(createLoopUnrollPass());
00611 
00612   // Now that we've optimized loops (in particular loop induction variables),
00613   // we may have exposed more scalar opportunities. Run parts of the scalar
00614   // optimizer again at this point.
00615   PM.add(createInstructionCombiningPass()); // Initial cleanup
00616   PM.add(createCFGSimplificationPass()); // if-convert
00617   PM.add(createSCCPPass()); // Propagate exposed constants
00618   PM.add(createInstructionCombiningPass()); // Clean up again
00619   PM.add(createBitTrackingDCEPass());
00620 
00621   // More scalar chains could be vectorized due to more alias information
00622   if (RunSLPAfterLoopVectorization)
00623     if (SLPVectorize)
00624       PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
00625 
00626   // After vectorization, assume intrinsics may tell us more about pointer
00627   // alignments.
00628   PM.add(createAlignmentFromAssumptionsPass());
00629 
00630   if (LoadCombine)
00631     PM.add(createLoadCombinePass());
00632 
00633   // Cleanup and simplify the code after the scalar optimizations.
00634   PM.add(createInstructionCombiningPass());
00635   addExtensionsToPM(EP_Peephole, PM);
00636 
00637   PM.add(createJumpThreadingPass());
00638 }
00639 
00640 void PassManagerBuilder::addLateLTOOptimizationPasses(
00641     legacy::PassManagerBase &PM) {
00642   // Delete basic blocks, which optimization passes may have killed.
00643   PM.add(createCFGSimplificationPass());
00644 
00645   // Drop bodies of available externally objects to improve GlobalDCE.
00646   PM.add(createEliminateAvailableExternallyPass());
00647 
00648   // Now that we have optimized the program, discard unreachable functions.
00649   PM.add(createGlobalDCEPass());
00650 
00651   // FIXME: this is profitable (for compiler time) to do at -O0 too, but
00652   // currently it damages debug info.
00653   if (MergeFunctions)
00654     PM.add(createMergeFunctionsPass());
00655 }
00656 
00657 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
00658   if (LibraryInfo)
00659     PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
00660 
00661   if (VerifyInput)
00662     PM.add(createVerifierPass());
00663 
00664   if (OptLevel > 1)
00665     addLTOOptimizationPasses(PM);
00666 
00667   // Create a function that performs CFI checks for cross-DSO calls with targets
00668   // in the current module.
00669   PM.add(createCrossDSOCFIPass());
00670 
00671   // Lower bit sets to globals. This pass supports Clang's control flow
00672   // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
00673   // is enabled. The pass does nothing if CFI is disabled.
00674   PM.add(createLowerBitSetsPass());
00675 
00676   if (OptLevel != 0)
00677     addLateLTOOptimizationPasses(PM);
00678 
00679   if (VerifyOutput)
00680     PM.add(createVerifierPass());
00681 }
00682 
00683 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
00684     return reinterpret_cast<PassManagerBuilder*>(P);
00685 }
00686 
00687 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
00688   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
00689 }
00690 
00691 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
00692   PassManagerBuilder *PMB = new PassManagerBuilder();
00693   return wrap(PMB);
00694 }
00695 
00696 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
00697   PassManagerBuilder *Builder = unwrap(PMB);
00698   delete Builder;
00699 }
00700 
00701 void
00702 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
00703                                   unsigned OptLevel) {
00704   PassManagerBuilder *Builder = unwrap(PMB);
00705   Builder->OptLevel = OptLevel;
00706 }
00707 
00708 void
00709 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
00710                                    unsigned SizeLevel) {
00711   PassManagerBuilder *Builder = unwrap(PMB);
00712   Builder->SizeLevel = SizeLevel;
00713 }
00714 
00715 void
00716 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
00717                                             LLVMBool Value) {
00718   PassManagerBuilder *Builder = unwrap(PMB);
00719   Builder->DisableUnitAtATime = Value;
00720 }
00721 
00722 void
00723 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
00724                                             LLVMBool Value) {
00725   PassManagerBuilder *Builder = unwrap(PMB);
00726   Builder->DisableUnrollLoops = Value;
00727 }
00728 
00729 void
00730 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
00731                                                  LLVMBool Value) {
00732   // NOTE: The simplify-libcalls pass has been removed.
00733 }
00734 
00735 void
00736 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
00737                                               unsigned Threshold) {
00738   PassManagerBuilder *Builder = unwrap(PMB);
00739   Builder->Inliner = createFunctionInliningPass(Threshold);
00740 }
00741 
00742 void
00743 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
00744                                                   LLVMPassManagerRef PM) {
00745   PassManagerBuilder *Builder = unwrap(PMB);
00746   legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
00747   Builder->populateFunctionPassManager(*FPM);
00748 }
00749 
00750 void
00751 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
00752                                                 LLVMPassManagerRef PM) {
00753   PassManagerBuilder *Builder = unwrap(PMB);
00754   legacy::PassManagerBase *MPM = unwrap(PM);
00755   Builder->populateModulePassManager(*MPM);
00756 }
00757 
00758 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
00759                                                   LLVMPassManagerRef PM,
00760                                                   LLVMBool Internalize,
00761                                                   LLVMBool RunInliner) {
00762   PassManagerBuilder *Builder = unwrap(PMB);
00763   legacy::PassManagerBase *LPM = unwrap(PM);
00764 
00765   // A small backwards compatibility hack. populateLTOPassManager used to take
00766   // an RunInliner option.
00767   if (RunInliner && !Builder->Inliner)
00768     Builder->Inliner = createFunctionInliningPass();
00769 
00770   Builder->populateLTOPassManager(*LPM);
00771 }