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