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