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(createGlobalOptimizerPass()); 486 487 // Linking modules together can lead to duplicated global constants, only 488 // keep one copy of each constant. 489 PM.add(createConstantMergePass()); 490 491 // Remove unused arguments from functions. 492 PM.add(createDeadArgEliminationPass()); 493 494 // Reduce the code after globalopt and ipsccp. Both can open up significant 495 // simplification opportunities, and both can propagate functions through 496 // function pointers. When this happens, we often have to resolve varargs 497 // calls, etc, so let instcombine do this. 498 PM.add(createInstructionCombiningPass()); 499 addExtensionsToPM(EP_Peephole, PM); 500 501 // Inline small functions 502 bool RunInliner = Inliner; 503 if (RunInliner) { 504 PM.add(Inliner); 505 Inliner = nullptr; 506 } 507 508 PM.add(createPruneEHPass()); // Remove dead EH info. 509 510 // Optimize globals again if we ran the inliner. 511 if (RunInliner) 512 PM.add(createGlobalOptimizerPass()); 513 PM.add(createGlobalDCEPass()); // Remove dead functions. 514 515 // If we didn't decide to inline a function, check to see if we can 516 // transform it to pass arguments by value instead of by reference. 517 PM.add(createArgumentPromotionPass()); 518 519 // The IPO passes may leave cruft around. Clean up after them. 520 PM.add(createInstructionCombiningPass()); 521 addExtensionsToPM(EP_Peephole, PM); 522 PM.add(createJumpThreadingPass()); 523 524 // Break up allocas 525 if (UseNewSROA) 526 PM.add(createSROAPass()); 527 else 528 PM.add(createScalarReplAggregatesPass()); 529 530 // Run a few AA driven optimizations here and now, to cleanup the code. 531 PM.add(createFunctionAttrsPass()); // Add nocapture. 532 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis. 533 534 PM.add(createLICMPass()); // Hoist loop invariants. 535 if (EnableMLSM) 536 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds. 537 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 538 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 539 540 // Nuke dead stores. 541 PM.add(createDeadStoreEliminationPass()); 542 543 // More loops are countable; try to optimize them. 544 PM.add(createIndVarSimplifyPass()); 545 PM.add(createLoopDeletionPass()); 546 if (EnableLoopInterchange) 547 PM.add(createLoopInterchangePass()); 548 549 PM.add(createLoopVectorizePass(true, LoopVectorize)); 550 551 // More scalar chains could be vectorized due to more alias information 552 if (RunSLPAfterLoopVectorization) 553 if (SLPVectorize) 554 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 555 556 // After vectorization, assume intrinsics may tell us more about pointer 557 // alignments. 558 PM.add(createAlignmentFromAssumptionsPass()); 559 560 if (LoadCombine) 561 PM.add(createLoadCombinePass()); 562 563 // Cleanup and simplify the code after the scalar optimizations. 564 PM.add(createInstructionCombiningPass()); 565 addExtensionsToPM(EP_Peephole, PM); 566 567 PM.add(createJumpThreadingPass()); 568 } 569 570 void PassManagerBuilder::addLateLTOOptimizationPasses( 571 legacy::PassManagerBase &PM) { 572 // Delete basic blocks, which optimization passes may have killed. 573 PM.add(createCFGSimplificationPass()); 574 575 // Drop bodies of available externally objects to improve GlobalDCE. 576 PM.add(createEliminateAvailableExternallyPass()); 577 578 // Now that we have optimized the program, discard unreachable functions. 579 PM.add(createGlobalDCEPass()); 580 581 // FIXME: this is profitable (for compiler time) to do at -O0 too, but 582 // currently it damages debug info. 583 if (MergeFunctions) 584 PM.add(createMergeFunctionsPass()); 585 } 586 587 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { 588 if (LibraryInfo) 589 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 590 591 if (VerifyInput) 592 PM.add(createVerifierPass()); 593 594 if (OptLevel > 1) 595 addLTOOptimizationPasses(PM); 596 597 // Lower bit sets to globals. This pass supports Clang's control flow 598 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI 599 // is enabled. The pass does nothing if CFI is disabled. 600 PM.add(createLowerBitSetsPass()); 601 602 if (OptLevel != 0) 603 addLateLTOOptimizationPasses(PM); 604 605 if (VerifyOutput) 606 PM.add(createVerifierPass()); 607 } 608 609 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { 610 return reinterpret_cast<PassManagerBuilder*>(P); 611 } 612 613 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { 614 return reinterpret_cast<LLVMPassManagerBuilderRef>(P); 615 } 616 617 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { 618 PassManagerBuilder *PMB = new PassManagerBuilder(); 619 return wrap(PMB); 620 } 621 622 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 623 PassManagerBuilder *Builder = unwrap(PMB); 624 delete Builder; 625 } 626 627 void 628 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 629 unsigned OptLevel) { 630 PassManagerBuilder *Builder = unwrap(PMB); 631 Builder->OptLevel = OptLevel; 632 } 633 634 void 635 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 636 unsigned SizeLevel) { 637 PassManagerBuilder *Builder = unwrap(PMB); 638 Builder->SizeLevel = SizeLevel; 639 } 640 641 void 642 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 643 LLVMBool Value) { 644 PassManagerBuilder *Builder = unwrap(PMB); 645 Builder->DisableUnitAtATime = Value; 646 } 647 648 void 649 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 650 LLVMBool Value) { 651 PassManagerBuilder *Builder = unwrap(PMB); 652 Builder->DisableUnrollLoops = Value; 653 } 654 655 void 656 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 657 LLVMBool Value) { 658 // NOTE: The simplify-libcalls pass has been removed. 659 } 660 661 void 662 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 663 unsigned Threshold) { 664 PassManagerBuilder *Builder = unwrap(PMB); 665 Builder->Inliner = createFunctionInliningPass(Threshold); 666 } 667 668 void 669 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 670 LLVMPassManagerRef PM) { 671 PassManagerBuilder *Builder = unwrap(PMB); 672 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM); 673 Builder->populateFunctionPassManager(*FPM); 674 } 675 676 void 677 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 678 LLVMPassManagerRef PM) { 679 PassManagerBuilder *Builder = unwrap(PMB); 680 legacy::PassManagerBase *MPM = unwrap(PM); 681 Builder->populateModulePassManager(*MPM); 682 } 683 684 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 685 LLVMPassManagerRef PM, 686 LLVMBool Internalize, 687 LLVMBool RunInliner) { 688 PassManagerBuilder *Builder = unwrap(PMB); 689 legacy::PassManagerBase *LPM = unwrap(PM); 690 691 // A small backwards compatibility hack. populateLTOPassManager used to take 692 // an RunInliner option. 693 if (RunInliner && !Builder->Inliner) 694 Builder->Inliner = createFunctionInliningPass(); 695 696 Builder->populateLTOPassManager(*LPM); 697 } 698