PassManagerBuilder.cpp revision 7d4c87ef6eea424b7a28392ea11137ed77b44b57
1//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the PassManagerBuilder class, which is used to set up a 11// "standard" optimization sequence suitable for languages like C and C++. 12// 13//===----------------------------------------------------------------------===// 14 15 16#include "llvm/Transforms/IPO/PassManagerBuilder.h" 17 18#include "llvm-c/Transforms/PassManagerBuilder.h" 19 20#include "llvm/PassManager.h" 21#include "llvm/DefaultPasses.h" 22#include "llvm/PassManager.h" 23#include "llvm/Analysis/Passes.h" 24#include "llvm/Target/TargetLibraryInfo.h" 25#include "llvm/Transforms/Scalar.h" 26#include "llvm/Transforms/IPO.h" 27#include "llvm/ADT/SmallVector.h" 28#include "llvm/Support/ManagedStatic.h" 29 30using namespace llvm; 31 32PassManagerBuilder::PassManagerBuilder() { 33 OptLevel = 2; 34 SizeLevel = 0; 35 LibraryInfo = 0; 36 Inliner = 0; 37 DisableSimplifyLibCalls = false; 38 DisableUnitAtATime = false; 39 DisableUnrollLoops = false; 40} 41 42PassManagerBuilder::~PassManagerBuilder() { 43 delete LibraryInfo; 44 delete Inliner; 45} 46 47/// Set of global extensions, automatically added as part of the standard set. 48static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 49 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 50 51void PassManagerBuilder::addGlobalExtension( 52 PassManagerBuilder::ExtensionPointTy Ty, 53 PassManagerBuilder::ExtensionFn Fn) { 54 GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 55} 56 57void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 58 Extensions.push_back(std::make_pair(Ty, Fn)); 59} 60 61void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 62 PassManagerBase &PM) const { 63 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 64 if ((*GlobalExtensions)[i].first == ETy) 65 (*GlobalExtensions)[i].second(*this, PM); 66 for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 67 if (Extensions[i].first == ETy) 68 Extensions[i].second(*this, PM); 69} 70 71void 72PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { 73 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 74 // BasicAliasAnalysis wins if they disagree. This is intended to help 75 // support "obvious" type-punning idioms. 76 PM.add(createTypeBasedAliasAnalysisPass()); 77 PM.add(createBasicAliasAnalysisPass()); 78} 79 80void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { 81 addExtensionsToPM(EP_EarlyAsPossible, FPM); 82 83 // Add LibraryInfo if we have some. 84 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); 85 86 if (OptLevel == 0) return; 87 88 addInitialAliasAnalysisPasses(FPM); 89 90 FPM.add(createCFGSimplificationPass()); 91 FPM.add(createScalarReplAggregatesPass()); 92 FPM.add(createEarlyCSEPass()); 93 FPM.add(createLowerExpectIntrinsicPass()); 94} 95 96void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { 97 // If all optimizations are disabled, just run the always-inline pass. 98 if (OptLevel == 0) { 99 if (Inliner) { 100 MPM.add(Inliner); 101 Inliner = 0; 102 } 103 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 104 return; 105 } 106 107 // Add LibraryInfo if we have some. 108 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); 109 110 addInitialAliasAnalysisPasses(MPM); 111 112 if (!DisableUnitAtATime) { 113 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 114 115 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 116 117 MPM.add(createIPSCCPPass()); // IP SCCP 118 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 119 120 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 121 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 122 } 123 124 // Start of CallGraph SCC passes. 125 if (!DisableUnitAtATime) 126 MPM.add(createPruneEHPass()); // Remove dead EH info 127 if (Inliner) { 128 MPM.add(Inliner); 129 Inliner = 0; 130 } 131 if (!DisableUnitAtATime) 132 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 133 if (OptLevel > 2) 134 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 135 136 // Start of function pass. 137 // Break up aggregate allocas, using SSAUpdater. 138 MPM.add(createScalarReplAggregatesPass(-1, false)); 139 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 140 if (!DisableSimplifyLibCalls) 141 MPM.add(createSimplifyLibCallsPass()); // Library Call Optimizations 142 MPM.add(createJumpThreadingPass()); // Thread jumps. 143 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 144 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 145 MPM.add(createInstructionCombiningPass()); // Combine silly seq's 146 147 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 148 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 149 MPM.add(createReassociatePass()); // Reassociate expressions 150 MPM.add(createLoopRotatePass()); // Rotate Loop 151 MPM.add(createLICMPass()); // Hoist loop invariants 152 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 153 MPM.add(createInstructionCombiningPass()); 154 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 155 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 156 MPM.add(createLoopDeletionPass()); // Delete dead loops 157 if (!DisableUnrollLoops) 158 MPM.add(createLoopUnrollPass()); // Unroll small loops 159 addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 160 161 if (OptLevel > 1) 162 MPM.add(createGVNPass()); // Remove redundancies 163 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 164 MPM.add(createSCCPPass()); // Constant prop with SCCP 165 166 // Run instcombine after redundancy elimination to exploit opportunities 167 // opened up by them. 168 MPM.add(createInstructionCombiningPass()); 169 MPM.add(createJumpThreadingPass()); // Thread jumps 170 MPM.add(createCorrelatedValuePropagationPass()); 171 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 172 173 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 174 175 MPM.add(createAggressiveDCEPass()); // Delete dead instructions 176 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 177 MPM.add(createInstructionCombiningPass()); // Clean up after everything. 178 179 if (!DisableUnitAtATime) { 180 // FIXME: We shouldn't bother with this anymore. 181 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 182 183 // GlobalOpt already deletes dead functions and globals, at -O3 try a 184 // late pass of GlobalDCE. It is capable of deleting dead cycles. 185 if (OptLevel > 2) 186 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 187 188 if (OptLevel > 1) 189 MPM.add(createConstantMergePass()); // Merge dup global constants 190 } 191} 192 193void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, 194 bool Internalize, 195 bool RunInliner) { 196 // Provide AliasAnalysis services for optimizations. 197 addInitialAliasAnalysisPasses(PM); 198 199 // Now that composite has been compiled, scan through the module, looking 200 // for a main function. If main is defined, mark all other functions 201 // internal. 202 if (Internalize) 203 PM.add(createInternalizePass(true)); 204 205 // Propagate constants at call sites into the functions they call. This 206 // opens opportunities for globalopt (and inlining) by substituting function 207 // pointers passed as arguments to direct uses of functions. 208 PM.add(createIPSCCPPass()); 209 210 // Now that we internalized some globals, see if we can hack on them! 211 PM.add(createGlobalOptimizerPass()); 212 213 // Linking modules together can lead to duplicated global constants, only 214 // keep one copy of each constant. 215 PM.add(createConstantMergePass()); 216 217 // Remove unused arguments from functions. 218 PM.add(createDeadArgEliminationPass()); 219 220 // Reduce the code after globalopt and ipsccp. Both can open up significant 221 // simplification opportunities, and both can propagate functions through 222 // function pointers. When this happens, we often have to resolve varargs 223 // calls, etc, so let instcombine do this. 224 PM.add(createInstructionCombiningPass()); 225 226 // Inline small functions 227 if (RunInliner) 228 PM.add(createFunctionInliningPass()); 229 230 PM.add(createPruneEHPass()); // Remove dead EH info. 231 232 // Optimize globals again if we ran the inliner. 233 if (RunInliner) 234 PM.add(createGlobalOptimizerPass()); 235 PM.add(createGlobalDCEPass()); // Remove dead functions. 236 237 // If we didn't decide to inline a function, check to see if we can 238 // transform it to pass arguments by value instead of by reference. 239 PM.add(createArgumentPromotionPass()); 240 241 // The IPO passes may leave cruft around. Clean up after them. 242 PM.add(createInstructionCombiningPass()); 243 PM.add(createJumpThreadingPass()); 244 // Break up allocas 245 PM.add(createScalarReplAggregatesPass()); 246 247 // Run a few AA driven optimizations here and now, to cleanup the code. 248 PM.add(createFunctionAttrsPass()); // Add nocapture. 249 PM.add(createGlobalsModRefPass()); // IP alias analysis. 250 251 PM.add(createLICMPass()); // Hoist loop invariants. 252 PM.add(createGVNPass()); // Remove redundancies. 253 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 254 // Nuke dead stores. 255 PM.add(createDeadStoreEliminationPass()); 256 257 // Cleanup and simplify the code after the scalar optimizations. 258 PM.add(createInstructionCombiningPass()); 259 260 PM.add(createJumpThreadingPass()); 261 262 // Delete basic blocks, which optimization passes may have killed. 263 PM.add(createCFGSimplificationPass()); 264 265 // Now that we have optimized the program, discard unreachable functions. 266 PM.add(createGlobalDCEPass()); 267} 268 269LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate(void) { 270 PassManagerBuilder *PMB = new PassManagerBuilder(); 271 return wrap(PMB); 272} 273 274void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 275 PassManagerBuilder *Builder = unwrap(PMB); 276 delete Builder; 277} 278 279void 280LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 281 unsigned OptLevel) { 282 PassManagerBuilder *Builder = unwrap(PMB); 283 Builder->OptLevel = OptLevel; 284} 285 286void 287LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 288 unsigned SizeLevel) { 289 PassManagerBuilder *Builder = unwrap(PMB); 290 Builder->SizeLevel = SizeLevel; 291} 292 293void 294LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 295 LLVMBool Value) { 296 PassManagerBuilder *Builder = unwrap(PMB); 297 Builder->DisableUnitAtATime = Value; 298} 299 300void 301LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 302 LLVMBool Value) { 303 PassManagerBuilder *Builder = unwrap(PMB); 304 Builder->DisableUnrollLoops = Value; 305} 306 307void 308LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 309 LLVMBool Value) { 310 PassManagerBuilder *Builder = unwrap(PMB); 311 Builder->DisableSimplifyLibCalls = Value; 312} 313 314void 315LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 316 unsigned Threshold) { 317 PassManagerBuilder *Builder = unwrap(PMB); 318 Builder->Inliner = createFunctionInliningPass(Threshold); 319} 320 321void 322LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 323 LLVMPassManagerRef PM) { 324 PassManagerBuilder *Builder = unwrap(PMB); 325 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); 326 Builder->populateFunctionPassManager(*FPM); 327} 328 329void 330LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 331 LLVMPassManagerRef PM) { 332 PassManagerBuilder *Builder = unwrap(PMB); 333 PassManagerBase *MPM = unwrap(PM); 334 Builder->populateModulePassManager(*MPM); 335} 336 337void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 338 LLVMPassManagerRef PM, 339 bool Internalize, 340 bool RunInliner) { 341 PassManagerBuilder *Builder = unwrap(PMB); 342 PassManagerBase *LPM = unwrap(PM); 343 Builder->populateLTOPassManager(*LPM, Internalize, RunInliner); 344} 345