PassManagerBuilder.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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#include "llvm-c/Transforms/PassManagerBuilder.h" 18#include "llvm/ADT/SmallVector.h" 19#include "llvm/Analysis/Passes.h" 20#include "llvm/IR/Verifier.h" 21#include "llvm/PassManager.h" 22#include "llvm/Support/CommandLine.h" 23#include "llvm/Support/ManagedStatic.h" 24#include "llvm/Target/TargetLibraryInfo.h" 25#include "llvm/Transforms/IPO.h" 26#include "llvm/Transforms/Scalar.h" 27#include "llvm/Transforms/Vectorize.h" 28 29using namespace llvm; 30 31static cl::opt<bool> 32RunLoopVectorization("vectorize-loops", cl::Hidden, 33 cl::desc("Run the Loop vectorization passes")); 34 35static cl::opt<bool> 36RunSLPVectorization("vectorize-slp", cl::Hidden, 37 cl::desc("Run the SLP vectorization passes")); 38 39static cl::opt<bool> 40RunBBVectorization("vectorize-slp-aggressive", cl::Hidden, 41 cl::desc("Run the BB vectorization passes")); 42 43static cl::opt<bool> 44UseGVNAfterVectorization("use-gvn-after-vectorization", 45 cl::init(false), cl::Hidden, 46 cl::desc("Run GVN instead of Early CSE after vectorization passes")); 47 48static cl::opt<bool> UseNewSROA("use-new-sroa", 49 cl::init(true), cl::Hidden, 50 cl::desc("Enable the new, experimental SROA pass")); 51 52static cl::opt<bool> 53RunLoopRerolling("reroll-loops", cl::Hidden, 54 cl::desc("Run the loop rerolling pass")); 55 56PassManagerBuilder::PassManagerBuilder() { 57 OptLevel = 2; 58 SizeLevel = 0; 59 LibraryInfo = 0; 60 Inliner = 0; 61 DisableUnitAtATime = false; 62 DisableUnrollLoops = false; 63 BBVectorize = RunBBVectorization; 64 SLPVectorize = RunSLPVectorization; 65 LoopVectorize = RunLoopVectorization; 66 RerollLoops = RunLoopRerolling; 67} 68 69PassManagerBuilder::~PassManagerBuilder() { 70 delete LibraryInfo; 71 delete Inliner; 72} 73 74/// Set of global extensions, automatically added as part of the standard set. 75static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 76 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 77 78void PassManagerBuilder::addGlobalExtension( 79 PassManagerBuilder::ExtensionPointTy Ty, 80 PassManagerBuilder::ExtensionFn Fn) { 81 GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 82} 83 84void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 85 Extensions.push_back(std::make_pair(Ty, Fn)); 86} 87 88void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 89 PassManagerBase &PM) const { 90 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 91 if ((*GlobalExtensions)[i].first == ETy) 92 (*GlobalExtensions)[i].second(*this, PM); 93 for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 94 if (Extensions[i].first == ETy) 95 Extensions[i].second(*this, PM); 96} 97 98void 99PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { 100 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 101 // BasicAliasAnalysis wins if they disagree. This is intended to help 102 // support "obvious" type-punning idioms. 103 PM.add(createTypeBasedAliasAnalysisPass()); 104 PM.add(createBasicAliasAnalysisPass()); 105} 106 107void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { 108 addExtensionsToPM(EP_EarlyAsPossible, FPM); 109 110 // Add LibraryInfo if we have some. 111 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); 112 113 if (OptLevel == 0) return; 114 115 addInitialAliasAnalysisPasses(FPM); 116 117 FPM.add(createCFGSimplificationPass()); 118 if (UseNewSROA) 119 FPM.add(createSROAPass()); 120 else 121 FPM.add(createScalarReplAggregatesPass()); 122 FPM.add(createEarlyCSEPass()); 123 FPM.add(createLowerExpectIntrinsicPass()); 124} 125 126void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { 127 // If all optimizations are disabled, just run the always-inline pass. 128 if (OptLevel == 0) { 129 if (Inliner) { 130 MPM.add(Inliner); 131 Inliner = 0; 132 } 133 134 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 135 // pass manager, but we don't want to add extensions into that pass manager. 136 // To prevent this we must insert a no-op module pass to reset the pass 137 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds. 138 if (!GlobalExtensions->empty() || !Extensions.empty()) 139 MPM.add(createBarrierNoopPass()); 140 141 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 142 return; 143 } 144 145 // Add LibraryInfo if we have some. 146 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); 147 148 addInitialAliasAnalysisPasses(MPM); 149 150 if (!DisableUnitAtATime) { 151 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 152 153 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 154 155 MPM.add(createIPSCCPPass()); // IP SCCP 156 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 157 158 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 159 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 160 } 161 162 // Start of CallGraph SCC passes. 163 if (!DisableUnitAtATime) 164 MPM.add(createPruneEHPass()); // Remove dead EH info 165 if (Inliner) { 166 MPM.add(Inliner); 167 Inliner = 0; 168 } 169 if (!DisableUnitAtATime) 170 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 171 if (OptLevel > 2) 172 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 173 174 // Start of function pass. 175 // Break up aggregate allocas, using SSAUpdater. 176 if (UseNewSROA) 177 MPM.add(createSROAPass(/*RequiresDomTree*/ false)); 178 else 179 MPM.add(createScalarReplAggregatesPass(-1, false)); 180 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 181 MPM.add(createJumpThreadingPass()); // Thread jumps. 182 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 183 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 184 MPM.add(createInstructionCombiningPass()); // Combine silly seq's 185 186 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 187 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 188 MPM.add(createReassociatePass()); // Reassociate expressions 189 MPM.add(createLoopRotatePass()); // Rotate Loop 190 MPM.add(createLICMPass()); // Hoist loop invariants 191 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 192 MPM.add(createInstructionCombiningPass()); 193 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 194 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 195 MPM.add(createLoopDeletionPass()); // Delete dead loops 196 197 if (!DisableUnrollLoops) 198 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops 199 addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 200 201 if (OptLevel > 1) 202 MPM.add(createGVNPass()); // Remove redundancies 203 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 204 MPM.add(createSCCPPass()); // Constant prop with SCCP 205 206 // Run instcombine after redundancy elimination to exploit opportunities 207 // opened up by them. 208 MPM.add(createInstructionCombiningPass()); 209 MPM.add(createJumpThreadingPass()); // Thread jumps 210 MPM.add(createCorrelatedValuePropagationPass()); 211 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 212 213 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 214 215 if (RerollLoops) 216 MPM.add(createLoopRerollPass()); 217 if (SLPVectorize) 218 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 219 220 if (BBVectorize) { 221 MPM.add(createBBVectorizePass()); 222 MPM.add(createInstructionCombiningPass()); 223 if (OptLevel > 1 && UseGVNAfterVectorization) 224 MPM.add(createGVNPass()); // Remove redundancies 225 else 226 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 227 228 // BBVectorize may have significantly shortened a loop body; unroll again. 229 if (!DisableUnrollLoops) 230 MPM.add(createLoopUnrollPass()); 231 } 232 233 MPM.add(createAggressiveDCEPass()); // Delete dead instructions 234 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 235 MPM.add(createInstructionCombiningPass()); // Clean up after everything. 236 237 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 238 // pass manager that we are specifically trying to avoid. To prevent this 239 // we must insert a no-op module pass to reset the pass manager. 240 MPM.add(createBarrierNoopPass()); 241 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize)); 242 // FIXME: Because of #pragma vectorize enable, the passes below are always 243 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when 244 // on -O1 and no #pragma is found). Would be good to have these two passes 245 // as function calls, so that we can only pass them when the vectorizer 246 // changed the code. 247 MPM.add(createInstructionCombiningPass()); 248 MPM.add(createCFGSimplificationPass()); 249 250 if (!DisableUnrollLoops) 251 MPM.add(createLoopUnrollPass()); // Unroll small loops 252 253 if (!DisableUnitAtATime) { 254 // FIXME: We shouldn't bother with this anymore. 255 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 256 257 // GlobalOpt already deletes dead functions and globals, at -O2 try a 258 // late pass of GlobalDCE. It is capable of deleting dead cycles. 259 if (OptLevel > 1) { 260 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 261 MPM.add(createConstantMergePass()); // Merge dup global constants 262 } 263 } 264 addExtensionsToPM(EP_OptimizerLast, MPM); 265} 266 267void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, 268 bool Internalize, 269 bool RunInliner, 270 bool DisableGVNLoadPRE) { 271 // Provide AliasAnalysis services for optimizations. 272 addInitialAliasAnalysisPasses(PM); 273 274 // Now that composite has been compiled, scan through the module, looking 275 // for a main function. If main is defined, mark all other functions 276 // internal. 277 if (Internalize) 278 PM.add(createInternalizePass("main")); 279 280 // Propagate constants at call sites into the functions they call. This 281 // opens opportunities for globalopt (and inlining) by substituting function 282 // pointers passed as arguments to direct uses of functions. 283 PM.add(createIPSCCPPass()); 284 285 // Now that we internalized some globals, see if we can hack on them! 286 PM.add(createGlobalOptimizerPass()); 287 288 // Linking modules together can lead to duplicated global constants, only 289 // keep one copy of each constant. 290 PM.add(createConstantMergePass()); 291 292 // Remove unused arguments from functions. 293 PM.add(createDeadArgEliminationPass()); 294 295 // Reduce the code after globalopt and ipsccp. Both can open up significant 296 // simplification opportunities, and both can propagate functions through 297 // function pointers. When this happens, we often have to resolve varargs 298 // calls, etc, so let instcombine do this. 299 PM.add(createInstructionCombiningPass()); 300 301 // Inline small functions 302 if (RunInliner) 303 PM.add(createFunctionInliningPass()); 304 305 PM.add(createPruneEHPass()); // Remove dead EH info. 306 307 // Optimize globals again if we ran the inliner. 308 if (RunInliner) 309 PM.add(createGlobalOptimizerPass()); 310 PM.add(createGlobalDCEPass()); // Remove dead functions. 311 312 // If we didn't decide to inline a function, check to see if we can 313 // transform it to pass arguments by value instead of by reference. 314 PM.add(createArgumentPromotionPass()); 315 316 // The IPO passes may leave cruft around. Clean up after them. 317 PM.add(createInstructionCombiningPass()); 318 PM.add(createJumpThreadingPass()); 319 320 // Break up allocas 321 if (UseNewSROA) 322 PM.add(createSROAPass()); 323 else 324 PM.add(createScalarReplAggregatesPass()); 325 326 // Run a few AA driven optimizations here and now, to cleanup the code. 327 PM.add(createFunctionAttrsPass()); // Add nocapture. 328 PM.add(createGlobalsModRefPass()); // IP alias analysis. 329 330 PM.add(createLICMPass()); // Hoist loop invariants. 331 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 332 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 333 334 // Nuke dead stores. 335 PM.add(createDeadStoreEliminationPass()); 336 337 // More loops are countable try to vectorize them. 338 PM.add(createLoopVectorizePass(true, true)); 339 340 // Cleanup and simplify the code after the scalar optimizations. 341 PM.add(createInstructionCombiningPass()); 342 343 PM.add(createJumpThreadingPass()); 344 345 // Delete basic blocks, which optimization passes may have killed. 346 PM.add(createCFGSimplificationPass()); 347 348 // Now that we have optimized the program, discard unreachable functions. 349 PM.add(createGlobalDCEPass()); 350} 351 352inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { 353 return reinterpret_cast<PassManagerBuilder*>(P); 354} 355 356inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { 357 return reinterpret_cast<LLVMPassManagerBuilderRef>(P); 358} 359 360LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { 361 PassManagerBuilder *PMB = new PassManagerBuilder(); 362 return wrap(PMB); 363} 364 365void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 366 PassManagerBuilder *Builder = unwrap(PMB); 367 delete Builder; 368} 369 370void 371LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 372 unsigned OptLevel) { 373 PassManagerBuilder *Builder = unwrap(PMB); 374 Builder->OptLevel = OptLevel; 375} 376 377void 378LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 379 unsigned SizeLevel) { 380 PassManagerBuilder *Builder = unwrap(PMB); 381 Builder->SizeLevel = SizeLevel; 382} 383 384void 385LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 386 LLVMBool Value) { 387 PassManagerBuilder *Builder = unwrap(PMB); 388 Builder->DisableUnitAtATime = Value; 389} 390 391void 392LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 393 LLVMBool Value) { 394 PassManagerBuilder *Builder = unwrap(PMB); 395 Builder->DisableUnrollLoops = Value; 396} 397 398void 399LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 400 LLVMBool Value) { 401 // NOTE: The simplify-libcalls pass has been removed. 402} 403 404void 405LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 406 unsigned Threshold) { 407 PassManagerBuilder *Builder = unwrap(PMB); 408 Builder->Inliner = createFunctionInliningPass(Threshold); 409} 410 411void 412LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 413 LLVMPassManagerRef PM) { 414 PassManagerBuilder *Builder = unwrap(PMB); 415 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); 416 Builder->populateFunctionPassManager(*FPM); 417} 418 419void 420LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 421 LLVMPassManagerRef PM) { 422 PassManagerBuilder *Builder = unwrap(PMB); 423 PassManagerBase *MPM = unwrap(PM); 424 Builder->populateModulePassManager(*MPM); 425} 426 427void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 428 LLVMPassManagerRef PM, 429 LLVMBool Internalize, 430 LLVMBool RunInliner) { 431 PassManagerBuilder *Builder = unwrap(PMB); 432 PassManagerBase *LPM = unwrap(PM); 433 Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0); 434} 435