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