1 //===- Parsing, selection, and construction of pass pipelines -------------===// 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 /// \file 9 /// 10 /// This file provides the implementation of the PassBuilder based on our 11 /// static pass registry as well as related functionality. It also provides 12 /// helpers to aid in analyzing, debugging, and testing passes and pass 13 /// pipelines. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Passes/PassBuilder.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/Analysis/AliasAnalysisEvaluator.h" 20 #include "llvm/Analysis/AliasSetTracker.h" 21 #include "llvm/Analysis/AssumptionCache.h" 22 #include "llvm/Analysis/BasicAliasAnalysis.h" 23 #include "llvm/Analysis/BlockFrequencyInfo.h" 24 #include "llvm/Analysis/BranchProbabilityInfo.h" 25 #include "llvm/Analysis/CFGPrinter.h" 26 #include "llvm/Analysis/CFLAndersAliasAnalysis.h" 27 #include "llvm/Analysis/CFLSteensAliasAnalysis.h" 28 #include "llvm/Analysis/CGSCCPassManager.h" 29 #include "llvm/Analysis/CallGraph.h" 30 #include "llvm/Analysis/DDG.h" 31 #include "llvm/Analysis/DDGPrinter.h" 32 #include "llvm/Analysis/Delinearization.h" 33 #include "llvm/Analysis/DemandedBits.h" 34 #include "llvm/Analysis/DependenceAnalysis.h" 35 #include "llvm/Analysis/DivergenceAnalysis.h" 36 #include "llvm/Analysis/DominanceFrontier.h" 37 #include "llvm/Analysis/FunctionPropertiesAnalysis.h" 38 #include "llvm/Analysis/GlobalsModRef.h" 39 #include "llvm/Analysis/IRSimilarityIdentifier.h" 40 #include "llvm/Analysis/IVUsers.h" 41 #include "llvm/Analysis/InlineAdvisor.h" 42 #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h" 43 #include "llvm/Analysis/InstCount.h" 44 #include "llvm/Analysis/LazyCallGraph.h" 45 #include "llvm/Analysis/LazyValueInfo.h" 46 #include "llvm/Analysis/Lint.h" 47 #include "llvm/Analysis/LoopAccessAnalysis.h" 48 #include "llvm/Analysis/LoopCacheAnalysis.h" 49 #include "llvm/Analysis/LoopInfo.h" 50 #include "llvm/Analysis/LoopNestAnalysis.h" 51 #include "llvm/Analysis/MemDerefPrinter.h" 52 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 53 #include "llvm/Analysis/MemorySSA.h" 54 #include "llvm/Analysis/ModuleDebugInfoPrinter.h" 55 #include "llvm/Analysis/ModuleSummaryAnalysis.h" 56 #include "llvm/Analysis/MustExecute.h" 57 #include "llvm/Analysis/ObjCARCAliasAnalysis.h" 58 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 59 #include "llvm/Analysis/PhiValues.h" 60 #include "llvm/Analysis/PostDominators.h" 61 #include "llvm/Analysis/ProfileSummaryInfo.h" 62 #include "llvm/Analysis/RegionInfo.h" 63 #include "llvm/Analysis/ScalarEvolution.h" 64 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 65 #include "llvm/Analysis/ScopedNoAliasAA.h" 66 #include "llvm/Analysis/StackLifetime.h" 67 #include "llvm/Analysis/StackSafetyAnalysis.h" 68 #include "llvm/Analysis/TargetLibraryInfo.h" 69 #include "llvm/Analysis/TargetTransformInfo.h" 70 #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 71 #include "llvm/IR/Dominators.h" 72 #include "llvm/IR/IRPrintingPasses.h" 73 #include "llvm/IR/PassManager.h" 74 #include "llvm/IR/PrintPasses.h" 75 #include "llvm/IR/SafepointIRVerifier.h" 76 #include "llvm/IR/Verifier.h" 77 #include "llvm/Support/CommandLine.h" 78 #include "llvm/Support/Debug.h" 79 #include "llvm/Support/ErrorHandling.h" 80 #include "llvm/Support/FormatVariadic.h" 81 #include "llvm/Support/Regex.h" 82 #include "llvm/Target/TargetMachine.h" 83 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" 84 #include "llvm/Transforms/Coroutines/CoroCleanup.h" 85 #include "llvm/Transforms/Coroutines/CoroEarly.h" 86 #include "llvm/Transforms/Coroutines/CoroElide.h" 87 #include "llvm/Transforms/Coroutines/CoroSplit.h" 88 #include "llvm/Transforms/IPO/AlwaysInliner.h" 89 #include "llvm/Transforms/IPO/Annotation2Metadata.h" 90 #include "llvm/Transforms/IPO/ArgumentPromotion.h" 91 #include "llvm/Transforms/IPO/Attributor.h" 92 #include "llvm/Transforms/IPO/BlockExtractor.h" 93 #include "llvm/Transforms/IPO/CalledValuePropagation.h" 94 #include "llvm/Transforms/IPO/ConstantMerge.h" 95 #include "llvm/Transforms/IPO/CrossDSOCFI.h" 96 #include "llvm/Transforms/IPO/DeadArgumentElimination.h" 97 #include "llvm/Transforms/IPO/ElimAvailExtern.h" 98 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 99 #include "llvm/Transforms/IPO/FunctionAttrs.h" 100 #include "llvm/Transforms/IPO/FunctionImport.h" 101 #include "llvm/Transforms/IPO/GlobalDCE.h" 102 #include "llvm/Transforms/IPO/GlobalOpt.h" 103 #include "llvm/Transforms/IPO/GlobalSplit.h" 104 #include "llvm/Transforms/IPO/HotColdSplitting.h" 105 #include "llvm/Transforms/IPO/IROutliner.h" 106 #include "llvm/Transforms/IPO/InferFunctionAttrs.h" 107 #include "llvm/Transforms/IPO/Inliner.h" 108 #include "llvm/Transforms/IPO/Internalize.h" 109 #include "llvm/Transforms/IPO/LoopExtractor.h" 110 #include "llvm/Transforms/IPO/LowerTypeTests.h" 111 #include "llvm/Transforms/IPO/MergeFunctions.h" 112 #include "llvm/Transforms/IPO/OpenMPOpt.h" 113 #include "llvm/Transforms/IPO/PartialInlining.h" 114 #include "llvm/Transforms/IPO/SCCP.h" 115 #include "llvm/Transforms/IPO/SampleProfile.h" 116 #include "llvm/Transforms/IPO/SampleProfileProbe.h" 117 #include "llvm/Transforms/IPO/StripDeadPrototypes.h" 118 #include "llvm/Transforms/IPO/StripSymbols.h" 119 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" 120 #include "llvm/Transforms/IPO/WholeProgramDevirt.h" 121 #include "llvm/Transforms/InstCombine/InstCombine.h" 122 #include "llvm/Transforms/Instrumentation.h" 123 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" 124 #include "llvm/Transforms/Instrumentation/BoundsChecking.h" 125 #include "llvm/Transforms/Instrumentation/CGProfile.h" 126 #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h" 127 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h" 128 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" 129 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h" 130 #include "llvm/Transforms/Instrumentation/InstrOrderFile.h" 131 #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 132 #include "llvm/Transforms/Instrumentation/MemProfiler.h" 133 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" 134 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 135 #include "llvm/Transforms/Instrumentation/PoisonChecking.h" 136 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h" 137 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" 138 #include "llvm/Transforms/ObjCARC.h" 139 #include "llvm/Transforms/Scalar/ADCE.h" 140 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" 141 #include "llvm/Transforms/Scalar/AnnotationRemarks.h" 142 #include "llvm/Transforms/Scalar/BDCE.h" 143 #include "llvm/Transforms/Scalar/CallSiteSplitting.h" 144 #include "llvm/Transforms/Scalar/ConstantHoisting.h" 145 #include "llvm/Transforms/Scalar/ConstraintElimination.h" 146 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" 147 #include "llvm/Transforms/Scalar/DCE.h" 148 #include "llvm/Transforms/Scalar/DFAJumpThreading.h" 149 #include "llvm/Transforms/Scalar/DeadStoreElimination.h" 150 #include "llvm/Transforms/Scalar/DivRemPairs.h" 151 #include "llvm/Transforms/Scalar/EarlyCSE.h" 152 #include "llvm/Transforms/Scalar/Float2Int.h" 153 #include "llvm/Transforms/Scalar/GVN.h" 154 #include "llvm/Transforms/Scalar/GuardWidening.h" 155 #include "llvm/Transforms/Scalar/IVUsersPrinter.h" 156 #include "llvm/Transforms/Scalar/IndVarSimplify.h" 157 #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h" 158 #include "llvm/Transforms/Scalar/InferAddressSpaces.h" 159 #include "llvm/Transforms/Scalar/InstSimplifyPass.h" 160 #include "llvm/Transforms/Scalar/JumpThreading.h" 161 #include "llvm/Transforms/Scalar/LICM.h" 162 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h" 163 #include "llvm/Transforms/Scalar/LoopBoundSplit.h" 164 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h" 165 #include "llvm/Transforms/Scalar/LoopDeletion.h" 166 #include "llvm/Transforms/Scalar/LoopDistribute.h" 167 #include "llvm/Transforms/Scalar/LoopFlatten.h" 168 #include "llvm/Transforms/Scalar/LoopFuse.h" 169 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" 170 #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 171 #include "llvm/Transforms/Scalar/LoopInterchange.h" 172 #include "llvm/Transforms/Scalar/LoopLoadElimination.h" 173 #include "llvm/Transforms/Scalar/LoopPassManager.h" 174 #include "llvm/Transforms/Scalar/LoopPredication.h" 175 #include "llvm/Transforms/Scalar/LoopReroll.h" 176 #include "llvm/Transforms/Scalar/LoopRotation.h" 177 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 178 #include "llvm/Transforms/Scalar/LoopSink.h" 179 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h" 180 #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" 181 #include "llvm/Transforms/Scalar/LoopUnrollPass.h" 182 #include "llvm/Transforms/Scalar/LoopVersioningLICM.h" 183 #include "llvm/Transforms/Scalar/LowerAtomic.h" 184 #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h" 185 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 186 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h" 187 #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h" 188 #include "llvm/Transforms/Scalar/LowerWidenableCondition.h" 189 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h" 190 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" 191 #include "llvm/Transforms/Scalar/MergeICmps.h" 192 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" 193 #include "llvm/Transforms/Scalar/NaryReassociate.h" 194 #include "llvm/Transforms/Scalar/NewGVN.h" 195 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h" 196 #include "llvm/Transforms/Scalar/Reassociate.h" 197 #include "llvm/Transforms/Scalar/Reg2Mem.h" 198 #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h" 199 #include "llvm/Transforms/Scalar/SCCP.h" 200 #include "llvm/Transforms/Scalar/SROA.h" 201 #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h" 202 #include "llvm/Transforms/Scalar/Scalarizer.h" 203 #include "llvm/Transforms/Scalar/SeparateConstOffsetFromGEP.h" 204 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 205 #include "llvm/Transforms/Scalar/SimplifyCFG.h" 206 #include "llvm/Transforms/Scalar/Sink.h" 207 #include "llvm/Transforms/Scalar/SpeculativeExecution.h" 208 #include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h" 209 #include "llvm/Transforms/Scalar/StructurizeCFG.h" 210 #include "llvm/Transforms/Scalar/TailRecursionElimination.h" 211 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" 212 #include "llvm/Transforms/Utils/AddDiscriminators.h" 213 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 214 #include "llvm/Transforms/Utils/BreakCriticalEdges.h" 215 #include "llvm/Transforms/Utils/CanonicalizeAliases.h" 216 #include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h" 217 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h" 218 #include "llvm/Transforms/Utils/FixIrreducible.h" 219 #include "llvm/Transforms/Utils/HelloWorld.h" 220 #include "llvm/Transforms/Utils/InjectTLIMappings.h" 221 #include "llvm/Transforms/Utils/InstructionNamer.h" 222 #include "llvm/Transforms/Utils/LCSSA.h" 223 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" 224 #include "llvm/Transforms/Utils/LoopSimplify.h" 225 #include "llvm/Transforms/Utils/LoopVersioning.h" 226 #include "llvm/Transforms/Utils/LowerInvoke.h" 227 #include "llvm/Transforms/Utils/LowerSwitch.h" 228 #include "llvm/Transforms/Utils/Mem2Reg.h" 229 #include "llvm/Transforms/Utils/MetaRenamer.h" 230 #include "llvm/Transforms/Utils/NameAnonGlobals.h" 231 #include "llvm/Transforms/Utils/RelLookupTableConverter.h" 232 #include "llvm/Transforms/Utils/StripGCRelocates.h" 233 #include "llvm/Transforms/Utils/StripNonLineTableDebugInfo.h" 234 #include "llvm/Transforms/Utils/SymbolRewriter.h" 235 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" 236 #include "llvm/Transforms/Utils/UnifyLoopExits.h" 237 #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h" 238 #include "llvm/Transforms/Vectorize/LoopVectorize.h" 239 #include "llvm/Transforms/Vectorize/SLPVectorizer.h" 240 #include "llvm/Transforms/Vectorize/VectorCombine.h" 241 242 using namespace llvm; 243 244 static cl::opt<InliningAdvisorMode> UseInlineAdvisor( 245 "enable-ml-inliner", cl::init(InliningAdvisorMode::Default), cl::Hidden, 246 cl::desc("Enable ML policy for inliner. Currently trained for -Oz only"), 247 cl::values(clEnumValN(InliningAdvisorMode::Default, "default", 248 "Heuristics-based inliner version."), 249 clEnumValN(InliningAdvisorMode::Development, "development", 250 "Use development mode (runtime-loadable model)."), 251 clEnumValN(InliningAdvisorMode::Release, "release", 252 "Use release mode (AOT-compiled model)."))); 253 254 static cl::opt<bool> EnableSyntheticCounts( 255 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore, 256 cl::desc("Run synthetic function entry count generation " 257 "pass")); 258 259 static const Regex DefaultAliasRegex( 260 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$"); 261 262 /// Flag to enable inline deferral during PGO. 263 static cl::opt<bool> 264 EnablePGOInlineDeferral("enable-npm-pgo-inline-deferral", cl::init(true), 265 cl::Hidden, 266 cl::desc("Enable inline deferral during PGO")); 267 268 static cl::opt<bool> EnableMemProfiler("enable-mem-prof", cl::init(false), 269 cl::Hidden, cl::ZeroOrMore, 270 cl::desc("Enable memory profiler")); 271 272 static cl::opt<bool> PerformMandatoryInliningsFirst( 273 "mandatory-inlining-first", cl::init(true), cl::Hidden, cl::ZeroOrMore, 274 cl::desc("Perform mandatory inlinings module-wide, before performing " 275 "inlining.")); 276 277 static cl::opt<bool> EnableO3NonTrivialUnswitching( 278 "enable-npm-O3-nontrivial-unswitch", cl::init(true), cl::Hidden, 279 cl::ZeroOrMore, cl::desc("Enable non-trivial loop unswitching for -O3")); 280 281 PipelineTuningOptions::PipelineTuningOptions() { 282 LoopInterleaving = true; 283 LoopVectorization = true; 284 SLPVectorization = false; 285 LoopUnrolling = true; 286 ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll; 287 LicmMssaOptCap = SetLicmMssaOptCap; 288 LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; 289 CallGraphProfile = true; 290 MergeFunctions = false; 291 } 292 293 namespace llvm { 294 extern cl::opt<unsigned> MaxDevirtIterations; 295 extern cl::opt<bool> EnableConstraintElimination; 296 extern cl::opt<bool> EnableFunctionSpecialization; 297 extern cl::opt<bool> EnableGVNHoist; 298 extern cl::opt<bool> EnableGVNSink; 299 extern cl::opt<bool> EnableHotColdSplit; 300 extern cl::opt<bool> EnableIROutliner; 301 extern cl::opt<bool> EnableOrderFileInstrumentation; 302 extern cl::opt<bool> EnableCHR; 303 extern cl::opt<bool> EnableLoopInterchange; 304 extern cl::opt<bool> EnableUnrollAndJam; 305 extern cl::opt<bool> EnableLoopFlatten; 306 extern cl::opt<bool> EnableDFAJumpThreading; 307 extern cl::opt<bool> RunNewGVN; 308 extern cl::opt<bool> RunPartialInlining; 309 extern cl::opt<bool> ExtraVectorizerPasses; 310 311 extern cl::opt<bool> FlattenedProfileUsed; 312 313 extern cl::opt<AttributorRunOption> AttributorRun; 314 extern cl::opt<bool> EnableKnowledgeRetention; 315 316 extern cl::opt<bool> EnableMatrix; 317 318 extern cl::opt<bool> DisablePreInliner; 319 extern cl::opt<int> PreInlineThreshold; 320 } // namespace llvm 321 322 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::O0 = { 323 /*SpeedLevel*/ 0, 324 /*SizeLevel*/ 0}; 325 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::O1 = { 326 /*SpeedLevel*/ 1, 327 /*SizeLevel*/ 0}; 328 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::O2 = { 329 /*SpeedLevel*/ 2, 330 /*SizeLevel*/ 0}; 331 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::O3 = { 332 /*SpeedLevel*/ 3, 333 /*SizeLevel*/ 0}; 334 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::Os = { 335 /*SpeedLevel*/ 2, 336 /*SizeLevel*/ 1}; 337 const PassBuilder::OptimizationLevel PassBuilder::OptimizationLevel::Oz = { 338 /*SpeedLevel*/ 2, 339 /*SizeLevel*/ 2}; 340 341 namespace { 342 343 // The following passes/analyses have custom names, otherwise their name will 344 // include `(anonymous namespace)`. These are special since they are only for 345 // testing purposes and don't live in a header file. 346 347 /// No-op module pass which does nothing. 348 struct NoOpModulePass : PassInfoMixin<NoOpModulePass> { 349 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { 350 return PreservedAnalyses::all(); 351 } 352 353 static StringRef name() { return "NoOpModulePass"; } 354 }; 355 356 /// No-op module analysis. 357 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> { 358 friend AnalysisInfoMixin<NoOpModuleAnalysis>; 359 static AnalysisKey Key; 360 361 public: 362 struct Result {}; 363 Result run(Module &, ModuleAnalysisManager &) { return Result(); } 364 static StringRef name() { return "NoOpModuleAnalysis"; } 365 }; 366 367 /// No-op CGSCC pass which does nothing. 368 struct NoOpCGSCCPass : PassInfoMixin<NoOpCGSCCPass> { 369 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &, 370 LazyCallGraph &, CGSCCUpdateResult &UR) { 371 return PreservedAnalyses::all(); 372 } 373 static StringRef name() { return "NoOpCGSCCPass"; } 374 }; 375 376 /// No-op CGSCC analysis. 377 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> { 378 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>; 379 static AnalysisKey Key; 380 381 public: 382 struct Result {}; 383 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) { 384 return Result(); 385 } 386 static StringRef name() { return "NoOpCGSCCAnalysis"; } 387 }; 388 389 /// No-op function pass which does nothing. 390 struct NoOpFunctionPass : PassInfoMixin<NoOpFunctionPass> { 391 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) { 392 return PreservedAnalyses::all(); 393 } 394 static StringRef name() { return "NoOpFunctionPass"; } 395 }; 396 397 /// No-op function analysis. 398 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> { 399 friend AnalysisInfoMixin<NoOpFunctionAnalysis>; 400 static AnalysisKey Key; 401 402 public: 403 struct Result {}; 404 Result run(Function &, FunctionAnalysisManager &) { return Result(); } 405 static StringRef name() { return "NoOpFunctionAnalysis"; } 406 }; 407 408 /// No-op loop pass which does nothing. 409 struct NoOpLoopPass : PassInfoMixin<NoOpLoopPass> { 410 PreservedAnalyses run(Loop &L, LoopAnalysisManager &, 411 LoopStandardAnalysisResults &, LPMUpdater &) { 412 return PreservedAnalyses::all(); 413 } 414 static StringRef name() { return "NoOpLoopPass"; } 415 }; 416 417 /// No-op loop analysis. 418 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> { 419 friend AnalysisInfoMixin<NoOpLoopAnalysis>; 420 static AnalysisKey Key; 421 422 public: 423 struct Result {}; 424 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) { 425 return Result(); 426 } 427 static StringRef name() { return "NoOpLoopAnalysis"; } 428 }; 429 430 AnalysisKey NoOpModuleAnalysis::Key; 431 AnalysisKey NoOpCGSCCAnalysis::Key; 432 AnalysisKey NoOpFunctionAnalysis::Key; 433 AnalysisKey NoOpLoopAnalysis::Key; 434 435 /// Whether or not we should populate a PassInstrumentationCallbacks's class to 436 /// pass name map. 437 /// 438 /// This is for optimization purposes so we don't populate it if we never use 439 /// it. This should be updated if new pass instrumentation wants to use the map. 440 /// We currently only use this for --print-before/after. 441 bool shouldPopulateClassToPassNames() { 442 return !printBeforePasses().empty() || !printAfterPasses().empty(); 443 } 444 445 } // namespace 446 447 PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO, 448 Optional<PGOOptions> PGOOpt, 449 PassInstrumentationCallbacks *PIC) 450 : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) { 451 if (TM) 452 TM->registerPassBuilderCallbacks(*this); 453 if (PIC && shouldPopulateClassToPassNames()) { 454 #define MODULE_PASS(NAME, CREATE_PASS) \ 455 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 456 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ 457 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 458 #define FUNCTION_PASS(NAME, CREATE_PASS) \ 459 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 460 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 461 PIC->addClassToPassName(CLASS, NAME); 462 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ 463 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 464 #define LOOP_PASS(NAME, CREATE_PASS) \ 465 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 466 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 467 PIC->addClassToPassName(CLASS, NAME); 468 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ 469 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 470 #define CGSCC_PASS(NAME, CREATE_PASS) \ 471 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 472 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ 473 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME); 474 #include "PassRegistry.def" 475 } 476 } 477 478 void PassBuilder::invokePeepholeEPCallbacks( 479 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { 480 for (auto &C : PeepholeEPCallbacks) 481 C(FPM, Level); 482 } 483 484 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) { 485 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ 486 MAM.registerPass([&] { return CREATE_PASS; }); 487 #include "PassRegistry.def" 488 489 for (auto &C : ModuleAnalysisRegistrationCallbacks) 490 C(MAM); 491 } 492 493 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) { 494 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ 495 CGAM.registerPass([&] { return CREATE_PASS; }); 496 #include "PassRegistry.def" 497 498 for (auto &C : CGSCCAnalysisRegistrationCallbacks) 499 C(CGAM); 500 } 501 502 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) { 503 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ 504 FAM.registerPass([&] { return CREATE_PASS; }); 505 #include "PassRegistry.def" 506 507 for (auto &C : FunctionAnalysisRegistrationCallbacks) 508 C(FAM); 509 } 510 511 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) { 512 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ 513 LAM.registerPass([&] { return CREATE_PASS; }); 514 #include "PassRegistry.def" 515 516 for (auto &C : LoopAnalysisRegistrationCallbacks) 517 C(LAM); 518 } 519 520 // Helper to add AnnotationRemarksPass. 521 static void addAnnotationRemarksPass(ModulePassManager &MPM) { 522 FunctionPassManager FPM; 523 FPM.addPass(AnnotationRemarksPass()); 524 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 525 } 526 527 // Helper to check if the current compilation phase is preparing for LTO 528 static bool isLTOPreLink(ThinOrFullLTOPhase Phase) { 529 return Phase == ThinOrFullLTOPhase::ThinLTOPreLink || 530 Phase == ThinOrFullLTOPhase::FullLTOPreLink; 531 } 532 533 // TODO: Investigate the cost/benefit of tail call elimination on debugging. 534 FunctionPassManager 535 PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, 536 ThinOrFullLTOPhase Phase) { 537 538 FunctionPassManager FPM; 539 540 // Form SSA out of local memory accesses after breaking apart aggregates into 541 // scalars. 542 FPM.addPass(SROA()); 543 544 // Catch trivial redundancies 545 FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 546 547 // Hoisting of scalars and load expressions. 548 FPM.addPass(SimplifyCFGPass()); 549 FPM.addPass(InstCombinePass()); 550 551 FPM.addPass(LibCallsShrinkWrapPass()); 552 553 invokePeepholeEPCallbacks(FPM, Level); 554 555 FPM.addPass(SimplifyCFGPass()); 556 557 // Form canonically associated expression trees, and simplify the trees using 558 // basic mathematical properties. For example, this will form (nearly) 559 // minimal multiplication trees. 560 FPM.addPass(ReassociatePass()); 561 562 // Add the primary loop simplification pipeline. 563 // FIXME: Currently this is split into two loop pass pipelines because we run 564 // some function passes in between them. These can and should be removed 565 // and/or replaced by scheduling the loop pass equivalents in the correct 566 // positions. But those equivalent passes aren't powerful enough yet. 567 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 568 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 569 // fully replace `SimplifyCFGPass`, and the closest to the other we have is 570 // `LoopInstSimplify`. 571 LoopPassManager LPM1, LPM2; 572 573 // Simplify the loop body. We do this initially to clean up after other loop 574 // passes run, either when iterating on a loop or on inner loops with 575 // implications on the outer loop. 576 LPM1.addPass(LoopInstSimplifyPass()); 577 LPM1.addPass(LoopSimplifyCFGPass()); 578 579 // Try to remove as much code from the loop header as possible, 580 // to reduce amount of IR that will have to be duplicated. 581 // TODO: Investigate promotion cap for O1. 582 LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 583 584 LPM1.addPass(LoopRotatePass(/* Disable header duplication */ true, 585 isLTOPreLink(Phase))); 586 // TODO: Investigate promotion cap for O1. 587 LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 588 LPM1.addPass(SimpleLoopUnswitchPass()); 589 590 LPM2.addPass(LoopIdiomRecognizePass()); 591 LPM2.addPass(IndVarSimplifyPass()); 592 593 for (auto &C : LateLoopOptimizationsEPCallbacks) 594 C(LPM2, Level); 595 596 LPM2.addPass(LoopDeletionPass()); 597 598 if (EnableLoopInterchange) 599 LPM2.addPass(LoopInterchangePass()); 600 601 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 602 // because it changes IR to makes profile annotation in back compile 603 // inaccurate. The normal unroller doesn't pay attention to forced full unroll 604 // attributes so we need to make sure and allow the full unroll pass to pay 605 // attention to it. 606 if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 607 PGOOpt->Action != PGOOptions::SampleUse) 608 LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 609 /* OnlyWhenForced= */ !PTO.LoopUnrolling, 610 PTO.ForgetAllSCEVInLoopUnroll)); 611 612 for (auto &C : LoopOptimizerEndEPCallbacks) 613 C(LPM2, Level); 614 615 // We provide the opt remark emitter pass for LICM to use. We only need to do 616 // this once as it is immutable. 617 FPM.addPass( 618 RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 619 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 620 EnableMSSALoopDependency, 621 /*UseBlockFrequencyInfo=*/true)); 622 FPM.addPass(SimplifyCFGPass()); 623 FPM.addPass(InstCombinePass()); 624 if (EnableLoopFlatten) 625 FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 626 // The loop passes in LPM2 (LoopFullUnrollPass) do not preserve MemorySSA. 627 // *All* loop passes must preserve it, in order to be able to use it. 628 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 629 /*UseMemorySSA=*/false, 630 /*UseBlockFrequencyInfo=*/false)); 631 632 // Delete small array after loop unroll. 633 FPM.addPass(SROA()); 634 635 // Specially optimize memory movement as it doesn't look like dataflow in SSA. 636 FPM.addPass(MemCpyOptPass()); 637 638 // Sparse conditional constant propagation. 639 // FIXME: It isn't clear why we do this *after* loop passes rather than 640 // before... 641 FPM.addPass(SCCPPass()); 642 643 // Delete dead bit computations (instcombine runs after to fold away the dead 644 // computations, and then ADCE will run later to exploit any new DCE 645 // opportunities that creates). 646 FPM.addPass(BDCEPass()); 647 648 // Run instcombine after redundancy and dead bit elimination to exploit 649 // opportunities opened up by them. 650 FPM.addPass(InstCombinePass()); 651 invokePeepholeEPCallbacks(FPM, Level); 652 653 FPM.addPass(CoroElidePass()); 654 655 for (auto &C : ScalarOptimizerLateEPCallbacks) 656 C(FPM, Level); 657 658 // Finally, do an expensive DCE pass to catch all the dead code exposed by 659 // the simplifications and basic cleanup after all the simplifications. 660 // TODO: Investigate if this is too expensive. 661 FPM.addPass(ADCEPass()); 662 FPM.addPass(SimplifyCFGPass()); 663 FPM.addPass(InstCombinePass()); 664 invokePeepholeEPCallbacks(FPM, Level); 665 666 return FPM; 667 } 668 669 FunctionPassManager 670 PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, 671 ThinOrFullLTOPhase Phase) { 672 assert(Level != OptimizationLevel::O0 && "Must request optimizations!"); 673 674 // The O1 pipeline has a separate pipeline creation function to simplify 675 // construction readability. 676 if (Level.getSpeedupLevel() == 1) 677 return buildO1FunctionSimplificationPipeline(Level, Phase); 678 679 FunctionPassManager FPM; 680 681 // Form SSA out of local memory accesses after breaking apart aggregates into 682 // scalars. 683 FPM.addPass(SROA()); 684 685 // Catch trivial redundancies 686 FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 687 if (EnableKnowledgeRetention) 688 FPM.addPass(AssumeSimplifyPass()); 689 690 // Hoisting of scalars and load expressions. 691 if (EnableGVNHoist) 692 FPM.addPass(GVNHoistPass()); 693 694 // Global value numbering based sinking. 695 if (EnableGVNSink) { 696 FPM.addPass(GVNSinkPass()); 697 FPM.addPass(SimplifyCFGPass()); 698 } 699 700 if (EnableConstraintElimination) 701 FPM.addPass(ConstraintEliminationPass()); 702 703 // Speculative execution if the target has divergent branches; otherwise nop. 704 FPM.addPass(SpeculativeExecutionPass(/* OnlyIfDivergentTarget =*/true)); 705 706 // Optimize based on known information about branches, and cleanup afterward. 707 FPM.addPass(JumpThreadingPass()); 708 FPM.addPass(CorrelatedValuePropagationPass()); 709 710 FPM.addPass(SimplifyCFGPass()); 711 if (Level == OptimizationLevel::O3) 712 FPM.addPass(AggressiveInstCombinePass()); 713 FPM.addPass(InstCombinePass()); 714 715 if (!Level.isOptimizingForSize()) 716 FPM.addPass(LibCallsShrinkWrapPass()); 717 718 invokePeepholeEPCallbacks(FPM, Level); 719 720 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy 721 // using the size value profile. Don't perform this when optimizing for size. 722 if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse && 723 !Level.isOptimizingForSize()) 724 FPM.addPass(PGOMemOPSizeOpt()); 725 726 FPM.addPass(TailCallElimPass()); 727 FPM.addPass(SimplifyCFGPass()); 728 729 // Form canonically associated expression trees, and simplify the trees using 730 // basic mathematical properties. For example, this will form (nearly) 731 // minimal multiplication trees. 732 FPM.addPass(ReassociatePass()); 733 734 // Add the primary loop simplification pipeline. 735 // FIXME: Currently this is split into two loop pass pipelines because we run 736 // some function passes in between them. These can and should be removed 737 // and/or replaced by scheduling the loop pass equivalents in the correct 738 // positions. But those equivalent passes aren't powerful enough yet. 739 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 740 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 741 // fully replace `SimplifyCFGPass`, and the closest to the other we have is 742 // `LoopInstSimplify`. 743 LoopPassManager LPM1, LPM2; 744 745 // Simplify the loop body. We do this initially to clean up after other loop 746 // passes run, either when iterating on a loop or on inner loops with 747 // implications on the outer loop. 748 LPM1.addPass(LoopInstSimplifyPass()); 749 LPM1.addPass(LoopSimplifyCFGPass()); 750 751 // Try to remove as much code from the loop header as possible, 752 // to reduce amount of IR that will have to be duplicated. 753 // TODO: Investigate promotion cap for O1. 754 LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 755 756 // Disable header duplication in loop rotation at -Oz. 757 LPM1.addPass( 758 LoopRotatePass(Level != OptimizationLevel::Oz, isLTOPreLink(Phase))); 759 // TODO: Investigate promotion cap for O1. 760 LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 761 LPM1.addPass( 762 SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 && 763 EnableO3NonTrivialUnswitching)); 764 LPM2.addPass(LoopIdiomRecognizePass()); 765 LPM2.addPass(IndVarSimplifyPass()); 766 767 for (auto &C : LateLoopOptimizationsEPCallbacks) 768 C(LPM2, Level); 769 770 LPM2.addPass(LoopDeletionPass()); 771 772 if (EnableLoopInterchange) 773 LPM2.addPass(LoopInterchangePass()); 774 775 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 776 // because it changes IR to makes profile annotation in back compile 777 // inaccurate. The normal unroller doesn't pay attention to forced full unroll 778 // attributes so we need to make sure and allow the full unroll pass to pay 779 // attention to it. 780 if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 781 PGOOpt->Action != PGOOptions::SampleUse) 782 LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 783 /* OnlyWhenForced= */ !PTO.LoopUnrolling, 784 PTO.ForgetAllSCEVInLoopUnroll)); 785 786 for (auto &C : LoopOptimizerEndEPCallbacks) 787 C(LPM2, Level); 788 789 // We provide the opt remark emitter pass for LICM to use. We only need to do 790 // this once as it is immutable. 791 FPM.addPass( 792 RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 793 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 794 EnableMSSALoopDependency, 795 /*UseBlockFrequencyInfo=*/true)); 796 FPM.addPass(SimplifyCFGPass()); 797 FPM.addPass(InstCombinePass()); 798 if (EnableLoopFlatten) 799 FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 800 // The loop passes in LPM2 (LoopIdiomRecognizePass, IndVarSimplifyPass, 801 // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA. 802 // *All* loop passes must preserve it, in order to be able to use it. 803 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 804 /*UseMemorySSA=*/false, 805 /*UseBlockFrequencyInfo=*/false)); 806 807 // Delete small array after loop unroll. 808 FPM.addPass(SROA()); 809 810 // Eliminate redundancies. 811 FPM.addPass(MergedLoadStoreMotionPass()); 812 if (RunNewGVN) 813 FPM.addPass(NewGVNPass()); 814 else 815 FPM.addPass(GVN()); 816 817 // Sparse conditional constant propagation. 818 // FIXME: It isn't clear why we do this *after* loop passes rather than 819 // before... 820 FPM.addPass(SCCPPass()); 821 822 // Delete dead bit computations (instcombine runs after to fold away the dead 823 // computations, and then ADCE will run later to exploit any new DCE 824 // opportunities that creates). 825 FPM.addPass(BDCEPass()); 826 827 // Run instcombine after redundancy and dead bit elimination to exploit 828 // opportunities opened up by them. 829 FPM.addPass(InstCombinePass()); 830 invokePeepholeEPCallbacks(FPM, Level); 831 832 // Re-consider control flow based optimizations after redundancy elimination, 833 // redo DCE, etc. 834 if (EnableDFAJumpThreading && Level.getSizeLevel() == 0) 835 FPM.addPass(DFAJumpThreadingPass()); 836 837 FPM.addPass(JumpThreadingPass()); 838 FPM.addPass(CorrelatedValuePropagationPass()); 839 840 // Finally, do an expensive DCE pass to catch all the dead code exposed by 841 // the simplifications and basic cleanup after all the simplifications. 842 // TODO: Investigate if this is too expensive. 843 FPM.addPass(ADCEPass()); 844 845 // Specially optimize memory movement as it doesn't look like dataflow in SSA. 846 FPM.addPass(MemCpyOptPass()); 847 848 FPM.addPass(DSEPass()); 849 FPM.addPass(createFunctionToLoopPassAdaptor( 850 LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 851 EnableMSSALoopDependency, /*UseBlockFrequencyInfo=*/true)); 852 853 FPM.addPass(CoroElidePass()); 854 855 for (auto &C : ScalarOptimizerLateEPCallbacks) 856 C(FPM, Level); 857 858 FPM.addPass(SimplifyCFGPass( 859 SimplifyCFGOptions().hoistCommonInsts(true).sinkCommonInsts(true))); 860 FPM.addPass(InstCombinePass()); 861 invokePeepholeEPCallbacks(FPM, Level); 862 863 if (EnableCHR && Level == OptimizationLevel::O3 && PGOOpt && 864 (PGOOpt->Action == PGOOptions::IRUse || 865 PGOOpt->Action == PGOOptions::SampleUse)) 866 FPM.addPass(ControlHeightReductionPass()); 867 868 return FPM; 869 } 870 871 void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) { 872 MPM.addPass(CanonicalizeAliasesPass()); 873 MPM.addPass(NameAnonGlobalPass()); 874 } 875 876 void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, 877 PassBuilder::OptimizationLevel Level, 878 bool RunProfileGen, bool IsCS, 879 std::string ProfileFile, 880 std::string ProfileRemappingFile) { 881 assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!"); 882 if (!IsCS && !DisablePreInliner) { 883 InlineParams IP; 884 885 IP.DefaultThreshold = PreInlineThreshold; 886 887 // FIXME: The hint threshold has the same value used by the regular inliner 888 // when not optimzing for size. This should probably be lowered after 889 // performance testing. 890 // FIXME: this comment is cargo culted from the old pass manager, revisit). 891 IP.HintThreshold = Level.isOptimizingForSize() ? PreInlineThreshold : 325; 892 ModuleInlinerWrapperPass MIWP(IP); 893 CGSCCPassManager &CGPipeline = MIWP.getPM(); 894 895 FunctionPassManager FPM; 896 FPM.addPass(SROA()); 897 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies. 898 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks. 899 FPM.addPass(InstCombinePass()); // Combine silly sequences. 900 invokePeepholeEPCallbacks(FPM, Level); 901 902 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM))); 903 904 MPM.addPass(std::move(MIWP)); 905 906 // Delete anything that is now dead to make sure that we don't instrument 907 // dead code. Instrumentation can end up keeping dead code around and 908 // dramatically increase code size. 909 MPM.addPass(GlobalDCEPass()); 910 } 911 912 if (!RunProfileGen) { 913 assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 914 MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 915 // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 916 // RequireAnalysisPass for PSI before subsequent non-module passes. 917 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 918 return; 919 } 920 921 // Perform PGO instrumentation. 922 MPM.addPass(PGOInstrumentationGen(IsCS)); 923 924 FunctionPassManager FPM; 925 // Disable header duplication in loop rotation at -Oz. 926 FPM.addPass(createFunctionToLoopPassAdaptor( 927 LoopRotatePass(Level != OptimizationLevel::Oz), EnableMSSALoopDependency, 928 /*UseBlockFrequencyInfo=*/false)); 929 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 930 931 // Add the profile lowering pass. 932 InstrProfOptions Options; 933 if (!ProfileFile.empty()) 934 Options.InstrProfileOutput = ProfileFile; 935 // Do counter promotion at Level greater than O0. 936 Options.DoCounterPromotion = true; 937 Options.UseBFIInPromotion = IsCS; 938 MPM.addPass(InstrProfiling(Options, IsCS)); 939 } 940 941 void PassBuilder::addPGOInstrPassesForO0(ModulePassManager &MPM, 942 bool RunProfileGen, bool IsCS, 943 std::string ProfileFile, 944 std::string ProfileRemappingFile) { 945 if (!RunProfileGen) { 946 assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 947 MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 948 // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 949 // RequireAnalysisPass for PSI before subsequent non-module passes. 950 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 951 return; 952 } 953 954 // Perform PGO instrumentation. 955 MPM.addPass(PGOInstrumentationGen(IsCS)); 956 // Add the profile lowering pass. 957 InstrProfOptions Options; 958 if (!ProfileFile.empty()) 959 Options.InstrProfileOutput = ProfileFile; 960 // Do not do counter promotion at O0. 961 Options.DoCounterPromotion = false; 962 Options.UseBFIInPromotion = IsCS; 963 MPM.addPass(InstrProfiling(Options, IsCS)); 964 } 965 966 static InlineParams 967 getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) { 968 return getInlineParams(Level.getSpeedupLevel(), Level.getSizeLevel()); 969 } 970 971 ModuleInlinerWrapperPass 972 PassBuilder::buildInlinerPipeline(OptimizationLevel Level, 973 ThinOrFullLTOPhase Phase) { 974 InlineParams IP = getInlineParamsFromOptLevel(Level); 975 if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 976 PGOOpt->Action == PGOOptions::SampleUse) 977 IP.HotCallSiteThreshold = 0; 978 979 if (PGOOpt) 980 IP.EnableDeferral = EnablePGOInlineDeferral; 981 982 ModuleInlinerWrapperPass MIWP(IP, PerformMandatoryInliningsFirst, 983 UseInlineAdvisor, MaxDevirtIterations); 984 985 // Require the GlobalsAA analysis for the module so we can query it within 986 // the CGSCC pipeline. 987 MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>()); 988 // Invalidate AAManager so it can be recreated and pick up the newly available 989 // GlobalsAA. 990 MIWP.addModulePass( 991 createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 992 993 // Require the ProfileSummaryAnalysis for the module so we can query it within 994 // the inliner pass. 995 MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 996 997 // Now begin the main postorder CGSCC pipeline. 998 // FIXME: The current CGSCC pipeline has its origins in the legacy pass 999 // manager and trying to emulate its precise behavior. Much of this doesn't 1000 // make a lot of sense and we should revisit the core CGSCC structure. 1001 CGSCCPassManager &MainCGPipeline = MIWP.getPM(); 1002 1003 // Note: historically, the PruneEH pass was run first to deduce nounwind and 1004 // generally clean up exception handling overhead. It isn't clear this is 1005 // valuable as the inliner doesn't currently care whether it is inlining an 1006 // invoke or a call. 1007 1008 if (AttributorRun & AttributorRunOption::CGSCC) 1009 MainCGPipeline.addPass(AttributorCGSCCPass()); 1010 1011 // Now deduce any function attributes based in the current code. 1012 MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); 1013 1014 // When at O3 add argument promotion to the pass pipeline. 1015 // FIXME: It isn't at all clear why this should be limited to O3. 1016 if (Level == OptimizationLevel::O3) 1017 MainCGPipeline.addPass(ArgumentPromotionPass()); 1018 1019 // Try to perform OpenMP specific optimizations. This is a (quick!) no-op if 1020 // there are no OpenMP runtime calls present in the module. 1021 if (Level == OptimizationLevel::O2 || Level == OptimizationLevel::O3) 1022 MainCGPipeline.addPass(OpenMPOptCGSCCPass()); 1023 1024 for (auto &C : CGSCCOptimizerLateEPCallbacks) 1025 C(MainCGPipeline, Level); 1026 1027 // Lastly, add the core function simplification pipeline nested inside the 1028 // CGSCC walk. 1029 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 1030 buildFunctionSimplificationPipeline(Level, Phase))); 1031 1032 MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0)); 1033 1034 return MIWP; 1035 } 1036 1037 ModulePassManager 1038 PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, 1039 ThinOrFullLTOPhase Phase) { 1040 ModulePassManager MPM; 1041 1042 // Place pseudo probe instrumentation as the first pass of the pipeline to 1043 // minimize the impact of optimization changes. 1044 if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1045 Phase != ThinOrFullLTOPhase::ThinLTOPostLink) 1046 MPM.addPass(SampleProfileProbePass(TM)); 1047 1048 bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse); 1049 1050 // In ThinLTO mode, when flattened profile is used, all the available 1051 // profile information will be annotated in PreLink phase so there is 1052 // no need to load the profile again in PostLink. 1053 bool LoadSampleProfile = 1054 HasSampleProfile && 1055 !(FlattenedProfileUsed && Phase == ThinOrFullLTOPhase::ThinLTOPostLink); 1056 1057 // During the ThinLTO backend phase we perform early indirect call promotion 1058 // here, before globalopt. Otherwise imported available_externally functions 1059 // look unreferenced and are removed. If we are going to load the sample 1060 // profile then defer until later. 1061 // TODO: See if we can move later and consolidate with the location where 1062 // we perform ICP when we are loading a sample profile. 1063 // TODO: We pass HasSampleProfile (whether there was a sample profile file 1064 // passed to the compile) to the SamplePGO flag of ICP. This is used to 1065 // determine whether the new direct calls are annotated with prof metadata. 1066 // Ideally this should be determined from whether the IR is annotated with 1067 // sample profile, and not whether the a sample profile was provided on the 1068 // command line. E.g. for flattened profiles where we will not be reloading 1069 // the sample profile in the ThinLTO backend, we ideally shouldn't have to 1070 // provide the sample profile file. 1071 if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink && !LoadSampleProfile) 1072 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile)); 1073 1074 // Do basic inference of function attributes from known properties of system 1075 // libraries and other oracles. 1076 MPM.addPass(InferFunctionAttrsPass()); 1077 1078 // Create an early function pass manager to cleanup the output of the 1079 // frontend. 1080 FunctionPassManager EarlyFPM; 1081 // Lower llvm.expect to metadata before attempting transforms. 1082 // Compare/branch metadata may alter the behavior of passes like SimplifyCFG. 1083 EarlyFPM.addPass(LowerExpectIntrinsicPass()); 1084 EarlyFPM.addPass(SimplifyCFGPass()); 1085 EarlyFPM.addPass(SROA()); 1086 EarlyFPM.addPass(EarlyCSEPass()); 1087 EarlyFPM.addPass(CoroEarlyPass()); 1088 if (Level == OptimizationLevel::O3) 1089 EarlyFPM.addPass(CallSiteSplittingPass()); 1090 1091 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation 1092 // to convert bitcast to direct calls so that they can be inlined during the 1093 // profile annotation prepration step. 1094 // More details about SamplePGO design can be found in: 1095 // https://research.google.com/pubs/pub45290.html 1096 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. 1097 if (LoadSampleProfile) 1098 EarlyFPM.addPass(InstCombinePass()); 1099 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); 1100 1101 if (LoadSampleProfile) { 1102 // Annotate sample profile right after early FPM to ensure freshness of 1103 // the debug info. 1104 MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1105 PGOOpt->ProfileRemappingFile, Phase)); 1106 // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1107 // RequireAnalysisPass for PSI before subsequent non-module passes. 1108 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1109 // Do not invoke ICP in the LTOPrelink phase as it makes it hard 1110 // for the profile annotation to be accurate in the LTO backend. 1111 if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink && 1112 Phase != ThinOrFullLTOPhase::FullLTOPreLink) 1113 // We perform early indirect call promotion here, before globalopt. 1114 // This is important for the ThinLTO backend phase because otherwise 1115 // imported available_externally functions look unreferenced and are 1116 // removed. 1117 MPM.addPass( 1118 PGOIndirectCallPromotion(true /* IsInLTO */, true /* SamplePGO */)); 1119 } 1120 1121 // Try to perform OpenMP specific optimizations on the module. This is a 1122 // (quick!) no-op if there are no OpenMP runtime calls present in the module. 1123 if (Level != OptimizationLevel::O0) 1124 MPM.addPass(OpenMPOptPass()); 1125 1126 if (AttributorRun & AttributorRunOption::MODULE) 1127 MPM.addPass(AttributorPass()); 1128 1129 // Lower type metadata and the type.test intrinsic in the ThinLTO 1130 // post link pipeline after ICP. This is to enable usage of the type 1131 // tests in ICP sequences. 1132 if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink) 1133 MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1134 1135 for (auto &C : PipelineEarlySimplificationEPCallbacks) 1136 C(MPM, Level); 1137 1138 // Specialize functions with IPSCCP. 1139 if (EnableFunctionSpecialization) 1140 MPM.addPass(FunctionSpecializationPass()); 1141 1142 // Interprocedural constant propagation now that basic cleanup has occurred 1143 // and prior to optimizing globals. 1144 // FIXME: This position in the pipeline hasn't been carefully considered in 1145 // years, it should be re-analyzed. 1146 MPM.addPass(IPSCCPPass()); 1147 1148 // Attach metadata to indirect call sites indicating the set of functions 1149 // they may target at run-time. This should follow IPSCCP. 1150 MPM.addPass(CalledValuePropagationPass()); 1151 1152 // Optimize globals to try and fold them into constants. 1153 MPM.addPass(GlobalOptPass()); 1154 1155 // Promote any localized globals to SSA registers. 1156 // FIXME: Should this instead by a run of SROA? 1157 // FIXME: We should probably run instcombine and simplifycfg afterward to 1158 // delete control flows that are dead once globals have been folded to 1159 // constants. 1160 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1161 1162 // Remove any dead arguments exposed by cleanups and constant folding 1163 // globals. 1164 MPM.addPass(DeadArgumentEliminationPass()); 1165 1166 // Create a small function pass pipeline to cleanup after all the global 1167 // optimizations. 1168 FunctionPassManager GlobalCleanupPM; 1169 GlobalCleanupPM.addPass(InstCombinePass()); 1170 invokePeepholeEPCallbacks(GlobalCleanupPM, Level); 1171 1172 GlobalCleanupPM.addPass(SimplifyCFGPass()); 1173 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM))); 1174 1175 // Add all the requested passes for instrumentation PGO, if requested. 1176 if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1177 (PGOOpt->Action == PGOOptions::IRInstr || 1178 PGOOpt->Action == PGOOptions::IRUse)) { 1179 addPGOInstrPasses(MPM, Level, 1180 /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr, 1181 /* IsCS */ false, PGOOpt->ProfileFile, 1182 PGOOpt->ProfileRemappingFile); 1183 MPM.addPass(PGOIndirectCallPromotion(false, false)); 1184 } 1185 if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1186 PGOOpt->CSAction == PGOOptions::CSIRInstr) 1187 MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); 1188 1189 // Synthesize function entry counts for non-PGO compilation. 1190 if (EnableSyntheticCounts && !PGOOpt) 1191 MPM.addPass(SyntheticCountsPropagation()); 1192 1193 MPM.addPass(buildInlinerPipeline(Level, Phase)); 1194 1195 if (EnableMemProfiler && Phase != ThinOrFullLTOPhase::ThinLTOPreLink) { 1196 MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 1197 MPM.addPass(ModuleMemProfilerPass()); 1198 } 1199 1200 return MPM; 1201 } 1202 1203 /// TODO: Should LTO cause any differences to this set of passes? 1204 void PassBuilder::addVectorPasses(OptimizationLevel Level, 1205 FunctionPassManager &FPM, bool IsFullLTO) { 1206 FPM.addPass(LoopVectorizePass( 1207 LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization))); 1208 1209 if (IsFullLTO) { 1210 // The vectorizer may have significantly shortened a loop body; unroll 1211 // again. Unroll small loops to hide loop backedge latency and saturate any 1212 // parallel execution resources of an out-of-order processor. We also then 1213 // need to clean up redundancies and loop invariant code. 1214 // FIXME: It would be really good to use a loop-integrated instruction 1215 // combiner for cleanup here so that the unrolling and LICM can be pipelined 1216 // across the loop nests. 1217 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1218 if (EnableUnrollAndJam && PTO.LoopUnrolling) 1219 FPM.addPass(createFunctionToLoopPassAdaptor( 1220 LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1221 FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1222 Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1223 PTO.ForgetAllSCEVInLoopUnroll))); 1224 FPM.addPass(WarnMissedTransformationsPass()); 1225 } 1226 1227 if (!IsFullLTO) { 1228 // Eliminate loads by forwarding stores from the previous iteration to loads 1229 // of the current iteration. 1230 FPM.addPass(LoopLoadEliminationPass()); 1231 } 1232 // Cleanup after the loop optimization passes. 1233 FPM.addPass(InstCombinePass()); 1234 1235 if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 1236 // At higher optimization levels, try to clean up any runtime overlap and 1237 // alignment checks inserted by the vectorizer. We want to track correlated 1238 // runtime checks for two inner loops in the same outer loop, fold any 1239 // common computations, hoist loop-invariant aspects out of any outer loop, 1240 // and unswitch the runtime checks if possible. Once hoisted, we may have 1241 // dead (or speculatable) control flows or more combining opportunities. 1242 FPM.addPass(EarlyCSEPass()); 1243 FPM.addPass(CorrelatedValuePropagationPass()); 1244 FPM.addPass(InstCombinePass()); 1245 LoopPassManager LPM; 1246 LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 1247 LPM.addPass(SimpleLoopUnswitchPass(/* NonTrivial */ Level == 1248 OptimizationLevel::O3)); 1249 FPM.addPass( 1250 RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 1251 FPM.addPass(createFunctionToLoopPassAdaptor( 1252 std::move(LPM), EnableMSSALoopDependency, 1253 /*UseBlockFrequencyInfo=*/true)); 1254 FPM.addPass(SimplifyCFGPass()); 1255 FPM.addPass(InstCombinePass()); 1256 } 1257 1258 // Now that we've formed fast to execute loop structures, we do further 1259 // optimizations. These are run afterward as they might block doing complex 1260 // analyses and transforms such as what are needed for loop vectorization. 1261 1262 // Cleanup after loop vectorization, etc. Simplification passes like CVP and 1263 // GVN, loop transforms, and others have already run, so it's now better to 1264 // convert to more optimized IR using more aggressive simplify CFG options. 1265 // The extra sinking transform can create larger basic blocks, so do this 1266 // before SLP vectorization. 1267 FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 1268 .forwardSwitchCondToPhi(true) 1269 .convertSwitchToLookupTable(true) 1270 .needCanonicalLoops(false) 1271 .hoistCommonInsts(true) 1272 .sinkCommonInsts(true))); 1273 1274 if (IsFullLTO) { 1275 FPM.addPass(SCCPPass()); 1276 FPM.addPass(InstCombinePass()); 1277 FPM.addPass(BDCEPass()); 1278 } 1279 1280 // Optimize parallel scalar instruction chains into SIMD instructions. 1281 if (PTO.SLPVectorization) { 1282 FPM.addPass(SLPVectorizerPass()); 1283 if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 1284 FPM.addPass(EarlyCSEPass()); 1285 } 1286 } 1287 // Enhance/cleanup vector code. 1288 FPM.addPass(VectorCombinePass()); 1289 1290 if (!IsFullLTO) { 1291 FPM.addPass(InstCombinePass()); 1292 // Unroll small loops to hide loop backedge latency and saturate any 1293 // parallel execution resources of an out-of-order processor. We also then 1294 // need to clean up redundancies and loop invariant code. 1295 // FIXME: It would be really good to use a loop-integrated instruction 1296 // combiner for cleanup here so that the unrolling and LICM can be pipelined 1297 // across the loop nests. 1298 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1299 if (EnableUnrollAndJam && PTO.LoopUnrolling) { 1300 FPM.addPass(createFunctionToLoopPassAdaptor( 1301 LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1302 } 1303 FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1304 Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1305 PTO.ForgetAllSCEVInLoopUnroll))); 1306 FPM.addPass(WarnMissedTransformationsPass()); 1307 FPM.addPass(InstCombinePass()); 1308 FPM.addPass( 1309 RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 1310 FPM.addPass(createFunctionToLoopPassAdaptor( 1311 LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 1312 EnableMSSALoopDependency, /*UseBlockFrequencyInfo=*/true)); 1313 } 1314 1315 // Now that we've vectorized and unrolled loops, we may have more refined 1316 // alignment information, try to re-derive it here. 1317 FPM.addPass(AlignmentFromAssumptionsPass()); 1318 1319 if (IsFullLTO) 1320 FPM.addPass(InstCombinePass()); 1321 } 1322 1323 ModulePassManager 1324 PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level, 1325 bool LTOPreLink) { 1326 ModulePassManager MPM; 1327 1328 // Optimize globals now that the module is fully simplified. 1329 MPM.addPass(GlobalOptPass()); 1330 MPM.addPass(GlobalDCEPass()); 1331 1332 // Run partial inlining pass to partially inline functions that have 1333 // large bodies. 1334 if (RunPartialInlining) 1335 MPM.addPass(PartialInlinerPass()); 1336 1337 // Remove avail extern fns and globals definitions since we aren't compiling 1338 // an object file for later LTO. For LTO we want to preserve these so they 1339 // are eligible for inlining at link-time. Note if they are unreferenced they 1340 // will be removed by GlobalDCE later, so this only impacts referenced 1341 // available externally globals. Eventually they will be suppressed during 1342 // codegen, but eliminating here enables more opportunity for GlobalDCE as it 1343 // may make globals referenced by available external functions dead and saves 1344 // running remaining passes on the eliminated functions. These should be 1345 // preserved during prelinking for link-time inlining decisions. 1346 if (!LTOPreLink) 1347 MPM.addPass(EliminateAvailableExternallyPass()); 1348 1349 if (EnableOrderFileInstrumentation) 1350 MPM.addPass(InstrOrderFilePass()); 1351 1352 // Do RPO function attribute inference across the module to forward-propagate 1353 // attributes where applicable. 1354 // FIXME: Is this really an optimization rather than a canonicalization? 1355 MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1356 1357 // Do a post inline PGO instrumentation and use pass. This is a context 1358 // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as 1359 // cross-module inline has not been done yet. The context sensitive 1360 // instrumentation is after all the inlines are done. 1361 if (!LTOPreLink && PGOOpt) { 1362 if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1363 addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1364 /* IsCS */ true, PGOOpt->CSProfileGenFile, 1365 PGOOpt->ProfileRemappingFile); 1366 else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1367 addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1368 /* IsCS */ true, PGOOpt->ProfileFile, 1369 PGOOpt->ProfileRemappingFile); 1370 } 1371 1372 // Re-require GloblasAA here prior to function passes. This is particularly 1373 // useful as the above will have inlined, DCE'ed, and function-attr 1374 // propagated everything. We should at this point have a reasonably minimal 1375 // and richly annotated call graph. By computing aliasing and mod/ref 1376 // information for all local globals here, the late loop passes and notably 1377 // the vectorizer will be able to use them to help recognize vectorizable 1378 // memory operations. 1379 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1380 1381 FunctionPassManager OptimizePM; 1382 OptimizePM.addPass(Float2IntPass()); 1383 OptimizePM.addPass(LowerConstantIntrinsicsPass()); 1384 1385 if (EnableMatrix) { 1386 OptimizePM.addPass(LowerMatrixIntrinsicsPass()); 1387 OptimizePM.addPass(EarlyCSEPass()); 1388 } 1389 1390 // FIXME: We need to run some loop optimizations to re-rotate loops after 1391 // simplifycfg and others undo their rotation. 1392 1393 // Optimize the loop execution. These passes operate on entire loop nests 1394 // rather than on each loop in an inside-out manner, and so they are actually 1395 // function passes. 1396 1397 for (auto &C : VectorizerStartEPCallbacks) 1398 C(OptimizePM, Level); 1399 1400 // First rotate loops that may have been un-rotated by prior passes. 1401 // Disable header duplication at -Oz. 1402 OptimizePM.addPass(createFunctionToLoopPassAdaptor( 1403 LoopRotatePass(Level != OptimizationLevel::Oz, LTOPreLink), 1404 EnableMSSALoopDependency, 1405 /*UseBlockFrequencyInfo=*/false)); 1406 1407 // Distribute loops to allow partial vectorization. I.e. isolate dependences 1408 // into separate loop that would otherwise inhibit vectorization. This is 1409 // currently only performed for loops marked with the metadata 1410 // llvm.loop.distribute=true or when -enable-loop-distribute is specified. 1411 OptimizePM.addPass(LoopDistributePass()); 1412 1413 // Populates the VFABI attribute with the scalar-to-vector mappings 1414 // from the TargetLibraryInfo. 1415 OptimizePM.addPass(InjectTLIMappings()); 1416 1417 addVectorPasses(Level, OptimizePM, /* IsFullLTO */ false); 1418 1419 // Split out cold code. Splitting is done late to avoid hiding context from 1420 // other optimizations and inadvertently regressing performance. The tradeoff 1421 // is that this has a higher code size cost than splitting early. 1422 if (EnableHotColdSplit && !LTOPreLink) 1423 MPM.addPass(HotColdSplittingPass()); 1424 1425 // Search the code for similar regions of code. If enough similar regions can 1426 // be found where extracting the regions into their own function will decrease 1427 // the size of the program, we extract the regions, a deduplicate the 1428 // structurally similar regions. 1429 if (EnableIROutliner) 1430 MPM.addPass(IROutlinerPass()); 1431 1432 // Merge functions if requested. 1433 if (PTO.MergeFunctions) 1434 MPM.addPass(MergeFunctionsPass()); 1435 1436 // LoopSink pass sinks instructions hoisted by LICM, which serves as a 1437 // canonicalization pass that enables other optimizations. As a result, 1438 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 1439 // result too early. 1440 OptimizePM.addPass(LoopSinkPass()); 1441 1442 // And finally clean up LCSSA form before generating code. 1443 OptimizePM.addPass(InstSimplifyPass()); 1444 1445 // This hoists/decomposes div/rem ops. It should run after other sink/hoist 1446 // passes to avoid re-sinking, but before SimplifyCFG because it can allow 1447 // flattening of blocks. 1448 OptimizePM.addPass(DivRemPairsPass()); 1449 1450 // LoopSink (and other loop passes since the last simplifyCFG) might have 1451 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. 1452 OptimizePM.addPass(SimplifyCFGPass()); 1453 1454 OptimizePM.addPass(CoroCleanupPass()); 1455 1456 // Add the core optimizing pipeline. 1457 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM))); 1458 1459 for (auto &C : OptimizerLastEPCallbacks) 1460 C(MPM, Level); 1461 1462 if (PTO.CallGraphProfile) 1463 MPM.addPass(CGProfilePass()); 1464 1465 // Now we need to do some global optimization transforms. 1466 // FIXME: It would seem like these should come first in the optimization 1467 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird 1468 // ordering here. 1469 MPM.addPass(GlobalDCEPass()); 1470 MPM.addPass(ConstantMergePass()); 1471 1472 // TODO: Relative look table converter pass caused an issue when full lto is 1473 // enabled. See https://reviews.llvm.org/D94355 for more details. 1474 // Until the issue fixed, disable this pass during pre-linking phase. 1475 if (!LTOPreLink) 1476 MPM.addPass(RelLookupTableConverterPass()); 1477 1478 return MPM; 1479 } 1480 1481 ModulePassManager 1482 PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, 1483 bool LTOPreLink) { 1484 assert(Level != OptimizationLevel::O0 && 1485 "Must request optimizations for the default pipeline!"); 1486 1487 ModulePassManager MPM; 1488 1489 // Convert @llvm.global.annotations to !annotation metadata. 1490 MPM.addPass(Annotation2MetadataPass()); 1491 1492 // Force any function attributes we want the rest of the pipeline to observe. 1493 MPM.addPass(ForceFunctionAttrsPass()); 1494 1495 // Apply module pipeline start EP callback. 1496 for (auto &C : PipelineStartEPCallbacks) 1497 C(MPM, Level); 1498 1499 if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1500 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1501 1502 // Add the core simplification pipeline. 1503 MPM.addPass(buildModuleSimplificationPipeline( 1504 Level, LTOPreLink ? ThinOrFullLTOPhase::FullLTOPreLink 1505 : ThinOrFullLTOPhase::None)); 1506 1507 // Now add the optimization pipeline. 1508 MPM.addPass(buildModuleOptimizationPipeline(Level, LTOPreLink)); 1509 1510 if (PGOOpt && PGOOpt->PseudoProbeForProfiling) 1511 MPM.addPass(PseudoProbeUpdatePass()); 1512 1513 // Emit annotation remarks. 1514 addAnnotationRemarksPass(MPM); 1515 1516 if (LTOPreLink) 1517 addRequiredLTOPreLinkPasses(MPM); 1518 1519 return MPM; 1520 } 1521 1522 ModulePassManager 1523 PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1524 assert(Level != OptimizationLevel::O0 && 1525 "Must request optimizations for the default pipeline!"); 1526 1527 ModulePassManager MPM; 1528 1529 // Convert @llvm.global.annotations to !annotation metadata. 1530 MPM.addPass(Annotation2MetadataPass()); 1531 1532 // Force any function attributes we want the rest of the pipeline to observe. 1533 MPM.addPass(ForceFunctionAttrsPass()); 1534 1535 if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1536 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1537 1538 // Apply module pipeline start EP callback. 1539 for (auto &C : PipelineStartEPCallbacks) 1540 C(MPM, Level); 1541 1542 // If we are planning to perform ThinLTO later, we don't bloat the code with 1543 // unrolling/vectorization/... now. Just simplify the module as much as we 1544 // can. 1545 MPM.addPass(buildModuleSimplificationPipeline( 1546 Level, ThinOrFullLTOPhase::ThinLTOPreLink)); 1547 1548 // Run partial inlining pass to partially inline functions that have 1549 // large bodies. 1550 // FIXME: It isn't clear whether this is really the right place to run this 1551 // in ThinLTO. Because there is another canonicalization and simplification 1552 // phase that will run after the thin link, running this here ends up with 1553 // less information than will be available later and it may grow functions in 1554 // ways that aren't beneficial. 1555 if (RunPartialInlining) 1556 MPM.addPass(PartialInlinerPass()); 1557 1558 // Reduce the size of the IR as much as possible. 1559 MPM.addPass(GlobalOptPass()); 1560 1561 // Module simplification splits coroutines, but does not fully clean up 1562 // coroutine intrinsics. To ensure ThinLTO optimization passes don't trip up 1563 // on these, we schedule the cleanup here. 1564 MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass())); 1565 1566 if (PGOOpt && PGOOpt->PseudoProbeForProfiling) 1567 MPM.addPass(PseudoProbeUpdatePass()); 1568 1569 // Handle OptimizerLastEPCallbacks added by clang on PreLink. Actual 1570 // optimization is going to be done in PostLink stage, but clang can't 1571 // add callbacks there in case of in-process ThinLTO called by linker. 1572 for (auto &C : OptimizerLastEPCallbacks) 1573 C(MPM, Level); 1574 1575 // Emit annotation remarks. 1576 addAnnotationRemarksPass(MPM); 1577 1578 addRequiredLTOPreLinkPasses(MPM); 1579 1580 return MPM; 1581 } 1582 1583 ModulePassManager PassBuilder::buildThinLTODefaultPipeline( 1584 OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary) { 1585 ModulePassManager MPM; 1586 1587 // Convert @llvm.global.annotations to !annotation metadata. 1588 MPM.addPass(Annotation2MetadataPass()); 1589 1590 if (ImportSummary) { 1591 // These passes import type identifier resolutions for whole-program 1592 // devirtualization and CFI. They must run early because other passes may 1593 // disturb the specific instruction patterns that these passes look for, 1594 // creating dependencies on resolutions that may not appear in the summary. 1595 // 1596 // For example, GVN may transform the pattern assume(type.test) appearing in 1597 // two basic blocks into assume(phi(type.test, type.test)), which would 1598 // transform a dependency on a WPD resolution into a dependency on a type 1599 // identifier resolution for CFI. 1600 // 1601 // Also, WPD has access to more precise information than ICP and can 1602 // devirtualize more effectively, so it should operate on the IR first. 1603 // 1604 // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1605 // metadata and intrinsics. 1606 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary)); 1607 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary)); 1608 } 1609 1610 if (Level == OptimizationLevel::O0) { 1611 // Run a second time to clean up any type tests left behind by WPD for use 1612 // in ICP. 1613 MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1614 // Drop available_externally and unreferenced globals. This is necessary 1615 // with ThinLTO in order to avoid leaving undefined references to dead 1616 // globals in the object file. 1617 MPM.addPass(EliminateAvailableExternallyPass()); 1618 MPM.addPass(GlobalDCEPass()); 1619 return MPM; 1620 } 1621 1622 // Force any function attributes we want the rest of the pipeline to observe. 1623 MPM.addPass(ForceFunctionAttrsPass()); 1624 1625 // Add the core simplification pipeline. 1626 MPM.addPass(buildModuleSimplificationPipeline( 1627 Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1628 1629 // Now add the optimization pipeline. 1630 MPM.addPass(buildModuleOptimizationPipeline(Level)); 1631 1632 // Emit annotation remarks. 1633 addAnnotationRemarksPass(MPM); 1634 1635 return MPM; 1636 } 1637 1638 ModulePassManager 1639 PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1640 assert(Level != OptimizationLevel::O0 && 1641 "Must request optimizations for the default pipeline!"); 1642 // FIXME: We should use a customized pre-link pipeline! 1643 return buildPerModuleDefaultPipeline(Level, 1644 /* LTOPreLink */ true); 1645 } 1646 1647 ModulePassManager 1648 PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, 1649 ModuleSummaryIndex *ExportSummary) { 1650 ModulePassManager MPM; 1651 1652 // Convert @llvm.global.annotations to !annotation metadata. 1653 MPM.addPass(Annotation2MetadataPass()); 1654 1655 // Create a function that performs CFI checks for cross-DSO calls with targets 1656 // in the current module. 1657 MPM.addPass(CrossDSOCFIPass()); 1658 1659 if (Level == OptimizationLevel::O0) { 1660 // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1661 // metadata and intrinsics. 1662 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1663 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1664 // Run a second time to clean up any type tests left behind by WPD for use 1665 // in ICP. 1666 MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1667 1668 // Emit annotation remarks. 1669 addAnnotationRemarksPass(MPM); 1670 1671 return MPM; 1672 } 1673 1674 if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) { 1675 // Load sample profile before running the LTO optimization pipeline. 1676 MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1677 PGOOpt->ProfileRemappingFile, 1678 ThinOrFullLTOPhase::FullLTOPostLink)); 1679 // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1680 // RequireAnalysisPass for PSI before subsequent non-module passes. 1681 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1682 } 1683 1684 // Remove unused virtual tables to improve the quality of code generated by 1685 // whole-program devirtualization and bitset lowering. 1686 MPM.addPass(GlobalDCEPass()); 1687 1688 // Force any function attributes we want the rest of the pipeline to observe. 1689 MPM.addPass(ForceFunctionAttrsPass()); 1690 1691 // Do basic inference of function attributes from known properties of system 1692 // libraries and other oracles. 1693 MPM.addPass(InferFunctionAttrsPass()); 1694 1695 if (Level.getSpeedupLevel() > 1) { 1696 FunctionPassManager EarlyFPM; 1697 EarlyFPM.addPass(CallSiteSplittingPass()); 1698 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); 1699 1700 // Indirect call promotion. This should promote all the targets that are 1701 // left by the earlier promotion pass that promotes intra-module targets. 1702 // This two-step promotion is to save the compile time. For LTO, it should 1703 // produce the same result as if we only do promotion here. 1704 MPM.addPass(PGOIndirectCallPromotion( 1705 true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse)); 1706 1707 if (EnableFunctionSpecialization) 1708 MPM.addPass(FunctionSpecializationPass()); 1709 // Propagate constants at call sites into the functions they call. This 1710 // opens opportunities for globalopt (and inlining) by substituting function 1711 // pointers passed as arguments to direct uses of functions. 1712 MPM.addPass(IPSCCPPass()); 1713 1714 // Attach metadata to indirect call sites indicating the set of functions 1715 // they may target at run-time. This should follow IPSCCP. 1716 MPM.addPass(CalledValuePropagationPass()); 1717 } 1718 1719 // Now deduce any function attributes based in the current code. 1720 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( 1721 PostOrderFunctionAttrsPass())); 1722 1723 // Do RPO function attribute inference across the module to forward-propagate 1724 // attributes where applicable. 1725 // FIXME: Is this really an optimization rather than a canonicalization? 1726 MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1727 1728 // Use in-range annotations on GEP indices to split globals where beneficial. 1729 MPM.addPass(GlobalSplitPass()); 1730 1731 // Run whole program optimization of virtual call when the list of callees 1732 // is fixed. 1733 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1734 1735 // Stop here at -O1. 1736 if (Level == OptimizationLevel::O1) { 1737 // The LowerTypeTestsPass needs to run to lower type metadata and the 1738 // type.test intrinsics. The pass does nothing if CFI is disabled. 1739 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1740 // Run a second time to clean up any type tests left behind by WPD for use 1741 // in ICP (which is performed earlier than this in the regular LTO 1742 // pipeline). 1743 MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1744 1745 // Emit annotation remarks. 1746 addAnnotationRemarksPass(MPM); 1747 1748 return MPM; 1749 } 1750 1751 // Optimize globals to try and fold them into constants. 1752 MPM.addPass(GlobalOptPass()); 1753 1754 // Promote any localized globals to SSA registers. 1755 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1756 1757 // Linking modules together can lead to duplicate global constant, only 1758 // keep one copy of each constant. 1759 MPM.addPass(ConstantMergePass()); 1760 1761 // Remove unused arguments from functions. 1762 MPM.addPass(DeadArgumentEliminationPass()); 1763 1764 // Reduce the code after globalopt and ipsccp. Both can open up significant 1765 // simplification opportunities, and both can propagate functions through 1766 // function pointers. When this happens, we often have to resolve varargs 1767 // calls, etc, so let instcombine do this. 1768 FunctionPassManager PeepholeFPM; 1769 if (Level == OptimizationLevel::O3) 1770 PeepholeFPM.addPass(AggressiveInstCombinePass()); 1771 PeepholeFPM.addPass(InstCombinePass()); 1772 invokePeepholeEPCallbacks(PeepholeFPM, Level); 1773 1774 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM))); 1775 1776 // Note: historically, the PruneEH pass was run first to deduce nounwind and 1777 // generally clean up exception handling overhead. It isn't clear this is 1778 // valuable as the inliner doesn't currently care whether it is inlining an 1779 // invoke or a call. 1780 // Run the inliner now. 1781 MPM.addPass(ModuleInlinerWrapperPass(getInlineParamsFromOptLevel(Level))); 1782 1783 // Optimize globals again after we ran the inliner. 1784 MPM.addPass(GlobalOptPass()); 1785 1786 // Garbage collect dead functions. 1787 MPM.addPass(GlobalDCEPass()); 1788 1789 // If we didn't decide to inline a function, check to see if we can 1790 // transform it to pass arguments by value instead of by reference. 1791 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(ArgumentPromotionPass())); 1792 1793 FunctionPassManager FPM; 1794 // The IPO Passes may leave cruft around. Clean up after them. 1795 FPM.addPass(InstCombinePass()); 1796 invokePeepholeEPCallbacks(FPM, Level); 1797 1798 FPM.addPass(JumpThreadingPass(/*InsertFreezeWhenUnfoldingSelect*/ true)); 1799 1800 // Do a post inline PGO instrumentation and use pass. This is a context 1801 // sensitive PGO pass. 1802 if (PGOOpt) { 1803 if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1804 addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1805 /* IsCS */ true, PGOOpt->CSProfileGenFile, 1806 PGOOpt->ProfileRemappingFile); 1807 else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1808 addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1809 /* IsCS */ true, PGOOpt->ProfileFile, 1810 PGOOpt->ProfileRemappingFile); 1811 } 1812 1813 // Break up allocas 1814 FPM.addPass(SROA()); 1815 1816 // LTO provides additional opportunities for tailcall elimination due to 1817 // link-time inlining, and visibility of nocapture attribute. 1818 FPM.addPass(TailCallElimPass()); 1819 1820 // Run a few AA driver optimizations here and now to cleanup the code. 1821 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1822 1823 MPM.addPass( 1824 createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1825 1826 // Require the GlobalsAA analysis for the module so we can query it within 1827 // MainFPM. 1828 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1829 // Invalidate AAManager so it can be recreated and pick up the newly available 1830 // GlobalsAA. 1831 MPM.addPass( 1832 createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 1833 1834 FunctionPassManager MainFPM; 1835 MainFPM.addPass(createFunctionToLoopPassAdaptor( 1836 LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 1837 EnableMSSALoopDependency, /*UseBlockFrequencyInfo=*/true)); 1838 1839 if (RunNewGVN) 1840 MainFPM.addPass(NewGVNPass()); 1841 else 1842 MainFPM.addPass(GVN()); 1843 1844 // Remove dead memcpy()'s. 1845 MainFPM.addPass(MemCpyOptPass()); 1846 1847 // Nuke dead stores. 1848 MainFPM.addPass(DSEPass()); 1849 MainFPM.addPass(MergedLoadStoreMotionPass()); 1850 1851 // More loops are countable; try to optimize them. 1852 if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) 1853 MainFPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 1854 1855 if (EnableConstraintElimination) 1856 MainFPM.addPass(ConstraintEliminationPass()); 1857 1858 LoopPassManager LPM; 1859 LPM.addPass(IndVarSimplifyPass()); 1860 LPM.addPass(LoopDeletionPass()); 1861 // FIXME: Add loop interchange. 1862 1863 // Unroll small loops and perform peeling. 1864 LPM.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 1865 /* OnlyWhenForced= */ !PTO.LoopUnrolling, 1866 PTO.ForgetAllSCEVInLoopUnroll)); 1867 // The loop passes in LPM (LoopFullUnrollPass) do not preserve MemorySSA. 1868 // *All* loop passes must preserve it, in order to be able to use it. 1869 MainFPM.addPass(createFunctionToLoopPassAdaptor( 1870 std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/true)); 1871 1872 MainFPM.addPass(LoopDistributePass()); 1873 1874 addVectorPasses(Level, MainFPM, /* IsFullLTO */ true); 1875 1876 invokePeepholeEPCallbacks(MainFPM, Level); 1877 MainFPM.addPass(JumpThreadingPass(/*InsertFreezeWhenUnfoldingSelect*/ true)); 1878 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM))); 1879 1880 // Lower type metadata and the type.test intrinsic. This pass supports 1881 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs 1882 // to be run at link time if CFI is enabled. This pass does nothing if 1883 // CFI is disabled. 1884 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1885 // Run a second time to clean up any type tests left behind by WPD for use 1886 // in ICP (which is performed earlier than this in the regular LTO pipeline). 1887 MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1888 1889 // Enable splitting late in the FullLTO post-link pipeline. This is done in 1890 // the same stage in the old pass manager (\ref addLateLTOOptimizationPasses). 1891 if (EnableHotColdSplit) 1892 MPM.addPass(HotColdSplittingPass()); 1893 1894 // Add late LTO optimization passes. 1895 // Delete basic blocks, which optimization passes may have killed. 1896 MPM.addPass(createModuleToFunctionPassAdaptor( 1897 SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)))); 1898 1899 // Drop bodies of available eternally objects to improve GlobalDCE. 1900 MPM.addPass(EliminateAvailableExternallyPass()); 1901 1902 // Now that we have optimized the program, discard unreachable functions. 1903 MPM.addPass(GlobalDCEPass()); 1904 1905 if (PTO.MergeFunctions) 1906 MPM.addPass(MergeFunctionsPass()); 1907 1908 // Emit annotation remarks. 1909 addAnnotationRemarksPass(MPM); 1910 1911 return MPM; 1912 } 1913 1914 ModulePassManager PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level, 1915 bool LTOPreLink) { 1916 assert(Level == OptimizationLevel::O0 && 1917 "buildO0DefaultPipeline should only be used with O0"); 1918 1919 ModulePassManager MPM; 1920 1921 if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr || 1922 PGOOpt->Action == PGOOptions::IRUse)) 1923 addPGOInstrPassesForO0( 1924 MPM, 1925 /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr), 1926 /* IsCS */ false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile); 1927 1928 for (auto &C : PipelineStartEPCallbacks) 1929 C(MPM, Level); 1930 1931 if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1932 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1933 1934 for (auto &C : PipelineEarlySimplificationEPCallbacks) 1935 C(MPM, Level); 1936 1937 // Build a minimal pipeline based on the semantics required by LLVM, 1938 // which is just that always inlining occurs. Further, disable generating 1939 // lifetime intrinsics to avoid enabling further optimizations during 1940 // code generation. 1941 MPM.addPass(AlwaysInlinerPass( 1942 /*InsertLifetimeIntrinsics=*/false)); 1943 1944 if (PTO.MergeFunctions) 1945 MPM.addPass(MergeFunctionsPass()); 1946 1947 if (EnableMatrix) 1948 MPM.addPass( 1949 createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true))); 1950 1951 if (!CGSCCOptimizerLateEPCallbacks.empty()) { 1952 CGSCCPassManager CGPM; 1953 for (auto &C : CGSCCOptimizerLateEPCallbacks) 1954 C(CGPM, Level); 1955 if (!CGPM.isEmpty()) 1956 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 1957 } 1958 if (!LateLoopOptimizationsEPCallbacks.empty()) { 1959 LoopPassManager LPM; 1960 for (auto &C : LateLoopOptimizationsEPCallbacks) 1961 C(LPM, Level); 1962 if (!LPM.isEmpty()) { 1963 MPM.addPass(createModuleToFunctionPassAdaptor( 1964 createFunctionToLoopPassAdaptor(std::move(LPM)))); 1965 } 1966 } 1967 if (!LoopOptimizerEndEPCallbacks.empty()) { 1968 LoopPassManager LPM; 1969 for (auto &C : LoopOptimizerEndEPCallbacks) 1970 C(LPM, Level); 1971 if (!LPM.isEmpty()) { 1972 MPM.addPass(createModuleToFunctionPassAdaptor( 1973 createFunctionToLoopPassAdaptor(std::move(LPM)))); 1974 } 1975 } 1976 if (!ScalarOptimizerLateEPCallbacks.empty()) { 1977 FunctionPassManager FPM; 1978 for (auto &C : ScalarOptimizerLateEPCallbacks) 1979 C(FPM, Level); 1980 if (!FPM.isEmpty()) 1981 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1982 } 1983 if (!VectorizerStartEPCallbacks.empty()) { 1984 FunctionPassManager FPM; 1985 for (auto &C : VectorizerStartEPCallbacks) 1986 C(FPM, Level); 1987 if (!FPM.isEmpty()) 1988 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1989 } 1990 1991 MPM.addPass(createModuleToFunctionPassAdaptor(CoroEarlyPass())); 1992 CGSCCPassManager CGPM; 1993 CGPM.addPass(CoroSplitPass()); 1994 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 1995 MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass())); 1996 1997 for (auto &C : OptimizerLastEPCallbacks) 1998 C(MPM, Level); 1999 2000 if (LTOPreLink) 2001 addRequiredLTOPreLinkPasses(MPM); 2002 2003 return MPM; 2004 } 2005 2006 AAManager PassBuilder::buildDefaultAAPipeline() { 2007 AAManager AA; 2008 2009 // The order in which these are registered determines their priority when 2010 // being queried. 2011 2012 // First we register the basic alias analysis that provides the majority of 2013 // per-function local AA logic. This is a stateless, on-demand local set of 2014 // AA techniques. 2015 AA.registerFunctionAnalysis<BasicAA>(); 2016 2017 // Next we query fast, specialized alias analyses that wrap IR-embedded 2018 // information about aliasing. 2019 AA.registerFunctionAnalysis<ScopedNoAliasAA>(); 2020 AA.registerFunctionAnalysis<TypeBasedAA>(); 2021 2022 // Add support for querying global aliasing information when available. 2023 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module 2024 // analysis, all that the `AAManager` can do is query for any *cached* 2025 // results from `GlobalsAA` through a readonly proxy. 2026 AA.registerModuleAnalysis<GlobalsAA>(); 2027 2028 // Add target-specific alias analyses. 2029 if (TM) 2030 TM->registerDefaultAliasAnalyses(AA); 2031 2032 return AA; 2033 } 2034 2035 static Optional<int> parseRepeatPassName(StringRef Name) { 2036 if (!Name.consume_front("repeat<") || !Name.consume_back(">")) 2037 return None; 2038 int Count; 2039 if (Name.getAsInteger(0, Count) || Count <= 0) 2040 return None; 2041 return Count; 2042 } 2043 2044 static Optional<int> parseDevirtPassName(StringRef Name) { 2045 if (!Name.consume_front("devirt<") || !Name.consume_back(">")) 2046 return None; 2047 int Count; 2048 if (Name.getAsInteger(0, Count) || Count < 0) 2049 return None; 2050 return Count; 2051 } 2052 2053 static bool checkParametrizedPassName(StringRef Name, StringRef PassName) { 2054 if (!Name.consume_front(PassName)) 2055 return false; 2056 // normal pass name w/o parameters == default parameters 2057 if (Name.empty()) 2058 return true; 2059 return Name.startswith("<") && Name.endswith(">"); 2060 } 2061 2062 namespace { 2063 2064 /// This performs customized parsing of pass name with parameters. 2065 /// 2066 /// We do not need parametrization of passes in textual pipeline very often, 2067 /// yet on a rare occasion ability to specify parameters right there can be 2068 /// useful. 2069 /// 2070 /// \p Name - parameterized specification of a pass from a textual pipeline 2071 /// is a string in a form of : 2072 /// PassName '<' parameter-list '>' 2073 /// 2074 /// Parameter list is being parsed by the parser callable argument, \p Parser, 2075 /// It takes a string-ref of parameters and returns either StringError or a 2076 /// parameter list in a form of a custom parameters type, all wrapped into 2077 /// Expected<> template class. 2078 /// 2079 template <typename ParametersParseCallableT> 2080 auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name, 2081 StringRef PassName) -> decltype(Parser(StringRef{})) { 2082 using ParametersT = typename decltype(Parser(StringRef{}))::value_type; 2083 2084 StringRef Params = Name; 2085 if (!Params.consume_front(PassName)) { 2086 assert(false && 2087 "unable to strip pass name from parametrized pass specification"); 2088 } 2089 if (!Params.empty() && 2090 (!Params.consume_front("<") || !Params.consume_back(">"))) { 2091 assert(false && "invalid format for parametrized pass name"); 2092 } 2093 2094 Expected<ParametersT> Result = Parser(Params); 2095 assert((Result || Result.template errorIsA<StringError>()) && 2096 "Pass parameter parser can only return StringErrors."); 2097 return Result; 2098 } 2099 2100 /// Parser of parameters for LoopUnroll pass. 2101 Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) { 2102 LoopUnrollOptions UnrollOpts; 2103 while (!Params.empty()) { 2104 StringRef ParamName; 2105 std::tie(ParamName, Params) = Params.split(';'); 2106 int OptLevel = StringSwitch<int>(ParamName) 2107 .Case("O0", 0) 2108 .Case("O1", 1) 2109 .Case("O2", 2) 2110 .Case("O3", 3) 2111 .Default(-1); 2112 if (OptLevel >= 0) { 2113 UnrollOpts.setOptLevel(OptLevel); 2114 continue; 2115 } 2116 if (ParamName.consume_front("full-unroll-max=")) { 2117 int Count; 2118 if (ParamName.getAsInteger(0, Count)) 2119 return make_error<StringError>( 2120 formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(), 2121 inconvertibleErrorCode()); 2122 UnrollOpts.setFullUnrollMaxCount(Count); 2123 continue; 2124 } 2125 2126 bool Enable = !ParamName.consume_front("no-"); 2127 if (ParamName == "partial") { 2128 UnrollOpts.setPartial(Enable); 2129 } else if (ParamName == "peeling") { 2130 UnrollOpts.setPeeling(Enable); 2131 } else if (ParamName == "profile-peeling") { 2132 UnrollOpts.setProfileBasedPeeling(Enable); 2133 } else if (ParamName == "runtime") { 2134 UnrollOpts.setRuntime(Enable); 2135 } else if (ParamName == "upperbound") { 2136 UnrollOpts.setUpperBound(Enable); 2137 } else { 2138 return make_error<StringError>( 2139 formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(), 2140 inconvertibleErrorCode()); 2141 } 2142 } 2143 return UnrollOpts; 2144 } 2145 2146 Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) { 2147 MemorySanitizerOptions Result; 2148 while (!Params.empty()) { 2149 StringRef ParamName; 2150 std::tie(ParamName, Params) = Params.split(';'); 2151 2152 if (ParamName == "recover") { 2153 Result.Recover = true; 2154 } else if (ParamName == "kernel") { 2155 Result.Kernel = true; 2156 } else if (ParamName.consume_front("track-origins=")) { 2157 if (ParamName.getAsInteger(0, Result.TrackOrigins)) 2158 return make_error<StringError>( 2159 formatv("invalid argument to MemorySanitizer pass track-origins " 2160 "parameter: '{0}' ", 2161 ParamName) 2162 .str(), 2163 inconvertibleErrorCode()); 2164 } else { 2165 return make_error<StringError>( 2166 formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName) 2167 .str(), 2168 inconvertibleErrorCode()); 2169 } 2170 } 2171 return Result; 2172 } 2173 2174 /// Parser of parameters for SimplifyCFG pass. 2175 Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) { 2176 SimplifyCFGOptions Result; 2177 while (!Params.empty()) { 2178 StringRef ParamName; 2179 std::tie(ParamName, Params) = Params.split(';'); 2180 2181 bool Enable = !ParamName.consume_front("no-"); 2182 if (ParamName == "forward-switch-cond") { 2183 Result.forwardSwitchCondToPhi(Enable); 2184 } else if (ParamName == "switch-to-lookup") { 2185 Result.convertSwitchToLookupTable(Enable); 2186 } else if (ParamName == "keep-loops") { 2187 Result.needCanonicalLoops(Enable); 2188 } else if (ParamName == "hoist-common-insts") { 2189 Result.hoistCommonInsts(Enable); 2190 } else if (ParamName == "sink-common-insts") { 2191 Result.sinkCommonInsts(Enable); 2192 } else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) { 2193 APInt BonusInstThreshold; 2194 if (ParamName.getAsInteger(0, BonusInstThreshold)) 2195 return make_error<StringError>( 2196 formatv("invalid argument to SimplifyCFG pass bonus-threshold " 2197 "parameter: '{0}' ", 2198 ParamName).str(), 2199 inconvertibleErrorCode()); 2200 Result.bonusInstThreshold(BonusInstThreshold.getSExtValue()); 2201 } else { 2202 return make_error<StringError>( 2203 formatv("invalid SimplifyCFG pass parameter '{0}' ", ParamName).str(), 2204 inconvertibleErrorCode()); 2205 } 2206 } 2207 return Result; 2208 } 2209 2210 /// Parser of parameters for LoopVectorize pass. 2211 Expected<LoopVectorizeOptions> parseLoopVectorizeOptions(StringRef Params) { 2212 LoopVectorizeOptions Opts; 2213 while (!Params.empty()) { 2214 StringRef ParamName; 2215 std::tie(ParamName, Params) = Params.split(';'); 2216 2217 bool Enable = !ParamName.consume_front("no-"); 2218 if (ParamName == "interleave-forced-only") { 2219 Opts.setInterleaveOnlyWhenForced(Enable); 2220 } else if (ParamName == "vectorize-forced-only") { 2221 Opts.setVectorizeOnlyWhenForced(Enable); 2222 } else { 2223 return make_error<StringError>( 2224 formatv("invalid LoopVectorize parameter '{0}' ", ParamName).str(), 2225 inconvertibleErrorCode()); 2226 } 2227 } 2228 return Opts; 2229 } 2230 2231 Expected<std::pair<bool, bool>> parseLoopUnswitchOptions(StringRef Params) { 2232 std::pair<bool, bool> Result = {false, true}; 2233 while (!Params.empty()) { 2234 StringRef ParamName; 2235 std::tie(ParamName, Params) = Params.split(';'); 2236 2237 bool Enable = !ParamName.consume_front("no-"); 2238 if (ParamName == "nontrivial") { 2239 Result.first = Enable; 2240 } else if (ParamName == "trivial") { 2241 Result.second = Enable; 2242 } else { 2243 return make_error<StringError>( 2244 formatv("invalid LoopUnswitch pass parameter '{0}' ", ParamName) 2245 .str(), 2246 inconvertibleErrorCode()); 2247 } 2248 } 2249 return Result; 2250 } 2251 2252 Expected<bool> parseMergedLoadStoreMotionOptions(StringRef Params) { 2253 bool Result = false; 2254 while (!Params.empty()) { 2255 StringRef ParamName; 2256 std::tie(ParamName, Params) = Params.split(';'); 2257 2258 bool Enable = !ParamName.consume_front("no-"); 2259 if (ParamName == "split-footer-bb") { 2260 Result = Enable; 2261 } else { 2262 return make_error<StringError>( 2263 formatv("invalid MergedLoadStoreMotion pass parameter '{0}' ", 2264 ParamName) 2265 .str(), 2266 inconvertibleErrorCode()); 2267 } 2268 } 2269 return Result; 2270 } 2271 2272 Expected<GVNOptions> parseGVNOptions(StringRef Params) { 2273 GVNOptions Result; 2274 while (!Params.empty()) { 2275 StringRef ParamName; 2276 std::tie(ParamName, Params) = Params.split(';'); 2277 2278 bool Enable = !ParamName.consume_front("no-"); 2279 if (ParamName == "pre") { 2280 Result.setPRE(Enable); 2281 } else if (ParamName == "load-pre") { 2282 Result.setLoadPRE(Enable); 2283 } else if (ParamName == "split-backedge-load-pre") { 2284 Result.setLoadPRESplitBackedge(Enable); 2285 } else if (ParamName == "memdep") { 2286 Result.setMemDep(Enable); 2287 } else { 2288 return make_error<StringError>( 2289 formatv("invalid GVN pass parameter '{0}' ", ParamName).str(), 2290 inconvertibleErrorCode()); 2291 } 2292 } 2293 return Result; 2294 } 2295 2296 Expected<StackLifetime::LivenessType> 2297 parseStackLifetimeOptions(StringRef Params) { 2298 StackLifetime::LivenessType Result = StackLifetime::LivenessType::May; 2299 while (!Params.empty()) { 2300 StringRef ParamName; 2301 std::tie(ParamName, Params) = Params.split(';'); 2302 2303 if (ParamName == "may") { 2304 Result = StackLifetime::LivenessType::May; 2305 } else if (ParamName == "must") { 2306 Result = StackLifetime::LivenessType::Must; 2307 } else { 2308 return make_error<StringError>( 2309 formatv("invalid StackLifetime parameter '{0}' ", ParamName).str(), 2310 inconvertibleErrorCode()); 2311 } 2312 } 2313 return Result; 2314 } 2315 2316 } // namespace 2317 2318 /// Tests whether a pass name starts with a valid prefix for a default pipeline 2319 /// alias. 2320 static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) { 2321 return Name.startswith("default") || Name.startswith("thinlto") || 2322 Name.startswith("lto"); 2323 } 2324 2325 /// Tests whether registered callbacks will accept a given pass name. 2326 /// 2327 /// When parsing a pipeline text, the type of the outermost pipeline may be 2328 /// omitted, in which case the type is automatically determined from the first 2329 /// pass name in the text. This may be a name that is handled through one of the 2330 /// callbacks. We check this through the oridinary parsing callbacks by setting 2331 /// up a dummy PassManager in order to not force the client to also handle this 2332 /// type of query. 2333 template <typename PassManagerT, typename CallbacksT> 2334 static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) { 2335 if (!Callbacks.empty()) { 2336 PassManagerT DummyPM; 2337 for (auto &CB : Callbacks) 2338 if (CB(Name, DummyPM, {})) 2339 return true; 2340 } 2341 return false; 2342 } 2343 2344 template <typename CallbacksT> 2345 static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) { 2346 // Manually handle aliases for pre-configured pipeline fragments. 2347 if (startsWithDefaultPipelineAliasPrefix(Name)) 2348 return DefaultAliasRegex.match(Name); 2349 2350 // Explicitly handle pass manager names. 2351 if (Name == "module") 2352 return true; 2353 if (Name == "cgscc") 2354 return true; 2355 if (Name == "function") 2356 return true; 2357 2358 // Explicitly handle custom-parsed pass names. 2359 if (parseRepeatPassName(Name)) 2360 return true; 2361 2362 #define MODULE_PASS(NAME, CREATE_PASS) \ 2363 if (Name == NAME) \ 2364 return true; 2365 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ 2366 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ 2367 return true; 2368 #include "PassRegistry.def" 2369 2370 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks); 2371 } 2372 2373 template <typename CallbacksT> 2374 static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) { 2375 // Explicitly handle pass manager names. 2376 if (Name == "cgscc") 2377 return true; 2378 if (Name == "function") 2379 return true; 2380 2381 // Explicitly handle custom-parsed pass names. 2382 if (parseRepeatPassName(Name)) 2383 return true; 2384 if (parseDevirtPassName(Name)) 2385 return true; 2386 2387 #define CGSCC_PASS(NAME, CREATE_PASS) \ 2388 if (Name == NAME) \ 2389 return true; 2390 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ 2391 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ 2392 return true; 2393 #include "PassRegistry.def" 2394 2395 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks); 2396 } 2397 2398 template <typename CallbacksT> 2399 static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) { 2400 // Explicitly handle pass manager names. 2401 if (Name == "function") 2402 return true; 2403 if (Name == "loop" || Name == "loop-mssa") 2404 return true; 2405 2406 // Explicitly handle custom-parsed pass names. 2407 if (parseRepeatPassName(Name)) 2408 return true; 2409 2410 #define FUNCTION_PASS(NAME, CREATE_PASS) \ 2411 if (Name == NAME) \ 2412 return true; 2413 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2414 if (checkParametrizedPassName(Name, NAME)) \ 2415 return true; 2416 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ 2417 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ 2418 return true; 2419 #include "PassRegistry.def" 2420 2421 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks); 2422 } 2423 2424 template <typename CallbacksT> 2425 static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) { 2426 // Explicitly handle pass manager names. 2427 if (Name == "loop" || Name == "loop-mssa") 2428 return true; 2429 2430 // Explicitly handle custom-parsed pass names. 2431 if (parseRepeatPassName(Name)) 2432 return true; 2433 2434 #define LOOP_PASS(NAME, CREATE_PASS) \ 2435 if (Name == NAME) \ 2436 return true; 2437 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2438 if (checkParametrizedPassName(Name, NAME)) \ 2439 return true; 2440 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ 2441 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ 2442 return true; 2443 #include "PassRegistry.def" 2444 2445 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks); 2446 } 2447 2448 Optional<std::vector<PassBuilder::PipelineElement>> 2449 PassBuilder::parsePipelineText(StringRef Text) { 2450 std::vector<PipelineElement> ResultPipeline; 2451 2452 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = { 2453 &ResultPipeline}; 2454 for (;;) { 2455 std::vector<PipelineElement> &Pipeline = *PipelineStack.back(); 2456 size_t Pos = Text.find_first_of(",()"); 2457 Pipeline.push_back({Text.substr(0, Pos), {}}); 2458 2459 // If we have a single terminating name, we're done. 2460 if (Pos == Text.npos) 2461 break; 2462 2463 char Sep = Text[Pos]; 2464 Text = Text.substr(Pos + 1); 2465 if (Sep == ',') 2466 // Just a name ending in a comma, continue. 2467 continue; 2468 2469 if (Sep == '(') { 2470 // Push the inner pipeline onto the stack to continue processing. 2471 PipelineStack.push_back(&Pipeline.back().InnerPipeline); 2472 continue; 2473 } 2474 2475 assert(Sep == ')' && "Bogus separator!"); 2476 // When handling the close parenthesis, we greedily consume them to avoid 2477 // empty strings in the pipeline. 2478 do { 2479 // If we try to pop the outer pipeline we have unbalanced parentheses. 2480 if (PipelineStack.size() == 1) 2481 return None; 2482 2483 PipelineStack.pop_back(); 2484 } while (Text.consume_front(")")); 2485 2486 // Check if we've finished parsing. 2487 if (Text.empty()) 2488 break; 2489 2490 // Otherwise, the end of an inner pipeline always has to be followed by 2491 // a comma, and then we can continue. 2492 if (!Text.consume_front(",")) 2493 return None; 2494 } 2495 2496 if (PipelineStack.size() > 1) 2497 // Unbalanced paretheses. 2498 return None; 2499 2500 assert(PipelineStack.back() == &ResultPipeline && 2501 "Wrong pipeline at the bottom of the stack!"); 2502 return {std::move(ResultPipeline)}; 2503 } 2504 2505 Error PassBuilder::parseModulePass(ModulePassManager &MPM, 2506 const PipelineElement &E) { 2507 auto &Name = E.Name; 2508 auto &InnerPipeline = E.InnerPipeline; 2509 2510 // First handle complex passes like the pass managers which carry pipelines. 2511 if (!InnerPipeline.empty()) { 2512 if (Name == "module") { 2513 ModulePassManager NestedMPM; 2514 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline)) 2515 return Err; 2516 MPM.addPass(std::move(NestedMPM)); 2517 return Error::success(); 2518 } 2519 if (Name == "cgscc") { 2520 CGSCCPassManager CGPM; 2521 if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline)) 2522 return Err; 2523 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 2524 return Error::success(); 2525 } 2526 if (Name == "function") { 2527 FunctionPassManager FPM; 2528 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline)) 2529 return Err; 2530 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 2531 return Error::success(); 2532 } 2533 if (auto Count = parseRepeatPassName(Name)) { 2534 ModulePassManager NestedMPM; 2535 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline)) 2536 return Err; 2537 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM))); 2538 return Error::success(); 2539 } 2540 2541 for (auto &C : ModulePipelineParsingCallbacks) 2542 if (C(Name, MPM, InnerPipeline)) 2543 return Error::success(); 2544 2545 // Normal passes can't have pipelines. 2546 return make_error<StringError>( 2547 formatv("invalid use of '{0}' pass as module pipeline", Name).str(), 2548 inconvertibleErrorCode()); 2549 ; 2550 } 2551 2552 // Manually handle aliases for pre-configured pipeline fragments. 2553 if (startsWithDefaultPipelineAliasPrefix(Name)) { 2554 SmallVector<StringRef, 3> Matches; 2555 if (!DefaultAliasRegex.match(Name, &Matches)) 2556 return make_error<StringError>( 2557 formatv("unknown default pipeline alias '{0}'", Name).str(), 2558 inconvertibleErrorCode()); 2559 2560 assert(Matches.size() == 3 && "Must capture two matched strings!"); 2561 2562 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2]) 2563 .Case("O0", OptimizationLevel::O0) 2564 .Case("O1", OptimizationLevel::O1) 2565 .Case("O2", OptimizationLevel::O2) 2566 .Case("O3", OptimizationLevel::O3) 2567 .Case("Os", OptimizationLevel::Os) 2568 .Case("Oz", OptimizationLevel::Oz); 2569 if (L == OptimizationLevel::O0 && Matches[1] != "thinlto" && 2570 Matches[1] != "lto") { 2571 MPM.addPass(buildO0DefaultPipeline(L, Matches[1] == "thinlto-pre-link" || 2572 Matches[1] == "lto-pre-link")); 2573 return Error::success(); 2574 } 2575 2576 // This is consistent with old pass manager invoked via opt, but 2577 // inconsistent with clang. Clang doesn't enable loop vectorization 2578 // but does enable slp vectorization at Oz. 2579 PTO.LoopVectorization = 2580 L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz; 2581 PTO.SLPVectorization = 2582 L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz; 2583 2584 if (Matches[1] == "default") { 2585 MPM.addPass(buildPerModuleDefaultPipeline(L)); 2586 } else if (Matches[1] == "thinlto-pre-link") { 2587 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L)); 2588 } else if (Matches[1] == "thinlto") { 2589 MPM.addPass(buildThinLTODefaultPipeline(L, nullptr)); 2590 } else if (Matches[1] == "lto-pre-link") { 2591 MPM.addPass(buildLTOPreLinkDefaultPipeline(L)); 2592 } else { 2593 assert(Matches[1] == "lto" && "Not one of the matched options!"); 2594 MPM.addPass(buildLTODefaultPipeline(L, nullptr)); 2595 } 2596 return Error::success(); 2597 } 2598 2599 // Finally expand the basic registered passes from the .inc file. 2600 #define MODULE_PASS(NAME, CREATE_PASS) \ 2601 if (Name == NAME) { \ 2602 MPM.addPass(CREATE_PASS); \ 2603 return Error::success(); \ 2604 } 2605 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ 2606 if (Name == "require<" NAME ">") { \ 2607 MPM.addPass( \ 2608 RequireAnalysisPass< \ 2609 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \ 2610 return Error::success(); \ 2611 } \ 2612 if (Name == "invalidate<" NAME ">") { \ 2613 MPM.addPass(InvalidateAnalysisPass< \ 2614 std::remove_reference<decltype(CREATE_PASS)>::type>()); \ 2615 return Error::success(); \ 2616 } 2617 #define CGSCC_PASS(NAME, CREATE_PASS) \ 2618 if (Name == NAME) { \ 2619 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(CREATE_PASS)); \ 2620 return Error::success(); \ 2621 } 2622 #define FUNCTION_PASS(NAME, CREATE_PASS) \ 2623 if (Name == NAME) { \ 2624 MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS)); \ 2625 return Error::success(); \ 2626 } 2627 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2628 if (checkParametrizedPassName(Name, NAME)) { \ 2629 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2630 if (!Params) \ 2631 return Params.takeError(); \ 2632 MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \ 2633 return Error::success(); \ 2634 } 2635 #define LOOP_PASS(NAME, CREATE_PASS) \ 2636 if (Name == NAME) { \ 2637 MPM.addPass(createModuleToFunctionPassAdaptor( \ 2638 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \ 2639 return Error::success(); \ 2640 } 2641 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2642 if (checkParametrizedPassName(Name, NAME)) { \ 2643 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2644 if (!Params) \ 2645 return Params.takeError(); \ 2646 MPM.addPass( \ 2647 createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( \ 2648 CREATE_PASS(Params.get()), false, false))); \ 2649 return Error::success(); \ 2650 } 2651 #include "PassRegistry.def" 2652 2653 for (auto &C : ModulePipelineParsingCallbacks) 2654 if (C(Name, MPM, InnerPipeline)) 2655 return Error::success(); 2656 return make_error<StringError>( 2657 formatv("unknown module pass '{0}'", Name).str(), 2658 inconvertibleErrorCode()); 2659 } 2660 2661 Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM, 2662 const PipelineElement &E) { 2663 auto &Name = E.Name; 2664 auto &InnerPipeline = E.InnerPipeline; 2665 2666 // First handle complex passes like the pass managers which carry pipelines. 2667 if (!InnerPipeline.empty()) { 2668 if (Name == "cgscc") { 2669 CGSCCPassManager NestedCGPM; 2670 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline)) 2671 return Err; 2672 // Add the nested pass manager with the appropriate adaptor. 2673 CGPM.addPass(std::move(NestedCGPM)); 2674 return Error::success(); 2675 } 2676 if (Name == "function") { 2677 FunctionPassManager FPM; 2678 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline)) 2679 return Err; 2680 // Add the nested pass manager with the appropriate adaptor. 2681 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM))); 2682 return Error::success(); 2683 } 2684 if (auto Count = parseRepeatPassName(Name)) { 2685 CGSCCPassManager NestedCGPM; 2686 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline)) 2687 return Err; 2688 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM))); 2689 return Error::success(); 2690 } 2691 if (auto MaxRepetitions = parseDevirtPassName(Name)) { 2692 CGSCCPassManager NestedCGPM; 2693 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline)) 2694 return Err; 2695 CGPM.addPass( 2696 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions)); 2697 return Error::success(); 2698 } 2699 2700 for (auto &C : CGSCCPipelineParsingCallbacks) 2701 if (C(Name, CGPM, InnerPipeline)) 2702 return Error::success(); 2703 2704 // Normal passes can't have pipelines. 2705 return make_error<StringError>( 2706 formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(), 2707 inconvertibleErrorCode()); 2708 } 2709 2710 // Now expand the basic registered passes from the .inc file. 2711 #define CGSCC_PASS(NAME, CREATE_PASS) \ 2712 if (Name == NAME) { \ 2713 CGPM.addPass(CREATE_PASS); \ 2714 return Error::success(); \ 2715 } 2716 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ 2717 if (Name == "require<" NAME ">") { \ 2718 CGPM.addPass(RequireAnalysisPass< \ 2719 std::remove_reference<decltype(CREATE_PASS)>::type, \ 2720 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \ 2721 CGSCCUpdateResult &>()); \ 2722 return Error::success(); \ 2723 } \ 2724 if (Name == "invalidate<" NAME ">") { \ 2725 CGPM.addPass(InvalidateAnalysisPass< \ 2726 std::remove_reference<decltype(CREATE_PASS)>::type>()); \ 2727 return Error::success(); \ 2728 } 2729 #define FUNCTION_PASS(NAME, CREATE_PASS) \ 2730 if (Name == NAME) { \ 2731 CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS)); \ 2732 return Error::success(); \ 2733 } 2734 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2735 if (checkParametrizedPassName(Name, NAME)) { \ 2736 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2737 if (!Params) \ 2738 return Params.takeError(); \ 2739 CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \ 2740 return Error::success(); \ 2741 } 2742 #define LOOP_PASS(NAME, CREATE_PASS) \ 2743 if (Name == NAME) { \ 2744 CGPM.addPass(createCGSCCToFunctionPassAdaptor( \ 2745 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \ 2746 return Error::success(); \ 2747 } 2748 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2749 if (checkParametrizedPassName(Name, NAME)) { \ 2750 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2751 if (!Params) \ 2752 return Params.takeError(); \ 2753 CGPM.addPass( \ 2754 createCGSCCToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( \ 2755 CREATE_PASS(Params.get()), false, false))); \ 2756 return Error::success(); \ 2757 } 2758 #include "PassRegistry.def" 2759 2760 for (auto &C : CGSCCPipelineParsingCallbacks) 2761 if (C(Name, CGPM, InnerPipeline)) 2762 return Error::success(); 2763 return make_error<StringError>( 2764 formatv("unknown cgscc pass '{0}'", Name).str(), 2765 inconvertibleErrorCode()); 2766 } 2767 2768 Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM, 2769 const PipelineElement &E) { 2770 auto &Name = E.Name; 2771 auto &InnerPipeline = E.InnerPipeline; 2772 2773 // First handle complex passes like the pass managers which carry pipelines. 2774 if (!InnerPipeline.empty()) { 2775 if (Name == "function") { 2776 FunctionPassManager NestedFPM; 2777 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline)) 2778 return Err; 2779 // Add the nested pass manager with the appropriate adaptor. 2780 FPM.addPass(std::move(NestedFPM)); 2781 return Error::success(); 2782 } 2783 if (Name == "loop" || Name == "loop-mssa") { 2784 LoopPassManager LPM; 2785 if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline)) 2786 return Err; 2787 // Add the nested pass manager with the appropriate adaptor. 2788 bool UseMemorySSA = (Name == "loop-mssa"); 2789 bool UseBFI = llvm::any_of( 2790 InnerPipeline, [](auto Pipeline) { return Pipeline.Name == "licm"; }); 2791 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM), UseMemorySSA, 2792 UseBFI)); 2793 return Error::success(); 2794 } 2795 if (auto Count = parseRepeatPassName(Name)) { 2796 FunctionPassManager NestedFPM; 2797 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline)) 2798 return Err; 2799 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM))); 2800 return Error::success(); 2801 } 2802 2803 for (auto &C : FunctionPipelineParsingCallbacks) 2804 if (C(Name, FPM, InnerPipeline)) 2805 return Error::success(); 2806 2807 // Normal passes can't have pipelines. 2808 return make_error<StringError>( 2809 formatv("invalid use of '{0}' pass as function pipeline", Name).str(), 2810 inconvertibleErrorCode()); 2811 } 2812 2813 // Now expand the basic registered passes from the .inc file. 2814 #define FUNCTION_PASS(NAME, CREATE_PASS) \ 2815 if (Name == NAME) { \ 2816 FPM.addPass(CREATE_PASS); \ 2817 return Error::success(); \ 2818 } 2819 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2820 if (checkParametrizedPassName(Name, NAME)) { \ 2821 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2822 if (!Params) \ 2823 return Params.takeError(); \ 2824 FPM.addPass(CREATE_PASS(Params.get())); \ 2825 return Error::success(); \ 2826 } 2827 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ 2828 if (Name == "require<" NAME ">") { \ 2829 FPM.addPass( \ 2830 RequireAnalysisPass< \ 2831 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \ 2832 return Error::success(); \ 2833 } \ 2834 if (Name == "invalidate<" NAME ">") { \ 2835 FPM.addPass(InvalidateAnalysisPass< \ 2836 std::remove_reference<decltype(CREATE_PASS)>::type>()); \ 2837 return Error::success(); \ 2838 } 2839 // FIXME: UseMemorySSA is set to false. Maybe we could do things like: 2840 // bool UseMemorySSA = !("canon-freeze" || "loop-predication" || 2841 // "guard-widening"); 2842 // The risk is that it may become obsolete if we're not careful. 2843 #define LOOP_PASS(NAME, CREATE_PASS) \ 2844 if (Name == NAME) { \ 2845 FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)); \ 2846 return Error::success(); \ 2847 } 2848 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2849 if (checkParametrizedPassName(Name, NAME)) { \ 2850 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2851 if (!Params) \ 2852 return Params.takeError(); \ 2853 FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS(Params.get()), \ 2854 false, false)); \ 2855 return Error::success(); \ 2856 } 2857 #include "PassRegistry.def" 2858 2859 for (auto &C : FunctionPipelineParsingCallbacks) 2860 if (C(Name, FPM, InnerPipeline)) 2861 return Error::success(); 2862 return make_error<StringError>( 2863 formatv("unknown function pass '{0}'", Name).str(), 2864 inconvertibleErrorCode()); 2865 } 2866 2867 Error PassBuilder::parseLoopPass(LoopPassManager &LPM, 2868 const PipelineElement &E) { 2869 StringRef Name = E.Name; 2870 auto &InnerPipeline = E.InnerPipeline; 2871 2872 // First handle complex passes like the pass managers which carry pipelines. 2873 if (!InnerPipeline.empty()) { 2874 if (Name == "loop") { 2875 LoopPassManager NestedLPM; 2876 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline)) 2877 return Err; 2878 // Add the nested pass manager with the appropriate adaptor. 2879 LPM.addPass(std::move(NestedLPM)); 2880 return Error::success(); 2881 } 2882 if (auto Count = parseRepeatPassName(Name)) { 2883 LoopPassManager NestedLPM; 2884 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline)) 2885 return Err; 2886 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM))); 2887 return Error::success(); 2888 } 2889 2890 for (auto &C : LoopPipelineParsingCallbacks) 2891 if (C(Name, LPM, InnerPipeline)) 2892 return Error::success(); 2893 2894 // Normal passes can't have pipelines. 2895 return make_error<StringError>( 2896 formatv("invalid use of '{0}' pass as loop pipeline", Name).str(), 2897 inconvertibleErrorCode()); 2898 } 2899 2900 // Now expand the basic registered passes from the .inc file. 2901 #define LOOP_PASS(NAME, CREATE_PASS) \ 2902 if (Name == NAME) { \ 2903 LPM.addPass(CREATE_PASS); \ 2904 return Error::success(); \ 2905 } 2906 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 2907 if (checkParametrizedPassName(Name, NAME)) { \ 2908 auto Params = parsePassParameters(PARSER, Name, NAME); \ 2909 if (!Params) \ 2910 return Params.takeError(); \ 2911 LPM.addPass(CREATE_PASS(Params.get())); \ 2912 return Error::success(); \ 2913 } 2914 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ 2915 if (Name == "require<" NAME ">") { \ 2916 LPM.addPass(RequireAnalysisPass< \ 2917 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \ 2918 LoopAnalysisManager, LoopStandardAnalysisResults &, \ 2919 LPMUpdater &>()); \ 2920 return Error::success(); \ 2921 } \ 2922 if (Name == "invalidate<" NAME ">") { \ 2923 LPM.addPass(InvalidateAnalysisPass< \ 2924 std::remove_reference<decltype(CREATE_PASS)>::type>()); \ 2925 return Error::success(); \ 2926 } 2927 #include "PassRegistry.def" 2928 2929 for (auto &C : LoopPipelineParsingCallbacks) 2930 if (C(Name, LPM, InnerPipeline)) 2931 return Error::success(); 2932 return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(), 2933 inconvertibleErrorCode()); 2934 } 2935 2936 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) { 2937 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 2938 if (Name == NAME) { \ 2939 AA.registerModuleAnalysis< \ 2940 std::remove_reference<decltype(CREATE_PASS)>::type>(); \ 2941 return true; \ 2942 } 2943 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 2944 if (Name == NAME) { \ 2945 AA.registerFunctionAnalysis< \ 2946 std::remove_reference<decltype(CREATE_PASS)>::type>(); \ 2947 return true; \ 2948 } 2949 #include "PassRegistry.def" 2950 2951 for (auto &C : AAParsingCallbacks) 2952 if (C(Name, AA)) 2953 return true; 2954 return false; 2955 } 2956 2957 Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM, 2958 ArrayRef<PipelineElement> Pipeline) { 2959 for (const auto &Element : Pipeline) { 2960 if (auto Err = parseLoopPass(LPM, Element)) 2961 return Err; 2962 } 2963 return Error::success(); 2964 } 2965 2966 Error PassBuilder::parseFunctionPassPipeline( 2967 FunctionPassManager &FPM, ArrayRef<PipelineElement> Pipeline) { 2968 for (const auto &Element : Pipeline) { 2969 if (auto Err = parseFunctionPass(FPM, Element)) 2970 return Err; 2971 } 2972 return Error::success(); 2973 } 2974 2975 Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM, 2976 ArrayRef<PipelineElement> Pipeline) { 2977 for (const auto &Element : Pipeline) { 2978 if (auto Err = parseCGSCCPass(CGPM, Element)) 2979 return Err; 2980 } 2981 return Error::success(); 2982 } 2983 2984 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM, 2985 FunctionAnalysisManager &FAM, 2986 CGSCCAnalysisManager &CGAM, 2987 ModuleAnalysisManager &MAM) { 2988 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); 2989 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); }); 2990 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); }); 2991 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); }); 2992 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); 2993 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); }); 2994 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); }); 2995 } 2996 2997 Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM, 2998 ArrayRef<PipelineElement> Pipeline) { 2999 for (const auto &Element : Pipeline) { 3000 if (auto Err = parseModulePass(MPM, Element)) 3001 return Err; 3002 } 3003 return Error::success(); 3004 } 3005 3006 // Primary pass pipeline description parsing routine for a \c ModulePassManager 3007 // FIXME: Should this routine accept a TargetMachine or require the caller to 3008 // pre-populate the analysis managers with target-specific stuff? 3009 Error PassBuilder::parsePassPipeline(ModulePassManager &MPM, 3010 StringRef PipelineText) { 3011 auto Pipeline = parsePipelineText(PipelineText); 3012 if (!Pipeline || Pipeline->empty()) 3013 return make_error<StringError>( 3014 formatv("invalid pipeline '{0}'", PipelineText).str(), 3015 inconvertibleErrorCode()); 3016 3017 // If the first name isn't at the module layer, wrap the pipeline up 3018 // automatically. 3019 StringRef FirstName = Pipeline->front().Name; 3020 3021 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) { 3022 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) { 3023 Pipeline = {{"cgscc", std::move(*Pipeline)}}; 3024 } else if (isFunctionPassName(FirstName, 3025 FunctionPipelineParsingCallbacks)) { 3026 Pipeline = {{"function", std::move(*Pipeline)}}; 3027 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) { 3028 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}}; 3029 } else { 3030 for (auto &C : TopLevelPipelineParsingCallbacks) 3031 if (C(MPM, *Pipeline)) 3032 return Error::success(); 3033 3034 // Unknown pass or pipeline name! 3035 auto &InnerPipeline = Pipeline->front().InnerPipeline; 3036 return make_error<StringError>( 3037 formatv("unknown {0} name '{1}'", 3038 (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName) 3039 .str(), 3040 inconvertibleErrorCode()); 3041 } 3042 } 3043 3044 if (auto Err = parseModulePassPipeline(MPM, *Pipeline)) 3045 return Err; 3046 return Error::success(); 3047 } 3048 3049 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager 3050 Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM, 3051 StringRef PipelineText) { 3052 auto Pipeline = parsePipelineText(PipelineText); 3053 if (!Pipeline || Pipeline->empty()) 3054 return make_error<StringError>( 3055 formatv("invalid pipeline '{0}'", PipelineText).str(), 3056 inconvertibleErrorCode()); 3057 3058 StringRef FirstName = Pipeline->front().Name; 3059 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) 3060 return make_error<StringError>( 3061 formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName, 3062 PipelineText) 3063 .str(), 3064 inconvertibleErrorCode()); 3065 3066 if (auto Err = parseCGSCCPassPipeline(CGPM, *Pipeline)) 3067 return Err; 3068 return Error::success(); 3069 } 3070 3071 // Primary pass pipeline description parsing routine for a \c 3072 // FunctionPassManager 3073 Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM, 3074 StringRef PipelineText) { 3075 auto Pipeline = parsePipelineText(PipelineText); 3076 if (!Pipeline || Pipeline->empty()) 3077 return make_error<StringError>( 3078 formatv("invalid pipeline '{0}'", PipelineText).str(), 3079 inconvertibleErrorCode()); 3080 3081 StringRef FirstName = Pipeline->front().Name; 3082 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks)) 3083 return make_error<StringError>( 3084 formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName, 3085 PipelineText) 3086 .str(), 3087 inconvertibleErrorCode()); 3088 3089 if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline)) 3090 return Err; 3091 return Error::success(); 3092 } 3093 3094 // Primary pass pipeline description parsing routine for a \c LoopPassManager 3095 Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM, 3096 StringRef PipelineText) { 3097 auto Pipeline = parsePipelineText(PipelineText); 3098 if (!Pipeline || Pipeline->empty()) 3099 return make_error<StringError>( 3100 formatv("invalid pipeline '{0}'", PipelineText).str(), 3101 inconvertibleErrorCode()); 3102 3103 if (auto Err = parseLoopPassPipeline(CGPM, *Pipeline)) 3104 return Err; 3105 3106 return Error::success(); 3107 } 3108 3109 Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) { 3110 // If the pipeline just consists of the word 'default' just replace the AA 3111 // manager with our default one. 3112 if (PipelineText == "default") { 3113 AA = buildDefaultAAPipeline(); 3114 return Error::success(); 3115 } 3116 3117 while (!PipelineText.empty()) { 3118 StringRef Name; 3119 std::tie(Name, PipelineText) = PipelineText.split(','); 3120 if (!parseAAPassName(AA, Name)) 3121 return make_error<StringError>( 3122 formatv("unknown alias analysis name '{0}'", Name).str(), 3123 inconvertibleErrorCode()); 3124 } 3125 3126 return Error::success(); 3127 } 3128 3129 bool PassBuilder::isAAPassName(StringRef PassName) { 3130 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 3131 if (PassName == NAME) \ 3132 return true; 3133 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 3134 if (PassName == NAME) \ 3135 return true; 3136 #include "PassRegistry.def" 3137 return false; 3138 } 3139 3140 bool PassBuilder::isAnalysisPassName(StringRef PassName) { 3141 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ 3142 if (PassName == NAME) \ 3143 return true; 3144 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ 3145 if (PassName == NAME) \ 3146 return true; 3147 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ 3148 if (PassName == NAME) \ 3149 return true; 3150 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ 3151 if (PassName == NAME) \ 3152 return true; 3153 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 3154 if (PassName == NAME) \ 3155 return true; 3156 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ 3157 if (PassName == NAME) \ 3158 return true; 3159 #include "PassRegistry.def" 3160 return false; 3161 } 3162 3163 static void printPassName(StringRef PassName, raw_ostream &OS) { 3164 OS << " " << PassName << "\n"; 3165 } 3166 static void printPassName(StringRef PassName, StringRef Params, 3167 raw_ostream &OS) { 3168 OS << " " << PassName << "<" << Params << ">\n"; 3169 } 3170 3171 void PassBuilder::printPassNames(raw_ostream &OS) { 3172 // TODO: print pass descriptions when they are available 3173 3174 OS << "Module passes:\n"; 3175 #define MODULE_PASS(NAME, CREATE_PASS) printPassName(NAME, OS); 3176 #include "PassRegistry.def" 3177 3178 OS << "Module analyses:\n"; 3179 #define MODULE_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3180 #include "PassRegistry.def" 3181 3182 OS << "Module alias analyses:\n"; 3183 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3184 #include "PassRegistry.def" 3185 3186 OS << "CGSCC passes:\n"; 3187 #define CGSCC_PASS(NAME, CREATE_PASS) printPassName(NAME, OS); 3188 #include "PassRegistry.def" 3189 3190 OS << "CGSCC analyses:\n"; 3191 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3192 #include "PassRegistry.def" 3193 3194 OS << "Function passes:\n"; 3195 #define FUNCTION_PASS(NAME, CREATE_PASS) printPassName(NAME, OS); 3196 #include "PassRegistry.def" 3197 3198 OS << "Function passes with params:\n"; 3199 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 3200 printPassName(NAME, PARAMS, OS); 3201 #include "PassRegistry.def" 3202 3203 OS << "Function analyses:\n"; 3204 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3205 #include "PassRegistry.def" 3206 3207 OS << "Function alias analyses:\n"; 3208 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3209 #include "PassRegistry.def" 3210 3211 OS << "Loop passes:\n"; 3212 #define LOOP_PASS(NAME, CREATE_PASS) printPassName(NAME, OS); 3213 #include "PassRegistry.def" 3214 3215 OS << "Loop passes with params:\n"; 3216 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \ 3217 printPassName(NAME, PARAMS, OS); 3218 #include "PassRegistry.def" 3219 3220 OS << "Loop analyses:\n"; 3221 #define LOOP_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS); 3222 #include "PassRegistry.def" 3223 } 3224 3225 void PassBuilder::registerParseTopLevelPipelineCallback( 3226 const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)> 3227 &C) { 3228 TopLevelPipelineParsingCallbacks.push_back(C); 3229 } 3230