LLVM API Documentation

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/Analysis/Verifier.h"
00021 #include "llvm/PassManager.h"
00022 #include "llvm/Support/CommandLine.h"
00023 #include "llvm/Support/ManagedStatic.h"
00024 #include "llvm/Target/TargetLibraryInfo.h"
00025 #include "llvm/Transforms/IPO.h"
00026 #include "llvm/Transforms/Scalar.h"
00027 #include "llvm/Transforms/Vectorize.h"
00028 
00029 using namespace llvm;
00030 
00031 static cl::opt<bool>
00032 RunLoopVectorization("vectorize-loops",
00033                      cl::desc("Run the Loop vectorization passes"));
00034 
00035 // This is a helper flag that we use for testing the profitability of
00036 // vectorization on -O2 and -Os. It should go away once we make a decision.
00037 static cl::opt<bool>
00038 VectorizeO2("vectorize-o2",
00039             cl::desc("Enable vectorization on all O levels"));
00040 
00041 static cl::opt<bool>
00042 RunSLPVectorization("vectorize-slp",
00043                     cl::desc("Run the SLP vectorization passes"));
00044 
00045 static cl::opt<bool>
00046 RunBBVectorization("vectorize-slp-aggressive",
00047                     cl::desc("Run the BB vectorization passes"));
00048 
00049 static cl::opt<bool>
00050 UseGVNAfterVectorization("use-gvn-after-vectorization",
00051   cl::init(false), cl::Hidden,
00052   cl::desc("Run GVN instead of Early CSE after vectorization passes"));
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 PassManagerBuilder::PassManagerBuilder() {
00059     OptLevel = 2;
00060     SizeLevel = 0;
00061     LibraryInfo = 0;
00062     Inliner = 0;
00063     DisableSimplifyLibCalls = false;
00064     DisableUnitAtATime = false;
00065     DisableUnrollLoops = false;
00066     BBVectorize = RunBBVectorization;
00067     SLPVectorize = RunSLPVectorization;
00068     LoopVectorize = RunLoopVectorization;
00069 }
00070 
00071 PassManagerBuilder::~PassManagerBuilder() {
00072   delete LibraryInfo;
00073   delete Inliner;
00074 }
00075 
00076 /// Set of global extensions, automatically added as part of the standard set.
00077 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
00078    PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
00079 
00080 void PassManagerBuilder::addGlobalExtension(
00081     PassManagerBuilder::ExtensionPointTy Ty,
00082     PassManagerBuilder::ExtensionFn Fn) {
00083   GlobalExtensions->push_back(std::make_pair(Ty, Fn));
00084 }
00085 
00086 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
00087   Extensions.push_back(std::make_pair(Ty, Fn));
00088 }
00089 
00090 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
00091                                            PassManagerBase &PM) const {
00092   for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
00093     if ((*GlobalExtensions)[i].first == ETy)
00094       (*GlobalExtensions)[i].second(*this, PM);
00095   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
00096     if (Extensions[i].first == ETy)
00097       Extensions[i].second(*this, PM);
00098 }
00099 
00100 void
00101 PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
00102   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
00103   // BasicAliasAnalysis wins if they disagree. This is intended to help
00104   // support "obvious" type-punning idioms.
00105   PM.add(createTypeBasedAliasAnalysisPass());
00106   PM.add(createBasicAliasAnalysisPass());
00107 }
00108 
00109 void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
00110   addExtensionsToPM(EP_EarlyAsPossible, FPM);
00111 
00112   // Add LibraryInfo if we have some.
00113   if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
00114 
00115   if (OptLevel == 0) return;
00116 
00117   addInitialAliasAnalysisPasses(FPM);
00118 
00119   FPM.add(createCFGSimplificationPass());
00120   if (UseNewSROA)
00121     FPM.add(createSROAPass());
00122   else
00123     FPM.add(createScalarReplAggregatesPass());
00124   FPM.add(createEarlyCSEPass());
00125   FPM.add(createLowerExpectIntrinsicPass());
00126 }
00127 
00128 void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
00129   // If all optimizations are disabled, just run the always-inline pass.
00130   if (OptLevel == 0) {
00131     if (Inliner) {
00132       MPM.add(Inliner);
00133       Inliner = 0;
00134     }
00135 
00136     // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
00137     // pass manager, but we don't want to add extensions into that pass manager.
00138     // To prevent this we must insert a no-op module pass to reset the pass
00139     // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
00140     if (!GlobalExtensions->empty() || !Extensions.empty())
00141       MPM.add(createBarrierNoopPass());
00142 
00143     addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
00144     return;
00145   }
00146 
00147   // Add LibraryInfo if we have some.
00148   if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
00149 
00150   addInitialAliasAnalysisPasses(MPM);
00151 
00152   if (!DisableUnitAtATime) {
00153     addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
00154 
00155     MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
00156 
00157     MPM.add(createIPSCCPPass());              // IP SCCP
00158     MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
00159 
00160     MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
00161     MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
00162   }
00163 
00164   // Start of CallGraph SCC passes.
00165   if (!DisableUnitAtATime)
00166     MPM.add(createPruneEHPass());             // Remove dead EH info
00167   if (Inliner) {
00168     MPM.add(Inliner);
00169     Inliner = 0;
00170   }
00171   if (!DisableUnitAtATime)
00172     MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
00173   if (OptLevel > 2)
00174     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
00175 
00176   // Start of function pass.
00177   // Break up aggregate allocas, using SSAUpdater.
00178   if (UseNewSROA)
00179     MPM.add(createSROAPass(/*RequiresDomTree*/ false));
00180   else
00181     MPM.add(createScalarReplAggregatesPass(-1, false));
00182   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
00183   if (!DisableSimplifyLibCalls)
00184     MPM.add(createSimplifyLibCallsPass());    // Library Call Optimizations
00185   MPM.add(createJumpThreadingPass());         // Thread jumps.
00186   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
00187   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00188   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
00189 
00190   MPM.add(createTailCallEliminationPass());   // Eliminate tail calls
00191   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00192   MPM.add(createReassociatePass());           // Reassociate expressions
00193   MPM.add(createLoopRotatePass());            // Rotate Loop
00194   MPM.add(createLICMPass());                  // Hoist loop invariants
00195   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
00196   MPM.add(createInstructionCombiningPass());
00197   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
00198   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
00199   MPM.add(createLoopDeletionPass());          // Delete dead loops
00200 
00201   if (LoopVectorize && (OptLevel > 2 || VectorizeO2))
00202     MPM.add(createLoopVectorizePass());
00203 
00204   if (!DisableUnrollLoops)
00205     MPM.add(createLoopUnrollPass());          // Unroll small loops
00206   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
00207 
00208   if (OptLevel > 1)
00209     MPM.add(createGVNPass());                 // Remove redundancies
00210   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
00211   MPM.add(createSCCPPass());                  // Constant prop with SCCP
00212 
00213   // Run instcombine after redundancy elimination to exploit opportunities
00214   // opened up by them.
00215   MPM.add(createInstructionCombiningPass());
00216   MPM.add(createJumpThreadingPass());         // Thread jumps
00217   MPM.add(createCorrelatedValuePropagationPass());
00218   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
00219 
00220   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
00221 
00222   if (SLPVectorize)
00223     MPM.add(createSLPVectorizerPass());     // Vectorize parallel scalar chains.
00224 
00225   if (BBVectorize) {
00226     MPM.add(createBBVectorizePass());
00227     MPM.add(createInstructionCombiningPass());
00228     if (OptLevel > 1 && UseGVNAfterVectorization)
00229       MPM.add(createGVNPass());                   // Remove redundancies
00230     else
00231       MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
00232 
00233     // BBVectorize may have significantly shortened a loop body; unroll again.
00234     if (!DisableUnrollLoops)
00235       MPM.add(createLoopUnrollPass());
00236   }
00237 
00238   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
00239   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
00240   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
00241 
00242   if (!DisableUnitAtATime) {
00243     // FIXME: We shouldn't bother with this anymore.
00244     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
00245 
00246     // GlobalOpt already deletes dead functions and globals, at -O2 try a
00247     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
00248     if (OptLevel > 1) {
00249       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
00250       MPM.add(createConstantMergePass());     // Merge dup global constants
00251     }
00252   }
00253   addExtensionsToPM(EP_OptimizerLast, MPM);
00254 }
00255 
00256 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
00257                                                 bool Internalize,
00258                                                 bool RunInliner,
00259                                                 bool DisableGVNLoadPRE) {
00260   // Provide AliasAnalysis services for optimizations.
00261   addInitialAliasAnalysisPasses(PM);
00262 
00263   // Now that composite has been compiled, scan through the module, looking
00264   // for a main function.  If main is defined, mark all other functions
00265   // internal.
00266   if (Internalize) {
00267     std::vector<const char*> E;
00268     E.push_back("main");
00269     PM.add(createInternalizePass(E));
00270   }
00271 
00272   // Propagate constants at call sites into the functions they call.  This
00273   // opens opportunities for globalopt (and inlining) by substituting function
00274   // pointers passed as arguments to direct uses of functions.
00275   PM.add(createIPSCCPPass());
00276 
00277   // Now that we internalized some globals, see if we can hack on them!
00278   PM.add(createGlobalOptimizerPass());
00279 
00280   // Linking modules together can lead to duplicated global constants, only
00281   // keep one copy of each constant.
00282   PM.add(createConstantMergePass());
00283 
00284   // Remove unused arguments from functions.
00285   PM.add(createDeadArgEliminationPass());
00286 
00287   // Reduce the code after globalopt and ipsccp.  Both can open up significant
00288   // simplification opportunities, and both can propagate functions through
00289   // function pointers.  When this happens, we often have to resolve varargs
00290   // calls, etc, so let instcombine do this.
00291   PM.add(createInstructionCombiningPass());
00292 
00293   // Inline small functions
00294   if (RunInliner)
00295     PM.add(createFunctionInliningPass());
00296 
00297   PM.add(createPruneEHPass());   // Remove dead EH info.
00298 
00299   // Optimize globals again if we ran the inliner.
00300   if (RunInliner)
00301     PM.add(createGlobalOptimizerPass());
00302   PM.add(createGlobalDCEPass()); // Remove dead functions.
00303 
00304   // If we didn't decide to inline a function, check to see if we can
00305   // transform it to pass arguments by value instead of by reference.
00306   PM.add(createArgumentPromotionPass());
00307 
00308   // The IPO passes may leave cruft around.  Clean up after them.
00309   PM.add(createInstructionCombiningPass());
00310   PM.add(createJumpThreadingPass());
00311   // Break up allocas
00312   if (UseNewSROA)
00313     PM.add(createSROAPass());
00314   else
00315     PM.add(createScalarReplAggregatesPass());
00316 
00317   // Run a few AA driven optimizations here and now, to cleanup the code.
00318   PM.add(createFunctionAttrsPass()); // Add nocapture.
00319   PM.add(createGlobalsModRefPass()); // IP alias analysis.
00320 
00321   PM.add(createLICMPass());                 // Hoist loop invariants.
00322   PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
00323   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
00324   // Nuke dead stores.
00325   PM.add(createDeadStoreEliminationPass());
00326 
00327   // Cleanup and simplify the code after the scalar optimizations.
00328   PM.add(createInstructionCombiningPass());
00329 
00330   PM.add(createJumpThreadingPass());
00331 
00332   // Delete basic blocks, which optimization passes may have killed.
00333   PM.add(createCFGSimplificationPass());
00334 
00335   // Now that we have optimized the program, discard unreachable functions.
00336   PM.add(createGlobalDCEPass());
00337 }
00338 
00339 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
00340     return reinterpret_cast<PassManagerBuilder*>(P);
00341 }
00342 
00343 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
00344   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
00345 }
00346 
00347 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
00348   PassManagerBuilder *PMB = new PassManagerBuilder();
00349   return wrap(PMB);
00350 }
00351 
00352 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
00353   PassManagerBuilder *Builder = unwrap(PMB);
00354   delete Builder;
00355 }
00356 
00357 void
00358 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
00359                                   unsigned OptLevel) {
00360   PassManagerBuilder *Builder = unwrap(PMB);
00361   Builder->OptLevel = OptLevel;
00362 }
00363 
00364 void
00365 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
00366                                    unsigned SizeLevel) {
00367   PassManagerBuilder *Builder = unwrap(PMB);
00368   Builder->SizeLevel = SizeLevel;
00369 }
00370 
00371 void
00372 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
00373                                             LLVMBool Value) {
00374   PassManagerBuilder *Builder = unwrap(PMB);
00375   Builder->DisableUnitAtATime = Value;
00376 }
00377 
00378 void
00379 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
00380                                             LLVMBool Value) {
00381   PassManagerBuilder *Builder = unwrap(PMB);
00382   Builder->DisableUnrollLoops = Value;
00383 }
00384 
00385 void
00386 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
00387                                                  LLVMBool Value) {
00388   PassManagerBuilder *Builder = unwrap(PMB);
00389   Builder->DisableSimplifyLibCalls = Value;
00390 }
00391 
00392 void
00393 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
00394                                               unsigned Threshold) {
00395   PassManagerBuilder *Builder = unwrap(PMB);
00396   Builder->Inliner = createFunctionInliningPass(Threshold);
00397 }
00398 
00399 void
00400 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
00401                                                   LLVMPassManagerRef PM) {
00402   PassManagerBuilder *Builder = unwrap(PMB);
00403   FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
00404   Builder->populateFunctionPassManager(*FPM);
00405 }
00406 
00407 void
00408 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
00409                                                 LLVMPassManagerRef PM) {
00410   PassManagerBuilder *Builder = unwrap(PMB);
00411   PassManagerBase *MPM = unwrap(PM);
00412   Builder->populateModulePassManager(*MPM);
00413 }
00414 
00415 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
00416                                                   LLVMPassManagerRef PM,
00417                                                   LLVMBool Internalize,
00418                                                   LLVMBool RunInliner) {
00419   PassManagerBuilder *Builder = unwrap(PMB);
00420   PassManagerBase *LPM = unwrap(PM);
00421   Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0);
00422 }