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/DataLayout.h" 21 #include "llvm/IR/Verifier.h" 22 #include "llvm/IR/LegacyPassManager.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/ManagedStatic.h" 25 #include "llvm/Analysis/TargetLibraryInfo.h" 26 #include "llvm/Target/TargetMachine.h" 27 #include "llvm/Transforms/IPO.h" 28 #include "llvm/Transforms/Scalar.h" 29 #include "llvm/Transforms/Vectorize.h" 30 31 using namespace llvm; 32 33 static cl::opt<bool> 34 RunLoopVectorization("vectorize-loops", cl::Hidden, 35 cl::desc("Run the Loop vectorization passes")); 36 37 static cl::opt<bool> 38 RunSLPVectorization("vectorize-slp", cl::Hidden, 39 cl::desc("Run the SLP vectorization passes")); 40 41 static cl::opt<bool> 42 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden, 43 cl::desc("Run the BB vectorization passes")); 44 45 static cl::opt<bool> 46 UseGVNAfterVectorization("use-gvn-after-vectorization", 47 cl::init(false), cl::Hidden, 48 cl::desc("Run GVN instead of Early CSE after vectorization passes")); 49 50 static cl::opt<bool> ExtraVectorizerPasses( 51 "extra-vectorizer-passes", cl::init(false), cl::Hidden, 52 cl::desc("Run cleanup optimization passes after vectorization.")); 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 static cl::opt<bool> 59 RunLoopRerolling("reroll-loops", cl::Hidden, 60 cl::desc("Run the loop rerolling pass")); 61 62 static cl::opt<bool> 63 RunFloat2Int("float-to-int", cl::Hidden, cl::init(true), 64 cl::desc("Run the float2int (float demotion) pass")); 65 66 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false), 67 cl::Hidden, 68 cl::desc("Run the load combining pass")); 69 70 static cl::opt<bool> 71 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization", 72 cl::init(true), cl::Hidden, 73 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop " 74 "vectorizer instead of before")); 75 76 static cl::opt<bool> UseCFLAA("use-cfl-aa", 77 cl::init(false), cl::Hidden, 78 cl::desc("Enable the new, experimental CFL alias analysis")); 79 80 static cl::opt<bool> 81 EnableMLSM("mlsm", cl::init(true), cl::Hidden, 82 cl::desc("Enable motion of merged load and store")); 83 84 static cl::opt<bool> EnableLoopInterchange( 85 "enable-loopinterchange", cl::init(false), cl::Hidden, 86 cl::desc("Enable the new, experimental LoopInterchange Pass")); 87 88 static cl::opt<bool> EnableLoopDistribute( 89 "enable-loop-distribute", cl::init(false), cl::Hidden, 90 cl::desc("Enable the new, experimental LoopDistribution Pass")); 91 92 static cl::opt<bool> EnableNonLTOGlobalsModRef( 93 "enable-non-lto-gmr", cl::init(false), cl::Hidden, 94 cl::desc( 95 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline.")); 96 97 PassManagerBuilder::PassManagerBuilder() { 98 OptLevel = 2; 99 SizeLevel = 0; 100 LibraryInfo = nullptr; 101 Inliner = nullptr; 102 DisableUnitAtATime = false; 103 DisableUnrollLoops = false; 104 BBVectorize = RunBBVectorization; 105 SLPVectorize = RunSLPVectorization; 106 LoopVectorize = RunLoopVectorization; 107 RerollLoops = RunLoopRerolling; 108 LoadCombine = RunLoadCombine; 109 DisableGVNLoadPRE = false; 110 VerifyInput = false; 111 VerifyOutput = false; 112 MergeFunctions = false; 113 PrepareForLTO = false; 114 } 115 116 PassManagerBuilder::~PassManagerBuilder() { 117 delete LibraryInfo; 118 delete Inliner; 119 } 120 121 /// Set of global extensions, automatically added as part of the standard set. 122 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, 123 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; 124 125 void PassManagerBuilder::addGlobalExtension( 126 PassManagerBuilder::ExtensionPointTy Ty, 127 PassManagerBuilder::ExtensionFn Fn) { 128 GlobalExtensions->push_back(std::make_pair(Ty, Fn)); 129 } 130 131 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 132 Extensions.push_back(std::make_pair(Ty, Fn)); 133 } 134 135 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 136 legacy::PassManagerBase &PM) const { 137 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) 138 if ((*GlobalExtensions)[i].first == ETy) 139 (*GlobalExtensions)[i].second(*this, PM); 140 for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 141 if (Extensions[i].first == ETy) 142 Extensions[i].second(*this, PM); 143 } 144 145 void PassManagerBuilder::addInitialAliasAnalysisPasses( 146 legacy::PassManagerBase &PM) const { 147 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 148 // BasicAliasAnalysis wins if they disagree. This is intended to help 149 // support "obvious" type-punning idioms. 150 if (UseCFLAA) 151 PM.add(createCFLAliasAnalysisPass()); 152 PM.add(createTypeBasedAliasAnalysisPass()); 153 PM.add(createScopedNoAliasAAPass()); 154 PM.add(createBasicAliasAnalysisPass()); 155 } 156 157 void PassManagerBuilder::populateFunctionPassManager( 158 legacy::FunctionPassManager &FPM) { 159 addExtensionsToPM(EP_EarlyAsPossible, FPM); 160 161 // Add LibraryInfo if we have some. 162 if (LibraryInfo) 163 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 164 165 if (OptLevel == 0) return; 166 167 addInitialAliasAnalysisPasses(FPM); 168 169 FPM.add(createCFGSimplificationPass()); 170 if (UseNewSROA) 171 FPM.add(createSROAPass()); 172 else 173 FPM.add(createScalarReplAggregatesPass()); 174 FPM.add(createEarlyCSEPass()); 175 FPM.add(createLowerExpectIntrinsicPass()); 176 } 177 178 void PassManagerBuilder::populateModulePassManager( 179 legacy::PassManagerBase &MPM) { 180 // If all optimizations are disabled, just run the always-inline pass and, 181 // if enabled, the function merging pass. 182 if (OptLevel == 0) { 183 if (Inliner) { 184 MPM.add(Inliner); 185 Inliner = nullptr; 186 } 187 188 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly 189 // creates a CGSCC pass manager, but we don't want to add extensions into 190 // that pass manager. To prevent this we insert a no-op module pass to reset 191 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0 192 // builds. The function merging pass is 193 if (MergeFunctions) 194 MPM.add(createMergeFunctionsPass()); 195 else if (!GlobalExtensions->empty() || !Extensions.empty()) 196 MPM.add(createBarrierNoopPass()); 197 198 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 199 return; 200 } 201 202 // Add LibraryInfo if we have some. 203 if (LibraryInfo) 204 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 205 206 addInitialAliasAnalysisPasses(MPM); 207 208 if (!DisableUnitAtATime) { 209 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 210 211 MPM.add(createIPSCCPPass()); // IP SCCP 212 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 213 214 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 215 216 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE 217 addExtensionsToPM(EP_Peephole, MPM); 218 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 219 } 220 221 if (EnableNonLTOGlobalsModRef) 222 // We add a module alias analysis pass here. In part due to bugs in the 223 // analysis infrastructure this "works" in that the analysis stays alive 224 // for the entire SCC pass run below. 225 MPM.add(createGlobalsModRefPass()); 226 227 // Start of CallGraph SCC passes. 228 if (!DisableUnitAtATime) 229 MPM.add(createPruneEHPass()); // Remove dead EH info 230 if (Inliner) { 231 MPM.add(Inliner); 232 Inliner = nullptr; 233 } 234 if (!DisableUnitAtATime) 235 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs 236 if (OptLevel > 2) 237 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 238 239 // Start of function pass. 240 // Break up aggregate allocas, using SSAUpdater. 241 if (UseNewSROA) 242 MPM.add(createSROAPass(/*RequiresDomTree*/ false)); 243 else 244 MPM.add(createScalarReplAggregatesPass(-1, false)); 245 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 246 MPM.add(createJumpThreadingPass()); // Thread jumps. 247 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 248 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 249 MPM.add(createInstructionCombiningPass()); // Combine silly seq's 250 addExtensionsToPM(EP_Peephole, MPM); 251 252 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 253 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 254 MPM.add(createReassociatePass()); // Reassociate expressions 255 // Rotate Loop - disable header duplication at -Oz 256 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); 257 MPM.add(createLICMPass()); // Hoist loop invariants 258 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 259 MPM.add(createInstructionCombiningPass()); 260 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 261 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 262 MPM.add(createLoopDeletionPass()); // Delete dead loops 263 if (EnableLoopInterchange) { 264 MPM.add(createLoopInterchangePass()); // Interchange loops 265 MPM.add(createCFGSimplificationPass()); 266 } 267 if (!DisableUnrollLoops) 268 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops 269 addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 270 271 if (OptLevel > 1) { 272 if (EnableMLSM) 273 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds 274 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies 275 } 276 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 277 MPM.add(createSCCPPass()); // Constant prop with SCCP 278 279 // Delete dead bit computations (instcombine runs after to fold away the dead 280 // computations, and then ADCE will run later to exploit any new DCE 281 // opportunities that creates). 282 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations 283 284 // Run instcombine after redundancy elimination to exploit opportunities 285 // opened up by them. 286 MPM.add(createInstructionCombiningPass()); 287 addExtensionsToPM(EP_Peephole, MPM); 288 MPM.add(createJumpThreadingPass()); // Thread jumps 289 MPM.add(createCorrelatedValuePropagationPass()); 290 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 291 MPM.add(createLICMPass()); 292 293 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 294 295 if (RerollLoops) 296 MPM.add(createLoopRerollPass()); 297 if (!RunSLPAfterLoopVectorization) { 298 if (SLPVectorize) 299 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 300 301 if (BBVectorize) { 302 MPM.add(createBBVectorizePass()); 303 MPM.add(createInstructionCombiningPass()); 304 addExtensionsToPM(EP_Peephole, MPM); 305 if (OptLevel > 1 && UseGVNAfterVectorization) 306 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies 307 else 308 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 309 310 // BBVectorize may have significantly shortened a loop body; unroll again. 311 if (!DisableUnrollLoops) 312 MPM.add(createLoopUnrollPass()); 313 } 314 } 315 316 if (LoadCombine) 317 MPM.add(createLoadCombinePass()); 318 319 MPM.add(createAggressiveDCEPass()); // Delete dead instructions 320 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 321 MPM.add(createInstructionCombiningPass()); // Clean up after everything. 322 addExtensionsToPM(EP_Peephole, MPM); 323 324 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 325 // pass manager that we are specifically trying to avoid. To prevent this 326 // we must insert a no-op module pass to reset the pass manager. 327 MPM.add(createBarrierNoopPass()); 328 329 if (EnableNonLTOGlobalsModRef) 330 // We add a fresh GlobalsModRef run at this point. This is particularly 331 // useful as the above will have inlined, DCE'ed, and function-attr 332 // propagated everything. We should at this point have a reasonably minimal 333 // and richly annotated call graph. By computing aliasing and mod/ref 334 // information for all local globals here, the late loop passes and notably 335 // the vectorizer will be able to use them to help recognize vectorizable 336 // memory operations. 337 // 338 // Note that this relies on a bug in the pass manager which preserves 339 // a module analysis into a function pass pipeline (and throughout it) so 340 // long as the first function pass doesn't invalidate the module analysis. 341 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for 342 // this to work. Fortunately, it is trivial to preserve AliasAnalysis 343 // (doing nothing preserves it as it is required to be conservatively 344 // correct in the face of IR changes). 345 MPM.add(createGlobalsModRefPass()); 346 347 if (RunFloat2Int) 348 MPM.add(createFloat2IntPass()); 349 350 addExtensionsToPM(EP_VectorizerStart, MPM); 351 352 // Re-rotate loops in all our loop nests. These may have fallout out of 353 // rotated form due to GVN or other transformations, and the vectorizer relies 354 // on the rotated form. Disable header duplication at -Oz. 355 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); 356 357 // Distribute loops to allow partial vectorization. I.e. isolate dependences 358 // into separate loop that would otherwise inhibit vectorization. 359 if (EnableLoopDistribute) 360 MPM.add(createLoopDistributePass()); 361 362 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize)); 363 // FIXME: Because of #pragma vectorize enable, the passes below are always 364 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when 365 // on -O1 and no #pragma is found). Would be good to have these two passes 366 // as function calls, so that we can only pass them when the vectorizer 367 // changed the code. 368 MPM.add(createInstructionCombiningPass()); 369 if (OptLevel > 1 && ExtraVectorizerPasses) { 370 // At higher optimization levels, try to clean up any runtime overlap and 371 // alignment checks inserted by the vectorizer. We want to track correllated 372 // runtime checks for two inner loops in the same outer loop, fold any 373 // common computations, hoist loop-invariant aspects out of any outer loop, 374 // and unswitch the runtime checks if possible. Once hoisted, we may have 375 // dead (or speculatable) control flows or more combining opportunities. 376 MPM.add(createEarlyCSEPass()); 377 MPM.add(createCorrelatedValuePropagationPass()); 378 MPM.add(createInstructionCombiningPass()); 379 MPM.add(createLICMPass()); 380 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); 381 MPM.add(createCFGSimplificationPass()); 382 MPM.add(createInstructionCombiningPass()); 383 } 384 385 if (RunSLPAfterLoopVectorization) { 386 if (SLPVectorize) { 387 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 388 if (OptLevel > 1 && ExtraVectorizerPasses) { 389 MPM.add(createEarlyCSEPass()); 390 } 391 } 392 393 if (BBVectorize) { 394 MPM.add(createBBVectorizePass()); 395 MPM.add(createInstructionCombiningPass()); 396 addExtensionsToPM(EP_Peephole, MPM); 397 if (OptLevel > 1 && UseGVNAfterVectorization) 398 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies 399 else 400 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 401 402 // BBVectorize may have significantly shortened a loop body; unroll again. 403 if (!DisableUnrollLoops) 404 MPM.add(createLoopUnrollPass()); 405 } 406 } 407 408 addExtensionsToPM(EP_Peephole, MPM); 409 MPM.add(createCFGSimplificationPass()); 410 MPM.add(createInstructionCombiningPass()); 411 412 if (!DisableUnrollLoops) { 413 MPM.add(createLoopUnrollPass()); // Unroll small loops 414 415 // LoopUnroll may generate some redundency to cleanup. 416 MPM.add(createInstructionCombiningPass()); 417 418 // Runtime unrolling will introduce runtime check in loop prologue. If the 419 // unrolled loop is a inner loop, then the prologue will be inside the 420 // outer loop. LICM pass can help to promote the runtime check out if the 421 // checked value is loop invariant. 422 MPM.add(createLICMPass()); 423 } 424 425 // After vectorization and unrolling, assume intrinsics may tell us more 426 // about pointer alignments. 427 MPM.add(createAlignmentFromAssumptionsPass()); 428 429 if (!DisableUnitAtATime) { 430 // FIXME: We shouldn't bother with this anymore. 431 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 432 433 // GlobalOpt already deletes dead functions and globals, at -O2 try a 434 // late pass of GlobalDCE. It is capable of deleting dead cycles. 435 if (OptLevel > 1) { 436 if (!PrepareForLTO) { 437 // Remove avail extern fns and globals definitions if we aren't 438 // compiling an object file for later LTO. For LTO we want to preserve 439 // these so they are eligible for inlining at link-time. Note if they 440 // are unreferenced they will be removed by GlobalDCE below, so 441 // this only impacts referenced available externally globals. 442 // Eventually they will be suppressed during codegen, but eliminating 443 // here enables more opportunity for GlobalDCE as it may make 444 // globals referenced by available external functions dead. 445 MPM.add(createEliminateAvailableExternallyPass()); 446 } 447 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 448 MPM.add(createConstantMergePass()); // Merge dup global constants 449 } 450 } 451 452 if (MergeFunctions) 453 MPM.add(createMergeFunctionsPass()); 454 455 addExtensionsToPM(EP_OptimizerLast, MPM); 456 } 457 458 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) { 459 // Provide AliasAnalysis services for optimizations. 460 addInitialAliasAnalysisPasses(PM); 461 462 // Propagate constants at call sites into the functions they call. This 463 // opens opportunities for globalopt (and inlining) by substituting function 464 // pointers passed as arguments to direct uses of functions. 465 PM.add(createIPSCCPPass()); 466 467 // Now that we internalized some globals, see if we can hack on them! 468 PM.add(createGlobalOptimizerPass()); 469 470 // Linking modules together can lead to duplicated global constants, only 471 // keep one copy of each constant. 472 PM.add(createConstantMergePass()); 473 474 // Remove unused arguments from functions. 475 PM.add(createDeadArgEliminationPass()); 476 477 // Reduce the code after globalopt and ipsccp. Both can open up significant 478 // simplification opportunities, and both can propagate functions through 479 // function pointers. When this happens, we often have to resolve varargs 480 // calls, etc, so let instcombine do this. 481 PM.add(createInstructionCombiningPass()); 482 addExtensionsToPM(EP_Peephole, PM); 483 484 // Inline small functions 485 bool RunInliner = Inliner; 486 if (RunInliner) { 487 PM.add(Inliner); 488 Inliner = nullptr; 489 } 490 491 PM.add(createPruneEHPass()); // Remove dead EH info. 492 493 // Optimize globals again if we ran the inliner. 494 if (RunInliner) 495 PM.add(createGlobalOptimizerPass()); 496 PM.add(createGlobalDCEPass()); // Remove dead functions. 497 498 // If we didn't decide to inline a function, check to see if we can 499 // transform it to pass arguments by value instead of by reference. 500 PM.add(createArgumentPromotionPass()); 501 502 // The IPO passes may leave cruft around. Clean up after them. 503 PM.add(createInstructionCombiningPass()); 504 addExtensionsToPM(EP_Peephole, PM); 505 PM.add(createJumpThreadingPass()); 506 507 // Break up allocas 508 if (UseNewSROA) 509 PM.add(createSROAPass()); 510 else 511 PM.add(createScalarReplAggregatesPass()); 512 513 // Run a few AA driven optimizations here and now, to cleanup the code. 514 PM.add(createFunctionAttrsPass()); // Add nocapture. 515 PM.add(createGlobalsModRefPass()); // IP alias analysis. 516 517 PM.add(createLICMPass()); // Hoist loop invariants. 518 if (EnableMLSM) 519 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds. 520 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 521 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 522 523 // Nuke dead stores. 524 PM.add(createDeadStoreEliminationPass()); 525 526 // More loops are countable; try to optimize them. 527 PM.add(createIndVarSimplifyPass()); 528 PM.add(createLoopDeletionPass()); 529 if (EnableLoopInterchange) 530 PM.add(createLoopInterchangePass()); 531 532 PM.add(createLoopVectorizePass(true, LoopVectorize)); 533 534 // More scalar chains could be vectorized due to more alias information 535 if (RunSLPAfterLoopVectorization) 536 if (SLPVectorize) 537 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 538 539 // After vectorization, assume intrinsics may tell us more about pointer 540 // alignments. 541 PM.add(createAlignmentFromAssumptionsPass()); 542 543 if (LoadCombine) 544 PM.add(createLoadCombinePass()); 545 546 // Cleanup and simplify the code after the scalar optimizations. 547 PM.add(createInstructionCombiningPass()); 548 addExtensionsToPM(EP_Peephole, PM); 549 550 PM.add(createJumpThreadingPass()); 551 } 552 553 void PassManagerBuilder::addLateLTOOptimizationPasses( 554 legacy::PassManagerBase &PM) { 555 // Delete basic blocks, which optimization passes may have killed. 556 PM.add(createCFGSimplificationPass()); 557 558 // Now that we have optimized the program, discard unreachable functions. 559 PM.add(createGlobalDCEPass()); 560 561 // FIXME: this is profitable (for compiler time) to do at -O0 too, but 562 // currently it damages debug info. 563 if (MergeFunctions) 564 PM.add(createMergeFunctionsPass()); 565 } 566 567 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { 568 if (LibraryInfo) 569 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 570 571 if (VerifyInput) 572 PM.add(createVerifierPass()); 573 574 if (OptLevel > 1) 575 addLTOOptimizationPasses(PM); 576 577 // Lower bit sets to globals. This pass supports Clang's control flow 578 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI 579 // is enabled. The pass does nothing if CFI is disabled. 580 PM.add(createLowerBitSetsPass()); 581 582 if (OptLevel != 0) 583 addLateLTOOptimizationPasses(PM); 584 585 if (VerifyOutput) 586 PM.add(createVerifierPass()); 587 } 588 589 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { 590 return reinterpret_cast<PassManagerBuilder*>(P); 591 } 592 593 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { 594 return reinterpret_cast<LLVMPassManagerBuilderRef>(P); 595 } 596 597 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { 598 PassManagerBuilder *PMB = new PassManagerBuilder(); 599 return wrap(PMB); 600 } 601 602 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 603 PassManagerBuilder *Builder = unwrap(PMB); 604 delete Builder; 605 } 606 607 void 608 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 609 unsigned OptLevel) { 610 PassManagerBuilder *Builder = unwrap(PMB); 611 Builder->OptLevel = OptLevel; 612 } 613 614 void 615 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 616 unsigned SizeLevel) { 617 PassManagerBuilder *Builder = unwrap(PMB); 618 Builder->SizeLevel = SizeLevel; 619 } 620 621 void 622 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 623 LLVMBool Value) { 624 PassManagerBuilder *Builder = unwrap(PMB); 625 Builder->DisableUnitAtATime = Value; 626 } 627 628 void 629 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 630 LLVMBool Value) { 631 PassManagerBuilder *Builder = unwrap(PMB); 632 Builder->DisableUnrollLoops = Value; 633 } 634 635 void 636 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 637 LLVMBool Value) { 638 // NOTE: The simplify-libcalls pass has been removed. 639 } 640 641 void 642 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 643 unsigned Threshold) { 644 PassManagerBuilder *Builder = unwrap(PMB); 645 Builder->Inliner = createFunctionInliningPass(Threshold); 646 } 647 648 void 649 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 650 LLVMPassManagerRef PM) { 651 PassManagerBuilder *Builder = unwrap(PMB); 652 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM); 653 Builder->populateFunctionPassManager(*FPM); 654 } 655 656 void 657 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 658 LLVMPassManagerRef PM) { 659 PassManagerBuilder *Builder = unwrap(PMB); 660 legacy::PassManagerBase *MPM = unwrap(PM); 661 Builder->populateModulePassManager(*MPM); 662 } 663 664 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 665 LLVMPassManagerRef PM, 666 LLVMBool Internalize, 667 LLVMBool RunInliner) { 668 PassManagerBuilder *Builder = unwrap(PMB); 669 legacy::PassManagerBase *LPM = unwrap(PM); 670 671 // A small backwards compatibility hack. populateLTOPassManager used to take 672 // an RunInliner option. 673 if (RunInliner && !Builder->Inliner) 674 Builder->Inliner = createFunctionInliningPass(); 675 676 Builder->populateLTOPassManager(*LPM); 677 } 678