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