1//===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// This file defines the PassManagerBuilder class, which is used to set up a 10// "standard" optimization sequence suitable for languages like C and C++. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Transforms/IPO/PassManagerBuilder.h" 15#include "llvm-c/Transforms/PassManagerBuilder.h" 16#include "llvm/ADT/STLExtras.h" 17#include "llvm/ADT/SmallVector.h" 18#include "llvm/Analysis/BasicAliasAnalysis.h" 19#include "llvm/Analysis/CFLAndersAliasAnalysis.h" 20#include "llvm/Analysis/CFLSteensAliasAnalysis.h" 21#include "llvm/Analysis/GlobalsModRef.h" 22#include "llvm/Analysis/InlineCost.h" 23#include "llvm/Analysis/Passes.h" 24#include "llvm/Analysis/ScopedNoAliasAA.h" 25#include "llvm/Analysis/TargetLibraryInfo.h" 26#include "llvm/Analysis/TypeBasedAliasAnalysis.h" 27#include "llvm/IR/DataLayout.h" 28#include "llvm/IR/LegacyPassManager.h" 29#include "llvm/IR/Verifier.h" 30#include "llvm/Support/CommandLine.h" 31#include "llvm/Support/ManagedStatic.h" 32#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" 33#include "llvm/Transforms/IPO.h" 34#include "llvm/Transforms/IPO/Attributor.h" 35#include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 36#include "llvm/Transforms/IPO/FunctionAttrs.h" 37#include "llvm/Transforms/IPO/InferFunctionAttrs.h" 38#include "llvm/Transforms/InstCombine/InstCombine.h" 39#include "llvm/Transforms/Instrumentation.h" 40#include "llvm/Transforms/Scalar.h" 41#include "llvm/Transforms/Scalar/GVN.h" 42#include "llvm/Transforms/Scalar/InstSimplifyPass.h" 43#include "llvm/Transforms/Scalar/LICM.h" 44#include "llvm/Transforms/Scalar/LoopUnrollPass.h" 45#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 46#include "llvm/Transforms/Utils.h" 47#include "llvm/Transforms/Vectorize.h" 48#include "llvm/Transforms/Vectorize/LoopVectorize.h" 49#include "llvm/Transforms/Vectorize/SLPVectorizer.h" 50 51using namespace llvm; 52 53static cl::opt<bool> 54 RunPartialInlining("enable-partial-inlining", cl::init(false), cl::Hidden, 55 cl::ZeroOrMore, cl::desc("Run Partial inlinining pass")); 56 57static cl::opt<bool> 58UseGVNAfterVectorization("use-gvn-after-vectorization", 59 cl::init(false), cl::Hidden, 60 cl::desc("Run GVN instead of Early CSE after vectorization passes")); 61 62static cl::opt<bool> ExtraVectorizerPasses( 63 "extra-vectorizer-passes", cl::init(false), cl::Hidden, 64 cl::desc("Run cleanup optimization passes after vectorization.")); 65 66static cl::opt<bool> 67RunLoopRerolling("reroll-loops", cl::Hidden, 68 cl::desc("Run the loop rerolling pass")); 69 70static cl::opt<bool> RunNewGVN("enable-newgvn", cl::init(false), cl::Hidden, 71 cl::desc("Run the NewGVN pass")); 72 73// Experimental option to use CFL-AA 74enum class CFLAAType { None, Steensgaard, Andersen, Both }; 75static cl::opt<CFLAAType> 76 UseCFLAA("use-cfl-aa", cl::init(CFLAAType::None), cl::Hidden, 77 cl::desc("Enable the new, experimental CFL alias analysis"), 78 cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"), 79 clEnumValN(CFLAAType::Steensgaard, "steens", 80 "Enable unification-based CFL-AA"), 81 clEnumValN(CFLAAType::Andersen, "anders", 82 "Enable inclusion-based CFL-AA"), 83 clEnumValN(CFLAAType::Both, "both", 84 "Enable both variants of CFL-AA"))); 85 86static cl::opt<bool> EnableLoopInterchange( 87 "enable-loopinterchange", cl::init(false), cl::Hidden, 88 cl::desc("Enable the new, experimental LoopInterchange Pass")); 89 90static cl::opt<bool> EnableUnrollAndJam("enable-unroll-and-jam", 91 cl::init(false), cl::Hidden, 92 cl::desc("Enable Unroll And Jam Pass")); 93 94static cl::opt<bool> 95 EnablePrepareForThinLTO("prepare-for-thinlto", cl::init(false), cl::Hidden, 96 cl::desc("Enable preparation for ThinLTO.")); 97 98static cl::opt<bool> 99 EnablePerformThinLTO("perform-thinlto", cl::init(false), cl::Hidden, 100 cl::desc("Enable performing ThinLTO.")); 101 102cl::opt<bool> EnableHotColdSplit("hot-cold-split", cl::init(false), cl::Hidden, 103 cl::desc("Enable hot-cold splitting pass")); 104 105static cl::opt<bool> UseLoopVersioningLICM( 106 "enable-loop-versioning-licm", cl::init(false), cl::Hidden, 107 cl::desc("Enable the experimental Loop Versioning LICM pass")); 108 109static cl::opt<bool> 110 DisablePreInliner("disable-preinline", cl::init(false), cl::Hidden, 111 cl::desc("Disable pre-instrumentation inliner")); 112 113static cl::opt<int> PreInlineThreshold( 114 "preinline-threshold", cl::Hidden, cl::init(75), cl::ZeroOrMore, 115 cl::desc("Control the amount of inlining in pre-instrumentation inliner " 116 "(default = 75)")); 117 118static cl::opt<bool> EnableGVNHoist( 119 "enable-gvn-hoist", cl::init(false), cl::Hidden, 120 cl::desc("Enable the GVN hoisting pass (default = off)")); 121 122static cl::opt<bool> 123 DisableLibCallsShrinkWrap("disable-libcalls-shrinkwrap", cl::init(false), 124 cl::Hidden, 125 cl::desc("Disable shrink-wrap library calls")); 126 127static cl::opt<bool> EnableSimpleLoopUnswitch( 128 "enable-simple-loop-unswitch", cl::init(false), cl::Hidden, 129 cl::desc("Enable the simple loop unswitch pass. Also enables independent " 130 "cleanup passes integrated into the loop pass manager pipeline.")); 131 132static cl::opt<bool> EnableGVNSink( 133 "enable-gvn-sink", cl::init(false), cl::Hidden, 134 cl::desc("Enable the GVN sinking pass (default = off)")); 135 136// This option is used in simplifying testing SampleFDO optimizations for 137// profile loading. 138static cl::opt<bool> 139 EnableCHR("enable-chr", cl::init(true), cl::Hidden, 140 cl::desc("Enable control height reduction optimization (CHR)")); 141 142cl::opt<bool> FlattenedProfileUsed( 143 "flattened-profile-used", cl::init(false), cl::Hidden, 144 cl::desc("Indicate the sample profile being used is flattened, i.e., " 145 "no inline hierachy exists in the profile. ")); 146 147cl::opt<bool> EnableOrderFileInstrumentation( 148 "enable-order-file-instrumentation", cl::init(false), cl::Hidden, 149 cl::desc("Enable order file instrumentation (default = off)")); 150 151static cl::opt<bool> 152 EnableMatrix("enable-matrix", cl::init(false), cl::Hidden, 153 cl::desc("Enable lowering of the matrix intrinsics")); 154 155PassManagerBuilder::PassManagerBuilder() { 156 OptLevel = 2; 157 SizeLevel = 0; 158 LibraryInfo = nullptr; 159 Inliner = nullptr; 160 DisableUnrollLoops = false; 161 SLPVectorize = RunSLPVectorization; 162 LoopVectorize = EnableLoopVectorization; 163 LoopsInterleaved = EnableLoopInterleaving; 164 RerollLoops = RunLoopRerolling; 165 NewGVN = RunNewGVN; 166 LicmMssaOptCap = SetLicmMssaOptCap; 167 LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; 168 DisableGVNLoadPRE = false; 169 ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll; 170 VerifyInput = false; 171 VerifyOutput = false; 172 MergeFunctions = false; 173 PrepareForLTO = false; 174 EnablePGOInstrGen = false; 175 EnablePGOCSInstrGen = false; 176 EnablePGOCSInstrUse = false; 177 PGOInstrGen = ""; 178 PGOInstrUse = ""; 179 PGOSampleUse = ""; 180 PrepareForThinLTO = EnablePrepareForThinLTO; 181 PerformThinLTO = EnablePerformThinLTO; 182 DivergentTarget = false; 183} 184 185PassManagerBuilder::~PassManagerBuilder() { 186 delete LibraryInfo; 187 delete Inliner; 188} 189 190/// Set of global extensions, automatically added as part of the standard set. 191static ManagedStatic< 192 SmallVector<std::tuple<PassManagerBuilder::ExtensionPointTy, 193 PassManagerBuilder::ExtensionFn, 194 PassManagerBuilder::GlobalExtensionID>, 195 8>> 196 GlobalExtensions; 197static PassManagerBuilder::GlobalExtensionID GlobalExtensionsCounter; 198 199/// Check if GlobalExtensions is constructed and not empty. 200/// Since GlobalExtensions is a managed static, calling 'empty()' will trigger 201/// the construction of the object. 202static bool GlobalExtensionsNotEmpty() { 203 return GlobalExtensions.isConstructed() && !GlobalExtensions->empty(); 204} 205 206PassManagerBuilder::GlobalExtensionID 207PassManagerBuilder::addGlobalExtension(PassManagerBuilder::ExtensionPointTy Ty, 208 PassManagerBuilder::ExtensionFn Fn) { 209 auto ExtensionID = GlobalExtensionsCounter++; 210 GlobalExtensions->push_back(std::make_tuple(Ty, std::move(Fn), ExtensionID)); 211 return ExtensionID; 212} 213 214void PassManagerBuilder::removeGlobalExtension( 215 PassManagerBuilder::GlobalExtensionID ExtensionID) { 216 // RegisterStandardPasses may try to call this function after GlobalExtensions 217 // has already been destroyed; doing so should not generate an error. 218 if (!GlobalExtensions.isConstructed()) 219 return; 220 221 auto GlobalExtension = 222 llvm::find_if(*GlobalExtensions, [ExtensionID](const auto &elem) { 223 return std::get<2>(elem) == ExtensionID; 224 }); 225 assert(GlobalExtension != GlobalExtensions->end() && 226 "The extension ID to be removed should always be valid."); 227 228 GlobalExtensions->erase(GlobalExtension); 229} 230 231void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { 232 Extensions.push_back(std::make_pair(Ty, std::move(Fn))); 233} 234 235void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, 236 legacy::PassManagerBase &PM) const { 237 if (GlobalExtensionsNotEmpty()) { 238 for (auto &Ext : *GlobalExtensions) { 239 if (std::get<0>(Ext) == ETy) 240 std::get<1>(Ext)(*this, PM); 241 } 242 } 243 for (unsigned i = 0, e = Extensions.size(); i != e; ++i) 244 if (Extensions[i].first == ETy) 245 Extensions[i].second(*this, PM); 246} 247 248void PassManagerBuilder::addInitialAliasAnalysisPasses( 249 legacy::PassManagerBase &PM) const { 250 switch (UseCFLAA) { 251 case CFLAAType::Steensgaard: 252 PM.add(createCFLSteensAAWrapperPass()); 253 break; 254 case CFLAAType::Andersen: 255 PM.add(createCFLAndersAAWrapperPass()); 256 break; 257 case CFLAAType::Both: 258 PM.add(createCFLSteensAAWrapperPass()); 259 PM.add(createCFLAndersAAWrapperPass()); 260 break; 261 default: 262 break; 263 } 264 265 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that 266 // BasicAliasAnalysis wins if they disagree. This is intended to help 267 // support "obvious" type-punning idioms. 268 PM.add(createTypeBasedAAWrapperPass()); 269 PM.add(createScopedNoAliasAAWrapperPass()); 270} 271 272void PassManagerBuilder::addInstructionCombiningPass( 273 legacy::PassManagerBase &PM) const { 274 bool ExpensiveCombines = OptLevel > 2; 275 PM.add(createInstructionCombiningPass(ExpensiveCombines)); 276} 277 278void PassManagerBuilder::populateFunctionPassManager( 279 legacy::FunctionPassManager &FPM) { 280 addExtensionsToPM(EP_EarlyAsPossible, FPM); 281 FPM.add(createEntryExitInstrumenterPass()); 282 283 // Add LibraryInfo if we have some. 284 if (LibraryInfo) 285 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 286 287 if (OptLevel == 0) return; 288 289 addInitialAliasAnalysisPasses(FPM); 290 291 FPM.add(createCFGSimplificationPass()); 292 FPM.add(createSROAPass()); 293 FPM.add(createEarlyCSEPass()); 294 FPM.add(createLowerExpectIntrinsicPass()); 295} 296 297// Do PGO instrumentation generation or use pass as the option specified. 298void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM, 299 bool IsCS = false) { 300 if (IsCS) { 301 if (!EnablePGOCSInstrGen && !EnablePGOCSInstrUse) 302 return; 303 } else if (!EnablePGOInstrGen && PGOInstrUse.empty() && PGOSampleUse.empty()) 304 return; 305 306 // Perform the preinline and cleanup passes for O1 and above. 307 // And avoid doing them if optimizing for size. 308 // We will not do this inline for context sensitive PGO (when IsCS is true). 309 if (OptLevel > 0 && SizeLevel == 0 && !DisablePreInliner && 310 PGOSampleUse.empty() && !IsCS) { 311 // Create preinline pass. We construct an InlineParams object and specify 312 // the threshold here to avoid the command line options of the regular 313 // inliner to influence pre-inlining. The only fields of InlineParams we 314 // care about are DefaultThreshold and HintThreshold. 315 InlineParams IP; 316 IP.DefaultThreshold = PreInlineThreshold; 317 // FIXME: The hint threshold has the same value used by the regular inliner. 318 // This should probably be lowered after performance testing. 319 IP.HintThreshold = 325; 320 321 MPM.add(createFunctionInliningPass(IP)); 322 MPM.add(createSROAPass()); 323 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies 324 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 325 MPM.add(createInstructionCombiningPass()); // Combine silly seq's 326 addExtensionsToPM(EP_Peephole, MPM); 327 } 328 if ((EnablePGOInstrGen && !IsCS) || (EnablePGOCSInstrGen && IsCS)) { 329 MPM.add(createPGOInstrumentationGenLegacyPass(IsCS)); 330 // Add the profile lowering pass. 331 InstrProfOptions Options; 332 if (!PGOInstrGen.empty()) 333 Options.InstrProfileOutput = PGOInstrGen; 334 Options.DoCounterPromotion = true; 335 Options.UseBFIInPromotion = IsCS; 336 MPM.add(createLoopRotatePass()); 337 MPM.add(createInstrProfilingLegacyPass(Options, IsCS)); 338 } 339 if (!PGOInstrUse.empty()) 340 MPM.add(createPGOInstrumentationUseLegacyPass(PGOInstrUse, IsCS)); 341 // Indirect call promotion that promotes intra-module targets only. 342 // For ThinLTO this is done earlier due to interactions with globalopt 343 // for imported functions. We don't run this at -O0. 344 if (OptLevel > 0 && !IsCS) 345 MPM.add( 346 createPGOIndirectCallPromotionLegacyPass(false, !PGOSampleUse.empty())); 347} 348void PassManagerBuilder::addFunctionSimplificationPasses( 349 legacy::PassManagerBase &MPM) { 350 // Start of function pass. 351 // Break up aggregate allocas, using SSAUpdater. 352 assert(OptLevel >= 1 && "Calling function optimizer with no optimization level!"); 353 MPM.add(createSROAPass()); 354 MPM.add(createEarlyCSEPass(true /* Enable mem-ssa. */)); // Catch trivial redundancies 355 356 if (OptLevel > 1) { 357 if (EnableGVNHoist) 358 MPM.add(createGVNHoistPass()); 359 if (EnableGVNSink) { 360 MPM.add(createGVNSinkPass()); 361 MPM.add(createCFGSimplificationPass()); 362 } 363 } 364 365 if (OptLevel > 1) { 366 // Speculative execution if the target has divergent branches; otherwise nop. 367 MPM.add(createSpeculativeExecutionIfHasBranchDivergencePass()); 368 369 MPM.add(createJumpThreadingPass()); // Thread jumps. 370 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals 371 } 372 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 373 // Combine silly seq's 374 if (OptLevel > 2) 375 MPM.add(createAggressiveInstCombinerPass()); 376 addInstructionCombiningPass(MPM); 377 if (SizeLevel == 0 && !DisableLibCallsShrinkWrap) 378 MPM.add(createLibCallsShrinkWrapPass()); 379 addExtensionsToPM(EP_Peephole, MPM); 380 381 // Optimize memory intrinsic calls based on the profiled size information. 382 if (SizeLevel == 0) 383 MPM.add(createPGOMemOPSizeOptLegacyPass()); 384 385 // TODO: Investigate the cost/benefit of tail call elimination on debugging. 386 if (OptLevel > 1) 387 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls 388 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 389 MPM.add(createReassociatePass()); // Reassociate expressions 390 391 // Begin the loop pass pipeline. 392 if (EnableSimpleLoopUnswitch) { 393 // The simple loop unswitch pass relies on separate cleanup passes. Schedule 394 // them first so when we re-process a loop they run before other loop 395 // passes. 396 MPM.add(createLoopInstSimplifyPass()); 397 MPM.add(createLoopSimplifyCFGPass()); 398 } 399 // Rotate Loop - disable header duplication at -Oz 400 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); 401 // TODO: Investigate promotion cap for O1. 402 MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 403 if (EnableSimpleLoopUnswitch) 404 MPM.add(createSimpleLoopUnswitchLegacyPass()); 405 else 406 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3, DivergentTarget)); 407 // FIXME: We break the loop pass pipeline here in order to do full 408 // simplify-cfg. Eventually loop-simplifycfg should be enhanced to replace the 409 // need for this. 410 MPM.add(createCFGSimplificationPass()); 411 addInstructionCombiningPass(MPM); 412 // We resume loop passes creating a second loop pipeline here. 413 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars 414 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. 415 addExtensionsToPM(EP_LateLoopOptimizations, MPM); 416 MPM.add(createLoopDeletionPass()); // Delete dead loops 417 418 if (EnableLoopInterchange) 419 MPM.add(createLoopInterchangePass()); // Interchange loops 420 421 // Unroll small loops 422 MPM.add(createSimpleLoopUnrollPass(OptLevel, DisableUnrollLoops, 423 ForgetAllSCEVInLoopUnroll)); 424 addExtensionsToPM(EP_LoopOptimizerEnd, MPM); 425 // This ends the loop pass pipelines. 426 427 if (OptLevel > 1) { 428 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds 429 MPM.add(NewGVN ? createNewGVNPass() 430 : createGVNPass(DisableGVNLoadPRE)); // Remove redundancies 431 } 432 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset 433 MPM.add(createSCCPPass()); // Constant prop with SCCP 434 435 // Delete dead bit computations (instcombine runs after to fold away the dead 436 // computations, and then ADCE will run later to exploit any new DCE 437 // opportunities that creates). 438 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations 439 440 // Run instcombine after redundancy elimination to exploit opportunities 441 // opened up by them. 442 addInstructionCombiningPass(MPM); 443 addExtensionsToPM(EP_Peephole, MPM); 444 if (OptLevel > 1) { 445 MPM.add(createJumpThreadingPass()); // Thread jumps 446 MPM.add(createCorrelatedValuePropagationPass()); 447 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores 448 MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 449 } 450 451 addExtensionsToPM(EP_ScalarOptimizerLate, MPM); 452 453 if (RerollLoops) 454 MPM.add(createLoopRerollPass()); 455 456 // TODO: Investigate if this is too expensive at O1. 457 MPM.add(createAggressiveDCEPass()); // Delete dead instructions 458 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs 459 // Clean up after everything. 460 addInstructionCombiningPass(MPM); 461 addExtensionsToPM(EP_Peephole, MPM); 462 463 if (EnableCHR && OptLevel >= 3 && 464 (!PGOInstrUse.empty() || !PGOSampleUse.empty() || EnablePGOCSInstrGen)) 465 MPM.add(createControlHeightReductionLegacyPass()); 466} 467 468void PassManagerBuilder::populateModulePassManager( 469 legacy::PassManagerBase &MPM) { 470 // Whether this is a default or *LTO pre-link pipeline. The FullLTO post-link 471 // is handled separately, so just check this is not the ThinLTO post-link. 472 bool DefaultOrPreLinkPipeline = !PerformThinLTO; 473 474 if (!PGOSampleUse.empty()) { 475 MPM.add(createPruneEHPass()); 476 // In ThinLTO mode, when flattened profile is used, all the available 477 // profile information will be annotated in PreLink phase so there is 478 // no need to load the profile again in PostLink. 479 if (!(FlattenedProfileUsed && PerformThinLTO)) 480 MPM.add(createSampleProfileLoaderPass(PGOSampleUse)); 481 } 482 483 // Allow forcing function attributes as a debugging and tuning aid. 484 MPM.add(createForceFunctionAttrsLegacyPass()); 485 486 // If all optimizations are disabled, just run the always-inline pass and, 487 // if enabled, the function merging pass. 488 if (OptLevel == 0) { 489 addPGOInstrPasses(MPM); 490 if (Inliner) { 491 MPM.add(Inliner); 492 Inliner = nullptr; 493 } 494 495 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly 496 // creates a CGSCC pass manager, but we don't want to add extensions into 497 // that pass manager. To prevent this we insert a no-op module pass to reset 498 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0 499 // builds. The function merging pass is 500 if (MergeFunctions) 501 MPM.add(createMergeFunctionsPass()); 502 else if (GlobalExtensionsNotEmpty() || !Extensions.empty()) 503 MPM.add(createBarrierNoopPass()); 504 505 if (PerformThinLTO) { 506 // Drop available_externally and unreferenced globals. This is necessary 507 // with ThinLTO in order to avoid leaving undefined references to dead 508 // globals in the object file. 509 MPM.add(createEliminateAvailableExternallyPass()); 510 MPM.add(createGlobalDCEPass()); 511 } 512 513 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); 514 515 if (PrepareForLTO || PrepareForThinLTO) { 516 MPM.add(createCanonicalizeAliasesPass()); 517 // Rename anon globals to be able to export them in the summary. 518 // This has to be done after we add the extensions to the pass manager 519 // as there could be passes (e.g. Adddress sanitizer) which introduce 520 // new unnamed globals. 521 MPM.add(createNameAnonGlobalPass()); 522 } 523 return; 524 } 525 526 // Add LibraryInfo if we have some. 527 if (LibraryInfo) 528 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 529 530 addInitialAliasAnalysisPasses(MPM); 531 532 // For ThinLTO there are two passes of indirect call promotion. The 533 // first is during the compile phase when PerformThinLTO=false and 534 // intra-module indirect call targets are promoted. The second is during 535 // the ThinLTO backend when PerformThinLTO=true, when we promote imported 536 // inter-module indirect calls. For that we perform indirect call promotion 537 // earlier in the pass pipeline, here before globalopt. Otherwise imported 538 // available_externally functions look unreferenced and are removed. 539 if (PerformThinLTO) 540 MPM.add(createPGOIndirectCallPromotionLegacyPass(/*InLTO = */ true, 541 !PGOSampleUse.empty())); 542 543 // For SamplePGO in ThinLTO compile phase, we do not want to unroll loops 544 // as it will change the CFG too much to make the 2nd profile annotation 545 // in backend more difficult. 546 bool PrepareForThinLTOUsingPGOSampleProfile = 547 PrepareForThinLTO && !PGOSampleUse.empty(); 548 if (PrepareForThinLTOUsingPGOSampleProfile) 549 DisableUnrollLoops = true; 550 551 // Infer attributes about declarations if possible. 552 MPM.add(createInferFunctionAttrsLegacyPass()); 553 554 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); 555 556 if (OptLevel > 2) 557 MPM.add(createCallSiteSplittingPass()); 558 559 MPM.add(createIPSCCPPass()); // IP SCCP 560 MPM.add(createCalledValuePropagationPass()); 561 562 // Infer attributes on declarations, call sites, arguments, etc. 563 MPM.add(createAttributorLegacyPass()); 564 565 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars 566 // Promote any localized global vars. 567 MPM.add(createPromoteMemoryToRegisterPass()); 568 569 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination 570 571 addInstructionCombiningPass(MPM); // Clean up after IPCP & DAE 572 addExtensionsToPM(EP_Peephole, MPM); 573 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE 574 575 // For SamplePGO in ThinLTO compile phase, we do not want to do indirect 576 // call promotion as it will change the CFG too much to make the 2nd 577 // profile annotation in backend more difficult. 578 // PGO instrumentation is added during the compile phase for ThinLTO, do 579 // not run it a second time 580 if (DefaultOrPreLinkPipeline && !PrepareForThinLTOUsingPGOSampleProfile) 581 addPGOInstrPasses(MPM); 582 583 // Create profile COMDAT variables. Lld linker wants to see all variables 584 // before the LTO/ThinLTO link since it needs to resolve symbols/comdats. 585 if (!PerformThinLTO && EnablePGOCSInstrGen) 586 MPM.add(createPGOInstrumentationGenCreateVarLegacyPass(PGOInstrGen)); 587 588 // We add a module alias analysis pass here. In part due to bugs in the 589 // analysis infrastructure this "works" in that the analysis stays alive 590 // for the entire SCC pass run below. 591 MPM.add(createGlobalsAAWrapperPass()); 592 593 // Start of CallGraph SCC passes. 594 MPM.add(createPruneEHPass()); // Remove dead EH info 595 bool RunInliner = false; 596 if (Inliner) { 597 MPM.add(Inliner); 598 Inliner = nullptr; 599 RunInliner = true; 600 } 601 602 MPM.add(createPostOrderFunctionAttrsLegacyPass()); 603 if (OptLevel > 2) 604 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args 605 606 addExtensionsToPM(EP_CGSCCOptimizerLate, MPM); 607 addFunctionSimplificationPasses(MPM); 608 609 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC 610 // pass manager that we are specifically trying to avoid. To prevent this 611 // we must insert a no-op module pass to reset the pass manager. 612 MPM.add(createBarrierNoopPass()); 613 614 if (RunPartialInlining) 615 MPM.add(createPartialInliningPass()); 616 617 if (OptLevel > 1 && !PrepareForLTO && !PrepareForThinLTO) 618 // Remove avail extern fns and globals definitions if we aren't 619 // compiling an object file for later LTO. For LTO we want to preserve 620 // these so they are eligible for inlining at link-time. Note if they 621 // are unreferenced they will be removed by GlobalDCE later, so 622 // this only impacts referenced available externally globals. 623 // Eventually they will be suppressed during codegen, but eliminating 624 // here enables more opportunity for GlobalDCE as it may make 625 // globals referenced by available external functions dead 626 // and saves running remaining passes on the eliminated functions. 627 MPM.add(createEliminateAvailableExternallyPass()); 628 629 // CSFDO instrumentation and use pass. Don't invoke this for Prepare pass 630 // for LTO and ThinLTO -- The actual pass will be called after all inlines 631 // are performed. 632 // Need to do this after COMDAT variables have been eliminated, 633 // (i.e. after EliminateAvailableExternallyPass). 634 if (!(PrepareForLTO || PrepareForThinLTO)) 635 addPGOInstrPasses(MPM, /* IsCS */ true); 636 637 if (EnableOrderFileInstrumentation) 638 MPM.add(createInstrOrderFilePass()); 639 640 MPM.add(createReversePostOrderFunctionAttrsPass()); 641 642 // The inliner performs some kind of dead code elimination as it goes, 643 // but there are cases that are not really caught by it. We might 644 // at some point consider teaching the inliner about them, but it 645 // is OK for now to run GlobalOpt + GlobalDCE in tandem as their 646 // benefits generally outweight the cost, making the whole pipeline 647 // faster. 648 if (RunInliner) { 649 MPM.add(createGlobalOptimizerPass()); 650 MPM.add(createGlobalDCEPass()); 651 } 652 653 // If we are planning to perform ThinLTO later, let's not bloat the code with 654 // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes 655 // during ThinLTO and perform the rest of the optimizations afterward. 656 if (PrepareForThinLTO) { 657 // Ensure we perform any last passes, but do so before renaming anonymous 658 // globals in case the passes add any. 659 addExtensionsToPM(EP_OptimizerLast, MPM); 660 MPM.add(createCanonicalizeAliasesPass()); 661 // Rename anon globals to be able to export them in the summary. 662 MPM.add(createNameAnonGlobalPass()); 663 return; 664 } 665 666 if (PerformThinLTO) 667 // Optimize globals now when performing ThinLTO, this enables more 668 // optimizations later. 669 MPM.add(createGlobalOptimizerPass()); 670 671 // Scheduling LoopVersioningLICM when inlining is over, because after that 672 // we may see more accurate aliasing. Reason to run this late is that too 673 // early versioning may prevent further inlining due to increase of code 674 // size. By placing it just after inlining other optimizations which runs 675 // later might get benefit of no-alias assumption in clone loop. 676 if (UseLoopVersioningLICM) { 677 MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM 678 MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 679 } 680 681 // We add a fresh GlobalsModRef run at this point. This is particularly 682 // useful as the above will have inlined, DCE'ed, and function-attr 683 // propagated everything. We should at this point have a reasonably minimal 684 // and richly annotated call graph. By computing aliasing and mod/ref 685 // information for all local globals here, the late loop passes and notably 686 // the vectorizer will be able to use them to help recognize vectorizable 687 // memory operations. 688 // 689 // Note that this relies on a bug in the pass manager which preserves 690 // a module analysis into a function pass pipeline (and throughout it) so 691 // long as the first function pass doesn't invalidate the module analysis. 692 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for 693 // this to work. Fortunately, it is trivial to preserve AliasAnalysis 694 // (doing nothing preserves it as it is required to be conservatively 695 // correct in the face of IR changes). 696 MPM.add(createGlobalsAAWrapperPass()); 697 698 MPM.add(createFloat2IntPass()); 699 MPM.add(createLowerConstantIntrinsicsPass()); 700 701 if (EnableMatrix) { 702 MPM.add(createLowerMatrixIntrinsicsPass()); 703 // CSE the pointer arithmetic of the column vectors. This allows alias 704 // analysis to establish no-aliasing between loads and stores of different 705 // columns of the same matrix. 706 MPM.add(createEarlyCSEPass(false)); 707 } 708 709 addExtensionsToPM(EP_VectorizerStart, MPM); 710 711 // Re-rotate loops in all our loop nests. These may have fallout out of 712 // rotated form due to GVN or other transformations, and the vectorizer relies 713 // on the rotated form. Disable header duplication at -Oz. 714 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); 715 716 // Distribute loops to allow partial vectorization. I.e. isolate dependences 717 // into separate loop that would otherwise inhibit vectorization. This is 718 // currently only performed for loops marked with the metadata 719 // llvm.loop.distribute=true or when -enable-loop-distribute is specified. 720 MPM.add(createLoopDistributePass()); 721 722 MPM.add(createLoopVectorizePass(!LoopsInterleaved, !LoopVectorize)); 723 724 // Eliminate loads by forwarding stores from the previous iteration to loads 725 // of the current iteration. 726 MPM.add(createLoopLoadEliminationPass()); 727 728 // FIXME: Because of #pragma vectorize enable, the passes below are always 729 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when 730 // on -O1 and no #pragma is found). Would be good to have these two passes 731 // as function calls, so that we can only pass them when the vectorizer 732 // changed the code. 733 addInstructionCombiningPass(MPM); 734 if (OptLevel > 1 && ExtraVectorizerPasses) { 735 // At higher optimization levels, try to clean up any runtime overlap and 736 // alignment checks inserted by the vectorizer. We want to track correllated 737 // runtime checks for two inner loops in the same outer loop, fold any 738 // common computations, hoist loop-invariant aspects out of any outer loop, 739 // and unswitch the runtime checks if possible. Once hoisted, we may have 740 // dead (or speculatable) control flows or more combining opportunities. 741 MPM.add(createEarlyCSEPass()); 742 MPM.add(createCorrelatedValuePropagationPass()); 743 addInstructionCombiningPass(MPM); 744 MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 745 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3, DivergentTarget)); 746 MPM.add(createCFGSimplificationPass()); 747 addInstructionCombiningPass(MPM); 748 } 749 750 // Cleanup after loop vectorization, etc. Simplification passes like CVP and 751 // GVN, loop transforms, and others have already run, so it's now better to 752 // convert to more optimized IR using more aggressive simplify CFG options. 753 // The extra sinking transform can create larger basic blocks, so do this 754 // before SLP vectorization. 755 MPM.add(createCFGSimplificationPass(1, true, true, false, true)); 756 757 if (SLPVectorize) { 758 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 759 if (OptLevel > 1 && ExtraVectorizerPasses) { 760 MPM.add(createEarlyCSEPass()); 761 } 762 } 763 764 addExtensionsToPM(EP_Peephole, MPM); 765 addInstructionCombiningPass(MPM); 766 767 if (EnableUnrollAndJam && !DisableUnrollLoops) { 768 // Unroll and Jam. We do this before unroll but need to be in a separate 769 // loop pass manager in order for the outer loop to be processed by 770 // unroll and jam before the inner loop is unrolled. 771 MPM.add(createLoopUnrollAndJamPass(OptLevel)); 772 } 773 774 // Unroll small loops 775 MPM.add(createLoopUnrollPass(OptLevel, DisableUnrollLoops, 776 ForgetAllSCEVInLoopUnroll)); 777 778 if (!DisableUnrollLoops) { 779 // LoopUnroll may generate some redundency to cleanup. 780 addInstructionCombiningPass(MPM); 781 782 // Runtime unrolling will introduce runtime check in loop prologue. If the 783 // unrolled loop is a inner loop, then the prologue will be inside the 784 // outer loop. LICM pass can help to promote the runtime check out if the 785 // checked value is loop invariant. 786 MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 787 } 788 789 MPM.add(createWarnMissedTransformationsPass()); 790 791 // After vectorization and unrolling, assume intrinsics may tell us more 792 // about pointer alignments. 793 MPM.add(createAlignmentFromAssumptionsPass()); 794 795 // FIXME: We shouldn't bother with this anymore. 796 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes 797 798 // GlobalOpt already deletes dead functions and globals, at -O2 try a 799 // late pass of GlobalDCE. It is capable of deleting dead cycles. 800 if (OptLevel > 1) { 801 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. 802 MPM.add(createConstantMergePass()); // Merge dup global constants 803 } 804 805 // See comment in the new PM for justification of scheduling splitting at 806 // this stage (\ref buildModuleSimplificationPipeline). 807 if (EnableHotColdSplit && !(PrepareForLTO || PrepareForThinLTO)) 808 MPM.add(createHotColdSplittingPass()); 809 810 if (MergeFunctions) 811 MPM.add(createMergeFunctionsPass()); 812 813 // LoopSink pass sinks instructions hoisted by LICM, which serves as a 814 // canonicalization pass that enables other optimizations. As a result, 815 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 816 // result too early. 817 MPM.add(createLoopSinkPass()); 818 // Get rid of LCSSA nodes. 819 MPM.add(createInstSimplifyLegacyPass()); 820 821 // This hoists/decomposes div/rem ops. It should run after other sink/hoist 822 // passes to avoid re-sinking, but before SimplifyCFG because it can allow 823 // flattening of blocks. 824 MPM.add(createDivRemPairsPass()); 825 826 // LoopSink (and other loop passes since the last simplifyCFG) might have 827 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. 828 MPM.add(createCFGSimplificationPass()); 829 830 addExtensionsToPM(EP_OptimizerLast, MPM); 831 832 if (PrepareForLTO) { 833 MPM.add(createCanonicalizeAliasesPass()); 834 // Rename anon globals to be able to handle them in the summary 835 MPM.add(createNameAnonGlobalPass()); 836 } 837} 838 839void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) { 840 // Load sample profile before running the LTO optimization pipeline. 841 if (!PGOSampleUse.empty()) { 842 PM.add(createPruneEHPass()); 843 PM.add(createSampleProfileLoaderPass(PGOSampleUse)); 844 } 845 846 // Remove unused virtual tables to improve the quality of code generated by 847 // whole-program devirtualization and bitset lowering. 848 PM.add(createGlobalDCEPass()); 849 850 // Provide AliasAnalysis services for optimizations. 851 addInitialAliasAnalysisPasses(PM); 852 853 // Allow forcing function attributes as a debugging and tuning aid. 854 PM.add(createForceFunctionAttrsLegacyPass()); 855 856 // Infer attributes about declarations if possible. 857 PM.add(createInferFunctionAttrsLegacyPass()); 858 859 if (OptLevel > 1) { 860 // Split call-site with more constrained arguments. 861 PM.add(createCallSiteSplittingPass()); 862 863 // Indirect call promotion. This should promote all the targets that are 864 // left by the earlier promotion pass that promotes intra-module targets. 865 // This two-step promotion is to save the compile time. For LTO, it should 866 // produce the same result as if we only do promotion here. 867 PM.add( 868 createPGOIndirectCallPromotionLegacyPass(true, !PGOSampleUse.empty())); 869 870 // Propagate constants at call sites into the functions they call. This 871 // opens opportunities for globalopt (and inlining) by substituting function 872 // pointers passed as arguments to direct uses of functions. 873 PM.add(createIPSCCPPass()); 874 875 // Attach metadata to indirect call sites indicating the set of functions 876 // they may target at run-time. This should follow IPSCCP. 877 PM.add(createCalledValuePropagationPass()); 878 879 // Infer attributes on declarations, call sites, arguments, etc. 880 PM.add(createAttributorLegacyPass()); 881 } 882 883 // Infer attributes about definitions. The readnone attribute in particular is 884 // required for virtual constant propagation. 885 PM.add(createPostOrderFunctionAttrsLegacyPass()); 886 PM.add(createReversePostOrderFunctionAttrsPass()); 887 888 // Split globals using inrange annotations on GEP indices. This can help 889 // improve the quality of generated code when virtual constant propagation or 890 // control flow integrity are enabled. 891 PM.add(createGlobalSplitPass()); 892 893 // Apply whole-program devirtualization and virtual constant propagation. 894 PM.add(createWholeProgramDevirtPass(ExportSummary, nullptr)); 895 896 // That's all we need at opt level 1. 897 if (OptLevel == 1) 898 return; 899 900 // Now that we internalized some globals, see if we can hack on them! 901 PM.add(createGlobalOptimizerPass()); 902 // Promote any localized global vars. 903 PM.add(createPromoteMemoryToRegisterPass()); 904 905 // Linking modules together can lead to duplicated global constants, only 906 // keep one copy of each constant. 907 PM.add(createConstantMergePass()); 908 909 // Remove unused arguments from functions. 910 PM.add(createDeadArgEliminationPass()); 911 912 // Reduce the code after globalopt and ipsccp. Both can open up significant 913 // simplification opportunities, and both can propagate functions through 914 // function pointers. When this happens, we often have to resolve varargs 915 // calls, etc, so let instcombine do this. 916 if (OptLevel > 2) 917 PM.add(createAggressiveInstCombinerPass()); 918 addInstructionCombiningPass(PM); 919 addExtensionsToPM(EP_Peephole, PM); 920 921 // Inline small functions 922 bool RunInliner = Inliner; 923 if (RunInliner) { 924 PM.add(Inliner); 925 Inliner = nullptr; 926 } 927 928 PM.add(createPruneEHPass()); // Remove dead EH info. 929 930 // CSFDO instrumentation and use pass. 931 addPGOInstrPasses(PM, /* IsCS */ true); 932 933 // Optimize globals again if we ran the inliner. 934 if (RunInliner) 935 PM.add(createGlobalOptimizerPass()); 936 PM.add(createGlobalDCEPass()); // Remove dead functions. 937 938 // If we didn't decide to inline a function, check to see if we can 939 // transform it to pass arguments by value instead of by reference. 940 PM.add(createArgumentPromotionPass()); 941 942 // The IPO passes may leave cruft around. Clean up after them. 943 addInstructionCombiningPass(PM); 944 addExtensionsToPM(EP_Peephole, PM); 945 PM.add(createJumpThreadingPass()); 946 947 // Break up allocas 948 PM.add(createSROAPass()); 949 950 // LTO provides additional opportunities for tailcall elimination due to 951 // link-time inlining, and visibility of nocapture attribute. 952 if (OptLevel > 1) 953 PM.add(createTailCallEliminationPass()); 954 955 // Infer attributes on declarations, call sites, arguments, etc. 956 PM.add(createPostOrderFunctionAttrsLegacyPass()); // Add nocapture. 957 // Run a few AA driven optimizations here and now, to cleanup the code. 958 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis. 959 960 PM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap)); 961 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds. 962 PM.add(NewGVN ? createNewGVNPass() 963 : createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. 964 PM.add(createMemCpyOptPass()); // Remove dead memcpys. 965 966 // Nuke dead stores. 967 PM.add(createDeadStoreEliminationPass()); 968 969 // More loops are countable; try to optimize them. 970 PM.add(createIndVarSimplifyPass()); 971 PM.add(createLoopDeletionPass()); 972 if (EnableLoopInterchange) 973 PM.add(createLoopInterchangePass()); 974 975 // Unroll small loops 976 PM.add(createSimpleLoopUnrollPass(OptLevel, DisableUnrollLoops, 977 ForgetAllSCEVInLoopUnroll)); 978 PM.add(createLoopVectorizePass(true, !LoopVectorize)); 979 // The vectorizer may have significantly shortened a loop body; unroll again. 980 PM.add(createLoopUnrollPass(OptLevel, DisableUnrollLoops, 981 ForgetAllSCEVInLoopUnroll)); 982 983 PM.add(createWarnMissedTransformationsPass()); 984 985 // Now that we've optimized loops (in particular loop induction variables), 986 // we may have exposed more scalar opportunities. Run parts of the scalar 987 // optimizer again at this point. 988 addInstructionCombiningPass(PM); // Initial cleanup 989 PM.add(createCFGSimplificationPass()); // if-convert 990 PM.add(createSCCPPass()); // Propagate exposed constants 991 addInstructionCombiningPass(PM); // Clean up again 992 PM.add(createBitTrackingDCEPass()); 993 994 // More scalar chains could be vectorized due to more alias information 995 if (SLPVectorize) 996 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. 997 998 // After vectorization, assume intrinsics may tell us more about pointer 999 // alignments. 1000 PM.add(createAlignmentFromAssumptionsPass()); 1001 1002 // Cleanup and simplify the code after the scalar optimizations. 1003 addInstructionCombiningPass(PM); 1004 addExtensionsToPM(EP_Peephole, PM); 1005 1006 PM.add(createJumpThreadingPass()); 1007} 1008 1009void PassManagerBuilder::addLateLTOOptimizationPasses( 1010 legacy::PassManagerBase &PM) { 1011 // See comment in the new PM for justification of scheduling splitting at 1012 // this stage (\ref buildLTODefaultPipeline). 1013 if (EnableHotColdSplit) 1014 PM.add(createHotColdSplittingPass()); 1015 1016 // Delete basic blocks, which optimization passes may have killed. 1017 PM.add(createCFGSimplificationPass()); 1018 1019 // Drop bodies of available externally objects to improve GlobalDCE. 1020 PM.add(createEliminateAvailableExternallyPass()); 1021 1022 // Now that we have optimized the program, discard unreachable functions. 1023 PM.add(createGlobalDCEPass()); 1024 1025 // FIXME: this is profitable (for compiler time) to do at -O0 too, but 1026 // currently it damages debug info. 1027 if (MergeFunctions) 1028 PM.add(createMergeFunctionsPass()); 1029} 1030 1031void PassManagerBuilder::populateThinLTOPassManager( 1032 legacy::PassManagerBase &PM) { 1033 PerformThinLTO = true; 1034 if (LibraryInfo) 1035 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 1036 1037 if (VerifyInput) 1038 PM.add(createVerifierPass()); 1039 1040 if (ImportSummary) { 1041 // These passes import type identifier resolutions for whole-program 1042 // devirtualization and CFI. They must run early because other passes may 1043 // disturb the specific instruction patterns that these passes look for, 1044 // creating dependencies on resolutions that may not appear in the summary. 1045 // 1046 // For example, GVN may transform the pattern assume(type.test) appearing in 1047 // two basic blocks into assume(phi(type.test, type.test)), which would 1048 // transform a dependency on a WPD resolution into a dependency on a type 1049 // identifier resolution for CFI. 1050 // 1051 // Also, WPD has access to more precise information than ICP and can 1052 // devirtualize more effectively, so it should operate on the IR first. 1053 PM.add(createWholeProgramDevirtPass(nullptr, ImportSummary)); 1054 PM.add(createLowerTypeTestsPass(nullptr, ImportSummary)); 1055 } 1056 1057 populateModulePassManager(PM); 1058 1059 if (VerifyOutput) 1060 PM.add(createVerifierPass()); 1061 PerformThinLTO = false; 1062} 1063 1064void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { 1065 if (LibraryInfo) 1066 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); 1067 1068 if (VerifyInput) 1069 PM.add(createVerifierPass()); 1070 1071 addExtensionsToPM(EP_FullLinkTimeOptimizationEarly, PM); 1072 1073 if (OptLevel != 0) 1074 addLTOOptimizationPasses(PM); 1075 else { 1076 // The whole-program-devirt pass needs to run at -O0 because only it knows 1077 // about the llvm.type.checked.load intrinsic: it needs to both lower the 1078 // intrinsic itself and handle it in the summary. 1079 PM.add(createWholeProgramDevirtPass(ExportSummary, nullptr)); 1080 } 1081 1082 // Create a function that performs CFI checks for cross-DSO calls with targets 1083 // in the current module. 1084 PM.add(createCrossDSOCFIPass()); 1085 1086 // Lower type metadata and the type.test intrinsic. This pass supports Clang's 1087 // control flow integrity mechanisms (-fsanitize=cfi*) and needs to run at 1088 // link time if CFI is enabled. The pass does nothing if CFI is disabled. 1089 PM.add(createLowerTypeTestsPass(ExportSummary, nullptr)); 1090 1091 if (OptLevel != 0) 1092 addLateLTOOptimizationPasses(PM); 1093 1094 addExtensionsToPM(EP_FullLinkTimeOptimizationLast, PM); 1095 1096 if (VerifyOutput) 1097 PM.add(createVerifierPass()); 1098} 1099 1100inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { 1101 return reinterpret_cast<PassManagerBuilder*>(P); 1102} 1103 1104inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { 1105 return reinterpret_cast<LLVMPassManagerBuilderRef>(P); 1106} 1107 1108LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { 1109 PassManagerBuilder *PMB = new PassManagerBuilder(); 1110 return wrap(PMB); 1111} 1112 1113void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { 1114 PassManagerBuilder *Builder = unwrap(PMB); 1115 delete Builder; 1116} 1117 1118void 1119LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, 1120 unsigned OptLevel) { 1121 PassManagerBuilder *Builder = unwrap(PMB); 1122 Builder->OptLevel = OptLevel; 1123} 1124 1125void 1126LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, 1127 unsigned SizeLevel) { 1128 PassManagerBuilder *Builder = unwrap(PMB); 1129 Builder->SizeLevel = SizeLevel; 1130} 1131 1132void 1133LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, 1134 LLVMBool Value) { 1135 // NOTE: The DisableUnitAtATime switch has been removed. 1136} 1137 1138void 1139LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, 1140 LLVMBool Value) { 1141 PassManagerBuilder *Builder = unwrap(PMB); 1142 Builder->DisableUnrollLoops = Value; 1143} 1144 1145void 1146LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, 1147 LLVMBool Value) { 1148 // NOTE: The simplify-libcalls pass has been removed. 1149} 1150 1151void 1152LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, 1153 unsigned Threshold) { 1154 PassManagerBuilder *Builder = unwrap(PMB); 1155 Builder->Inliner = createFunctionInliningPass(Threshold); 1156} 1157 1158void 1159LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, 1160 LLVMPassManagerRef PM) { 1161 PassManagerBuilder *Builder = unwrap(PMB); 1162 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM); 1163 Builder->populateFunctionPassManager(*FPM); 1164} 1165 1166void 1167LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, 1168 LLVMPassManagerRef PM) { 1169 PassManagerBuilder *Builder = unwrap(PMB); 1170 legacy::PassManagerBase *MPM = unwrap(PM); 1171 Builder->populateModulePassManager(*MPM); 1172} 1173 1174void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, 1175 LLVMPassManagerRef PM, 1176 LLVMBool Internalize, 1177 LLVMBool RunInliner) { 1178 PassManagerBuilder *Builder = unwrap(PMB); 1179 legacy::PassManagerBase *LPM = unwrap(PM); 1180 1181 // A small backwards compatibility hack. populateLTOPassManager used to take 1182 // an RunInliner option. 1183 if (RunInliner && !Builder->Inliner) 1184 Builder->Inliner = createFunctionInliningPass(); 1185 1186 Builder->populateLTOPassManager(*LPM); 1187} 1188