1349cc55cSDimitry Andric //===- Construction of pass pipelines -------------------------------------===// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8349cc55cSDimitry Andric /// \file 9349cc55cSDimitry Andric /// 10349cc55cSDimitry Andric /// This file provides the implementation of the PassBuilder based on our 11349cc55cSDimitry Andric /// static pass registry as well as related functionality. It also provides 12349cc55cSDimitry Andric /// helpers to aid in analyzing, debugging, and testing passes and pass 13349cc55cSDimitry Andric /// pipelines. 14349cc55cSDimitry Andric /// 15349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 16349cc55cSDimitry Andric 17349cc55cSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 18349cc55cSDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h" 19349cc55cSDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h" 20349cc55cSDimitry Andric #include "llvm/Analysis/GlobalsModRef.h" 21349cc55cSDimitry Andric #include "llvm/Analysis/InlineAdvisor.h" 22349cc55cSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 23349cc55cSDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h" 24349cc55cSDimitry Andric #include "llvm/Analysis/ScopedNoAliasAA.h" 25349cc55cSDimitry Andric #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 26349cc55cSDimitry Andric #include "llvm/IR/PassManager.h" 27349cc55cSDimitry Andric #include "llvm/Passes/OptimizationLevel.h" 28349cc55cSDimitry Andric #include "llvm/Passes/PassBuilder.h" 29349cc55cSDimitry Andric #include "llvm/Support/CommandLine.h" 30349cc55cSDimitry Andric #include "llvm/Support/ErrorHandling.h" 31349cc55cSDimitry Andric #include "llvm/Support/PGOOptions.h" 32349cc55cSDimitry Andric #include "llvm/Target/TargetMachine.h" 33349cc55cSDimitry Andric #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" 34349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroCleanup.h" 3581ad6265SDimitry Andric #include "llvm/Transforms/Coroutines/CoroConditionalWrapper.h" 36349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroEarly.h" 37349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroElide.h" 38349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroSplit.h" 39349cc55cSDimitry Andric #include "llvm/Transforms/IPO/AlwaysInliner.h" 40349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Annotation2Metadata.h" 41349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ArgumentPromotion.h" 42349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Attributor.h" 43349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CalledValuePropagation.h" 44349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ConstantMerge.h" 45349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CrossDSOCFI.h" 46349cc55cSDimitry Andric #include "llvm/Transforms/IPO/DeadArgumentElimination.h" 47349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ElimAvailExtern.h" 48349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 49349cc55cSDimitry Andric #include "llvm/Transforms/IPO/FunctionAttrs.h" 50349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalDCE.h" 51349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalOpt.h" 52349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalSplit.h" 53349cc55cSDimitry Andric #include "llvm/Transforms/IPO/HotColdSplitting.h" 54349cc55cSDimitry Andric #include "llvm/Transforms/IPO/IROutliner.h" 55349cc55cSDimitry Andric #include "llvm/Transforms/IPO/InferFunctionAttrs.h" 56349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Inliner.h" 57349cc55cSDimitry Andric #include "llvm/Transforms/IPO/LowerTypeTests.h" 58349cc55cSDimitry Andric #include "llvm/Transforms/IPO/MergeFunctions.h" 59349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ModuleInliner.h" 60349cc55cSDimitry Andric #include "llvm/Transforms/IPO/OpenMPOpt.h" 61349cc55cSDimitry Andric #include "llvm/Transforms/IPO/PartialInlining.h" 62349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SCCP.h" 63349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfile.h" 64349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfileProbe.h" 65349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" 66349cc55cSDimitry Andric #include "llvm/Transforms/IPO/WholeProgramDevirt.h" 67349cc55cSDimitry Andric #include "llvm/Transforms/InstCombine/InstCombine.h" 68349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/CGProfile.h" 69349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h" 70349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrOrderFile.h" 71349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 72349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/MemProfiler.h" 73349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 74349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ADCE.h" 75349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" 76349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AnnotationRemarks.h" 77349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/BDCE.h" 78349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CallSiteSplitting.h" 79349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ConstraintElimination.h" 80349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" 81349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DFAJumpThreading.h" 82349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DeadStoreElimination.h" 83349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DivRemPairs.h" 84349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/EarlyCSE.h" 85349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Float2Int.h" 86349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/GVN.h" 87349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/IndVarSimplify.h" 88349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/InstSimplifyPass.h" 89349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/JumpThreading.h" 90349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LICM.h" 91349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDeletion.h" 92349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDistribute.h" 93349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopFlatten.h" 94349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" 95349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 96349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInterchange.h" 97349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopLoadElimination.h" 98349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h" 99349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopRotation.h" 100349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 101349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSink.h" 102349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" 103349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollPass.h" 104349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h" 105349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 106349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h" 107349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" 108349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" 109349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/NewGVN.h" 110349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Reassociate.h" 111349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SCCP.h" 112349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SROA.h" 113349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 114349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimplifyCFG.h" 115349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SpeculativeExecution.h" 116349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/TailRecursionElimination.h" 117349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" 118349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AddDiscriminators.h" 119349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 120349cc55cSDimitry Andric #include "llvm/Transforms/Utils/CanonicalizeAliases.h" 121349cc55cSDimitry Andric #include "llvm/Transforms/Utils/InjectTLIMappings.h" 122349cc55cSDimitry Andric #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" 123349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Mem2Reg.h" 124349cc55cSDimitry Andric #include "llvm/Transforms/Utils/NameAnonGlobals.h" 125349cc55cSDimitry Andric #include "llvm/Transforms/Utils/RelLookupTableConverter.h" 126349cc55cSDimitry Andric #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" 127349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/LoopVectorize.h" 128349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/SLPVectorizer.h" 129349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/VectorCombine.h" 130349cc55cSDimitry Andric 131349cc55cSDimitry Andric using namespace llvm; 132349cc55cSDimitry Andric 133349cc55cSDimitry Andric static cl::opt<InliningAdvisorMode> UseInlineAdvisor( 134349cc55cSDimitry Andric "enable-ml-inliner", cl::init(InliningAdvisorMode::Default), cl::Hidden, 135349cc55cSDimitry Andric cl::desc("Enable ML policy for inliner. Currently trained for -Oz only"), 136349cc55cSDimitry Andric cl::values(clEnumValN(InliningAdvisorMode::Default, "default", 137*bdd1243dSDimitry Andric "Heuristics-based inliner version"), 138349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Development, "development", 139*bdd1243dSDimitry Andric "Use development mode (runtime-loadable model)"), 140349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Release, "release", 141*bdd1243dSDimitry Andric "Use release mode (AOT-compiled model)"))); 142349cc55cSDimitry Andric 143349cc55cSDimitry Andric static cl::opt<bool> EnableSyntheticCounts( 14481ad6265SDimitry Andric "enable-npm-synthetic-counts", cl::Hidden, 145349cc55cSDimitry Andric cl::desc("Run synthetic function entry count generation " 146349cc55cSDimitry Andric "pass")); 147349cc55cSDimitry Andric 148349cc55cSDimitry Andric /// Flag to enable inline deferral during PGO. 149349cc55cSDimitry Andric static cl::opt<bool> 150349cc55cSDimitry Andric EnablePGOInlineDeferral("enable-npm-pgo-inline-deferral", cl::init(true), 151349cc55cSDimitry Andric cl::Hidden, 152349cc55cSDimitry Andric cl::desc("Enable inline deferral during PGO")); 153349cc55cSDimitry Andric 15481ad6265SDimitry Andric static cl::opt<bool> EnableMemProfiler("enable-mem-prof", cl::Hidden, 155349cc55cSDimitry Andric cl::desc("Enable memory profiler")); 156349cc55cSDimitry Andric 157349cc55cSDimitry Andric static cl::opt<bool> EnableModuleInliner("enable-module-inliner", 158349cc55cSDimitry Andric cl::init(false), cl::Hidden, 159349cc55cSDimitry Andric cl::desc("Enable module inliner")); 160349cc55cSDimitry Andric 161349cc55cSDimitry Andric static cl::opt<bool> PerformMandatoryInliningsFirst( 16281ad6265SDimitry Andric "mandatory-inlining-first", cl::init(true), cl::Hidden, 163349cc55cSDimitry Andric cl::desc("Perform mandatory inlinings module-wide, before performing " 164*bdd1243dSDimitry Andric "inlining")); 165349cc55cSDimitry Andric 166349cc55cSDimitry Andric static cl::opt<bool> EnableO3NonTrivialUnswitching( 167349cc55cSDimitry Andric "enable-npm-O3-nontrivial-unswitch", cl::init(true), cl::Hidden, 16881ad6265SDimitry Andric cl::desc("Enable non-trivial loop unswitching for -O3")); 169349cc55cSDimitry Andric 170349cc55cSDimitry Andric static cl::opt<bool> EnableEagerlyInvalidateAnalyses( 171349cc55cSDimitry Andric "eagerly-invalidate-analyses", cl::init(true), cl::Hidden, 172349cc55cSDimitry Andric cl::desc("Eagerly invalidate more analyses in default pipelines")); 173349cc55cSDimitry Andric 174349cc55cSDimitry Andric static cl::opt<bool> EnableNoRerunSimplificationPipeline( 175fcaf7f86SDimitry Andric "enable-no-rerun-simplification-pipeline", cl::init(true), cl::Hidden, 176349cc55cSDimitry Andric cl::desc( 177349cc55cSDimitry Andric "Prevent running the simplification pipeline on a function more " 178349cc55cSDimitry Andric "than once in the case that SCC mutations cause a function to be " 179349cc55cSDimitry Andric "visited multiple times as long as the function has not been changed")); 180349cc55cSDimitry Andric 1810eae32dcSDimitry Andric static cl::opt<bool> EnableMergeFunctions( 1820eae32dcSDimitry Andric "enable-merge-functions", cl::init(false), cl::Hidden, 1830eae32dcSDimitry Andric cl::desc("Enable function merging as part of the optimization pipeline")); 1840eae32dcSDimitry Andric 185*bdd1243dSDimitry Andric static cl::opt<bool> EnablePostPGOLoopRotation( 186*bdd1243dSDimitry Andric "enable-post-pgo-loop-rotation", cl::init(true), cl::Hidden, 187*bdd1243dSDimitry Andric cl::desc("Run the loop rotation transformation after PGO instrumentation")); 188*bdd1243dSDimitry Andric 189*bdd1243dSDimitry Andric static cl::opt<bool> EnableGlobalAnalyses( 190*bdd1243dSDimitry Andric "enable-global-analyses", cl::init(true), cl::Hidden, 191*bdd1243dSDimitry Andric cl::desc("Enable inter-procedural analyses")); 192*bdd1243dSDimitry Andric 193*bdd1243dSDimitry Andric static cl::opt<bool> 194*bdd1243dSDimitry Andric RunPartialInlining("enable-partial-inlining", cl::init(false), cl::Hidden, 195*bdd1243dSDimitry Andric cl::desc("Run Partial inlinining pass")); 196*bdd1243dSDimitry Andric 197*bdd1243dSDimitry Andric static cl::opt<bool> ExtraVectorizerPasses( 198*bdd1243dSDimitry Andric "extra-vectorizer-passes", cl::init(false), cl::Hidden, 199*bdd1243dSDimitry Andric cl::desc("Run cleanup optimization passes after vectorization")); 200*bdd1243dSDimitry Andric 201*bdd1243dSDimitry Andric static cl::opt<bool> RunNewGVN("enable-newgvn", cl::init(false), cl::Hidden, 202*bdd1243dSDimitry Andric cl::desc("Run the NewGVN pass")); 203*bdd1243dSDimitry Andric 204*bdd1243dSDimitry Andric static cl::opt<bool> EnableLoopInterchange( 205*bdd1243dSDimitry Andric "enable-loopinterchange", cl::init(false), cl::Hidden, 206*bdd1243dSDimitry Andric cl::desc("Enable the experimental LoopInterchange Pass")); 207*bdd1243dSDimitry Andric 208*bdd1243dSDimitry Andric static cl::opt<bool> EnableUnrollAndJam("enable-unroll-and-jam", 209*bdd1243dSDimitry Andric cl::init(false), cl::Hidden, 210*bdd1243dSDimitry Andric cl::desc("Enable Unroll And Jam Pass")); 211*bdd1243dSDimitry Andric 212*bdd1243dSDimitry Andric static cl::opt<bool> EnableLoopFlatten("enable-loop-flatten", cl::init(false), 213*bdd1243dSDimitry Andric cl::Hidden, 214*bdd1243dSDimitry Andric cl::desc("Enable the LoopFlatten Pass")); 215*bdd1243dSDimitry Andric 216*bdd1243dSDimitry Andric static cl::opt<bool> 217*bdd1243dSDimitry Andric EnableDFAJumpThreading("enable-dfa-jump-thread", 218*bdd1243dSDimitry Andric cl::desc("Enable DFA jump threading"), 219*bdd1243dSDimitry Andric cl::init(false), cl::Hidden); 220*bdd1243dSDimitry Andric 221*bdd1243dSDimitry Andric static cl::opt<bool> 222*bdd1243dSDimitry Andric EnableHotColdSplit("hot-cold-split", 223*bdd1243dSDimitry Andric cl::desc("Enable hot-cold splitting pass")); 224*bdd1243dSDimitry Andric 225*bdd1243dSDimitry Andric static cl::opt<bool> EnableIROutliner("ir-outliner", cl::init(false), 226*bdd1243dSDimitry Andric cl::Hidden, 227*bdd1243dSDimitry Andric cl::desc("Enable ir outliner pass")); 228*bdd1243dSDimitry Andric 229*bdd1243dSDimitry Andric static cl::opt<bool> 230*bdd1243dSDimitry Andric DisablePreInliner("disable-preinline", cl::init(false), cl::Hidden, 231*bdd1243dSDimitry Andric cl::desc("Disable pre-instrumentation inliner")); 232*bdd1243dSDimitry Andric 233*bdd1243dSDimitry Andric static cl::opt<int> PreInlineThreshold( 234*bdd1243dSDimitry Andric "preinline-threshold", cl::Hidden, cl::init(75), 235*bdd1243dSDimitry Andric cl::desc("Control the amount of inlining in pre-instrumentation inliner " 236*bdd1243dSDimitry Andric "(default = 75)")); 237*bdd1243dSDimitry Andric 238*bdd1243dSDimitry Andric static cl::opt<bool> 239*bdd1243dSDimitry Andric EnableGVNHoist("enable-gvn-hoist", 240*bdd1243dSDimitry Andric cl::desc("Enable the GVN hoisting pass (default = off)")); 241*bdd1243dSDimitry Andric 242*bdd1243dSDimitry Andric static cl::opt<bool> 243*bdd1243dSDimitry Andric EnableGVNSink("enable-gvn-sink", 244*bdd1243dSDimitry Andric cl::desc("Enable the GVN sinking pass (default = off)")); 245*bdd1243dSDimitry Andric 246*bdd1243dSDimitry Andric // This option is used in simplifying testing SampleFDO optimizations for 247*bdd1243dSDimitry Andric // profile loading. 248*bdd1243dSDimitry Andric static cl::opt<bool> 249*bdd1243dSDimitry Andric EnableCHR("enable-chr", cl::init(true), cl::Hidden, 250*bdd1243dSDimitry Andric cl::desc("Enable control height reduction optimization (CHR)")); 251*bdd1243dSDimitry Andric 252*bdd1243dSDimitry Andric static cl::opt<bool> FlattenedProfileUsed( 253*bdd1243dSDimitry Andric "flattened-profile-used", cl::init(false), cl::Hidden, 254*bdd1243dSDimitry Andric cl::desc("Indicate the sample profile being used is flattened, i.e., " 255*bdd1243dSDimitry Andric "no inline hierachy exists in the profile")); 256*bdd1243dSDimitry Andric 257*bdd1243dSDimitry Andric static cl::opt<bool> EnableOrderFileInstrumentation( 258*bdd1243dSDimitry Andric "enable-order-file-instrumentation", cl::init(false), cl::Hidden, 259*bdd1243dSDimitry Andric cl::desc("Enable order file instrumentation (default = off)")); 260*bdd1243dSDimitry Andric 261*bdd1243dSDimitry Andric static cl::opt<bool> 262*bdd1243dSDimitry Andric EnableMatrix("enable-matrix", cl::init(false), cl::Hidden, 263*bdd1243dSDimitry Andric cl::desc("Enable lowering of the matrix intrinsics")); 264*bdd1243dSDimitry Andric 265*bdd1243dSDimitry Andric static cl::opt<bool> EnableConstraintElimination( 266*bdd1243dSDimitry Andric "enable-constraint-elimination", cl::init(false), cl::Hidden, 267*bdd1243dSDimitry Andric cl::desc( 268*bdd1243dSDimitry Andric "Enable pass to eliminate conditions based on linear constraints")); 269*bdd1243dSDimitry Andric 270*bdd1243dSDimitry Andric static cl::opt<AttributorRunOption> AttributorRun( 271*bdd1243dSDimitry Andric "attributor-enable", cl::Hidden, cl::init(AttributorRunOption::NONE), 272*bdd1243dSDimitry Andric cl::desc("Enable the attributor inter-procedural deduction pass"), 273*bdd1243dSDimitry Andric cl::values(clEnumValN(AttributorRunOption::ALL, "all", 274*bdd1243dSDimitry Andric "enable all attributor runs"), 275*bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::MODULE, "module", 276*bdd1243dSDimitry Andric "enable module-wide attributor runs"), 277*bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::CGSCC, "cgscc", 278*bdd1243dSDimitry Andric "enable call graph SCC attributor runs"), 279*bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::NONE, "none", 280*bdd1243dSDimitry Andric "disable attributor runs"))); 281*bdd1243dSDimitry Andric 282349cc55cSDimitry Andric PipelineTuningOptions::PipelineTuningOptions() { 283349cc55cSDimitry Andric LoopInterleaving = true; 284349cc55cSDimitry Andric LoopVectorization = true; 285349cc55cSDimitry Andric SLPVectorization = false; 286349cc55cSDimitry Andric LoopUnrolling = true; 287349cc55cSDimitry Andric ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll; 288349cc55cSDimitry Andric LicmMssaOptCap = SetLicmMssaOptCap; 289349cc55cSDimitry Andric LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; 290349cc55cSDimitry Andric CallGraphProfile = true; 2910eae32dcSDimitry Andric MergeFunctions = EnableMergeFunctions; 292*bdd1243dSDimitry Andric InlinerThreshold = -1; 293349cc55cSDimitry Andric EagerlyInvalidateAnalyses = EnableEagerlyInvalidateAnalyses; 294349cc55cSDimitry Andric } 295349cc55cSDimitry Andric 296349cc55cSDimitry Andric namespace llvm { 297349cc55cSDimitry Andric extern cl::opt<unsigned> MaxDevirtIterations; 298349cc55cSDimitry Andric extern cl::opt<bool> EnableKnowledgeRetention; 299349cc55cSDimitry Andric } // namespace llvm 300349cc55cSDimitry Andric 301349cc55cSDimitry Andric void PassBuilder::invokePeepholeEPCallbacks(FunctionPassManager &FPM, 302349cc55cSDimitry Andric OptimizationLevel Level) { 303349cc55cSDimitry Andric for (auto &C : PeepholeEPCallbacks) 304349cc55cSDimitry Andric C(FPM, Level); 305349cc55cSDimitry Andric } 306349cc55cSDimitry Andric 307349cc55cSDimitry Andric // Helper to add AnnotationRemarksPass. 308349cc55cSDimitry Andric static void addAnnotationRemarksPass(ModulePassManager &MPM) { 30981ad6265SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass())); 310349cc55cSDimitry Andric } 311349cc55cSDimitry Andric 312349cc55cSDimitry Andric // Helper to check if the current compilation phase is preparing for LTO 313349cc55cSDimitry Andric static bool isLTOPreLink(ThinOrFullLTOPhase Phase) { 314349cc55cSDimitry Andric return Phase == ThinOrFullLTOPhase::ThinLTOPreLink || 315349cc55cSDimitry Andric Phase == ThinOrFullLTOPhase::FullLTOPreLink; 316349cc55cSDimitry Andric } 317349cc55cSDimitry Andric 318349cc55cSDimitry Andric // TODO: Investigate the cost/benefit of tail call elimination on debugging. 319349cc55cSDimitry Andric FunctionPassManager 320349cc55cSDimitry Andric PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, 321349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 322349cc55cSDimitry Andric 323349cc55cSDimitry Andric FunctionPassManager FPM; 324349cc55cSDimitry Andric 325349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 326349cc55cSDimitry Andric // scalars. 327*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 328349cc55cSDimitry Andric 329349cc55cSDimitry Andric // Catch trivial redundancies 330349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 331349cc55cSDimitry Andric 332349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 333fb03ea46SDimitry Andric FPM.addPass( 334fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 335349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 336349cc55cSDimitry Andric 337349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 338349cc55cSDimitry Andric 339349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 340349cc55cSDimitry Andric 341fb03ea46SDimitry Andric FPM.addPass( 342fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 343349cc55cSDimitry Andric 344349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 345349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 346349cc55cSDimitry Andric // minimal multiplication trees. 347349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 348349cc55cSDimitry Andric 349349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 350349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 351349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 352349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 353349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 354349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 355349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 356349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 357349cc55cSDimitry Andric // `LoopInstSimplify`. 358349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 359349cc55cSDimitry Andric 360349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 361349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 362349cc55cSDimitry Andric // implications on the outer loop. 363349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 364349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 365349cc55cSDimitry Andric 366349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 367fb03ea46SDimitry Andric // to reduce amount of IR that will have to be duplicated. However, 368fb03ea46SDimitry Andric // do not perform speculative hoisting the first time as LICM 369fb03ea46SDimitry Andric // will destroy metadata that may not need to be destroyed if run 370fb03ea46SDimitry Andric // after loop rotation. 371349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 372fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 373fb03ea46SDimitry Andric /*AllowSpeculation=*/false)); 374349cc55cSDimitry Andric 375349cc55cSDimitry Andric LPM1.addPass(LoopRotatePass(/* Disable header duplication */ true, 376349cc55cSDimitry Andric isLTOPreLink(Phase))); 377349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 378fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 379fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 380349cc55cSDimitry Andric LPM1.addPass(SimpleLoopUnswitchPass()); 38104eeddc0SDimitry Andric if (EnableLoopFlatten) 38204eeddc0SDimitry Andric LPM1.addPass(LoopFlattenPass()); 383349cc55cSDimitry Andric 384349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 385349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 386349cc55cSDimitry Andric 387349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 388349cc55cSDimitry Andric C(LPM2, Level); 389349cc55cSDimitry Andric 390349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 391349cc55cSDimitry Andric 392349cc55cSDimitry Andric if (EnableLoopInterchange) 393349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 394349cc55cSDimitry Andric 395349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 396349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 397349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 398349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 399349cc55cSDimitry Andric // attention to it. 400349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 401349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 402349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 403349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 404349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 405349cc55cSDimitry Andric 406349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 407349cc55cSDimitry Andric C(LPM2, Level); 408349cc55cSDimitry Andric 409349cc55cSDimitry Andric // We provide the opt remark emitter pass for LICM to use. We only need to do 410349cc55cSDimitry Andric // this once as it is immutable. 411349cc55cSDimitry Andric FPM.addPass( 412349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 413349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 414349cc55cSDimitry Andric /*UseMemorySSA=*/true, 415349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 416fb03ea46SDimitry Andric FPM.addPass( 417fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 418349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 419349cc55cSDimitry Andric // The loop passes in LPM2 (LoopFullUnrollPass) do not preserve MemorySSA. 420349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 421349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 422349cc55cSDimitry Andric /*UseMemorySSA=*/false, 423349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 424349cc55cSDimitry Andric 425349cc55cSDimitry Andric // Delete small array after loop unroll. 426*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 427349cc55cSDimitry Andric 428349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 429349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 430349cc55cSDimitry Andric 431349cc55cSDimitry Andric // Sparse conditional constant propagation. 432349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 433349cc55cSDimitry Andric // before... 434349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 435349cc55cSDimitry Andric 436349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 437349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 438349cc55cSDimitry Andric // opportunities that creates). 439349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 440349cc55cSDimitry Andric 441349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 442349cc55cSDimitry Andric // opportunities opened up by them. 443349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 444349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 445349cc55cSDimitry Andric 446349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 447349cc55cSDimitry Andric 448349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 449349cc55cSDimitry Andric C(FPM, Level); 450349cc55cSDimitry Andric 451349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 452349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 453349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 454349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 455fb03ea46SDimitry Andric FPM.addPass( 456fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 457349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 458349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 459349cc55cSDimitry Andric 460349cc55cSDimitry Andric return FPM; 461349cc55cSDimitry Andric } 462349cc55cSDimitry Andric 463349cc55cSDimitry Andric FunctionPassManager 464349cc55cSDimitry Andric PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, 465349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 466349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Must request optimizations!"); 467349cc55cSDimitry Andric 468349cc55cSDimitry Andric // The O1 pipeline has a separate pipeline creation function to simplify 469349cc55cSDimitry Andric // construction readability. 470349cc55cSDimitry Andric if (Level.getSpeedupLevel() == 1) 471349cc55cSDimitry Andric return buildO1FunctionSimplificationPipeline(Level, Phase); 472349cc55cSDimitry Andric 473349cc55cSDimitry Andric FunctionPassManager FPM; 474349cc55cSDimitry Andric 475349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 476349cc55cSDimitry Andric // scalars. 477*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 478349cc55cSDimitry Andric 479349cc55cSDimitry Andric // Catch trivial redundancies 480349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 481349cc55cSDimitry Andric if (EnableKnowledgeRetention) 482349cc55cSDimitry Andric FPM.addPass(AssumeSimplifyPass()); 483349cc55cSDimitry Andric 484349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 485349cc55cSDimitry Andric if (EnableGVNHoist) 486349cc55cSDimitry Andric FPM.addPass(GVNHoistPass()); 487349cc55cSDimitry Andric 488349cc55cSDimitry Andric // Global value numbering based sinking. 489349cc55cSDimitry Andric if (EnableGVNSink) { 490349cc55cSDimitry Andric FPM.addPass(GVNSinkPass()); 491fb03ea46SDimitry Andric FPM.addPass( 492fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 493349cc55cSDimitry Andric } 494349cc55cSDimitry Andric 495349cc55cSDimitry Andric // Speculative execution if the target has divergent branches; otherwise nop. 496349cc55cSDimitry Andric FPM.addPass(SpeculativeExecutionPass(/* OnlyIfDivergentTarget =*/true)); 497349cc55cSDimitry Andric 498349cc55cSDimitry Andric // Optimize based on known information about branches, and cleanup afterward. 499349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 500349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 501349cc55cSDimitry Andric 502fb03ea46SDimitry Andric FPM.addPass( 503fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 5040eae32dcSDimitry Andric FPM.addPass(InstCombinePass()); 505349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 506349cc55cSDimitry Andric FPM.addPass(AggressiveInstCombinePass()); 507349cc55cSDimitry Andric 508*bdd1243dSDimitry Andric if (EnableConstraintElimination) 509*bdd1243dSDimitry Andric FPM.addPass(ConstraintEliminationPass()); 510*bdd1243dSDimitry Andric 511349cc55cSDimitry Andric if (!Level.isOptimizingForSize()) 512349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 513349cc55cSDimitry Andric 514349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 515349cc55cSDimitry Andric 516349cc55cSDimitry Andric // For PGO use pipeline, try to optimize memory intrinsics such as memcpy 517349cc55cSDimitry Andric // using the size value profile. Don't perform this when optimizing for size. 518349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse && 519349cc55cSDimitry Andric !Level.isOptimizingForSize()) 520349cc55cSDimitry Andric FPM.addPass(PGOMemOPSizeOpt()); 521349cc55cSDimitry Andric 522349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 523fb03ea46SDimitry Andric FPM.addPass( 524fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 525349cc55cSDimitry Andric 526349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 527349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 528349cc55cSDimitry Andric // minimal multiplication trees. 529349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 530349cc55cSDimitry Andric 531349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 532349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 533349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 534349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 535349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 536349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 537349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 538349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 539349cc55cSDimitry Andric // `LoopInstSimplify`. 540349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 541349cc55cSDimitry Andric 542349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 543349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 544349cc55cSDimitry Andric // implications on the outer loop. 545349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 546349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 547349cc55cSDimitry Andric 548349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 549fb03ea46SDimitry Andric // to reduce amount of IR that will have to be duplicated. However, 550fb03ea46SDimitry Andric // do not perform speculative hoisting the first time as LICM 551fb03ea46SDimitry Andric // will destroy metadata that may not need to be destroyed if run 552fb03ea46SDimitry Andric // after loop rotation. 553349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 554fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 555fb03ea46SDimitry Andric /*AllowSpeculation=*/false)); 556349cc55cSDimitry Andric 557349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 558349cc55cSDimitry Andric LPM1.addPass( 559349cc55cSDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz, isLTOPreLink(Phase))); 560349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 561fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 562fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 563349cc55cSDimitry Andric LPM1.addPass( 564349cc55cSDimitry Andric SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 && 565349cc55cSDimitry Andric EnableO3NonTrivialUnswitching)); 56604eeddc0SDimitry Andric if (EnableLoopFlatten) 56704eeddc0SDimitry Andric LPM1.addPass(LoopFlattenPass()); 56804eeddc0SDimitry Andric 569349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 570349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 571349cc55cSDimitry Andric 572349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 573349cc55cSDimitry Andric C(LPM2, Level); 574349cc55cSDimitry Andric 575349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 576349cc55cSDimitry Andric 577349cc55cSDimitry Andric if (EnableLoopInterchange) 578349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 579349cc55cSDimitry Andric 580349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 581349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 582349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 583349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 584349cc55cSDimitry Andric // attention to it. 585349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 586349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 587349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 588349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 589349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 590349cc55cSDimitry Andric 591349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 592349cc55cSDimitry Andric C(LPM2, Level); 593349cc55cSDimitry Andric 594349cc55cSDimitry Andric // We provide the opt remark emitter pass for LICM to use. We only need to do 595349cc55cSDimitry Andric // this once as it is immutable. 596349cc55cSDimitry Andric FPM.addPass( 597349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 598349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 599349cc55cSDimitry Andric /*UseMemorySSA=*/true, 600349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 601fb03ea46SDimitry Andric FPM.addPass( 602fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 603349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 604349cc55cSDimitry Andric // The loop passes in LPM2 (LoopIdiomRecognizePass, IndVarSimplifyPass, 605349cc55cSDimitry Andric // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA. 606349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 607349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 608349cc55cSDimitry Andric /*UseMemorySSA=*/false, 609349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 610349cc55cSDimitry Andric 611349cc55cSDimitry Andric // Delete small array after loop unroll. 612*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 613349cc55cSDimitry Andric 614*bdd1243dSDimitry Andric // Try vectorization/scalarization transforms that are both improvements 615*bdd1243dSDimitry Andric // themselves and can allow further folds with GVN and InstCombine. 616*bdd1243dSDimitry Andric FPM.addPass(VectorCombinePass(/*TryEarlyFoldsOnly=*/true)); 617349cc55cSDimitry Andric 618349cc55cSDimitry Andric // Eliminate redundancies. 619349cc55cSDimitry Andric FPM.addPass(MergedLoadStoreMotionPass()); 620349cc55cSDimitry Andric if (RunNewGVN) 621349cc55cSDimitry Andric FPM.addPass(NewGVNPass()); 622349cc55cSDimitry Andric else 623349cc55cSDimitry Andric FPM.addPass(GVNPass()); 624349cc55cSDimitry Andric 625349cc55cSDimitry Andric // Sparse conditional constant propagation. 626349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 627349cc55cSDimitry Andric // before... 628349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 629349cc55cSDimitry Andric 630349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 631349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 632349cc55cSDimitry Andric // opportunities that creates). 633349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 634349cc55cSDimitry Andric 635349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 636349cc55cSDimitry Andric // opportunities opened up by them. 637349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 638349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 639349cc55cSDimitry Andric 640349cc55cSDimitry Andric // Re-consider control flow based optimizations after redundancy elimination, 641349cc55cSDimitry Andric // redo DCE, etc. 642349cc55cSDimitry Andric if (EnableDFAJumpThreading && Level.getSizeLevel() == 0) 643349cc55cSDimitry Andric FPM.addPass(DFAJumpThreadingPass()); 644349cc55cSDimitry Andric 645349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 646349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 647349cc55cSDimitry Andric 648349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 649349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 650349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 651349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 652349cc55cSDimitry Andric 653349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 654349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 655349cc55cSDimitry Andric 656349cc55cSDimitry Andric FPM.addPass(DSEPass()); 657349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 658fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 659fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 660349cc55cSDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 661349cc55cSDimitry Andric 662349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 663349cc55cSDimitry Andric 664349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 665349cc55cSDimitry Andric C(FPM, Level); 666349cc55cSDimitry Andric 667fb03ea46SDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 668fb03ea46SDimitry Andric .convertSwitchRangeToICmp(true) 669fb03ea46SDimitry Andric .hoistCommonInsts(true) 670fb03ea46SDimitry Andric .sinkCommonInsts(true))); 671349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 672349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 673349cc55cSDimitry Andric 674*bdd1243dSDimitry Andric // Don't add CHR pass for CSIRInstr build in PostLink as the profile 675*bdd1243dSDimitry Andric // is still the same as the PreLink compilation. 676349cc55cSDimitry Andric if (EnableCHR && Level == OptimizationLevel::O3 && PGOOpt && 677*bdd1243dSDimitry Andric ((PGOOpt->Action == PGOOptions::IRUse && 678*bdd1243dSDimitry Andric (Phase != ThinOrFullLTOPhase::ThinLTOPostLink || 679*bdd1243dSDimitry Andric PGOOpt->CSAction != PGOOptions::CSIRInstr)) || 680349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse)) 681349cc55cSDimitry Andric FPM.addPass(ControlHeightReductionPass()); 682349cc55cSDimitry Andric 683349cc55cSDimitry Andric return FPM; 684349cc55cSDimitry Andric } 685349cc55cSDimitry Andric 686349cc55cSDimitry Andric void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) { 687349cc55cSDimitry Andric MPM.addPass(CanonicalizeAliasesPass()); 688349cc55cSDimitry Andric MPM.addPass(NameAnonGlobalPass()); 689349cc55cSDimitry Andric } 690349cc55cSDimitry Andric 691349cc55cSDimitry Andric void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, 692349cc55cSDimitry Andric OptimizationLevel Level, bool RunProfileGen, 693349cc55cSDimitry Andric bool IsCS, std::string ProfileFile, 69481ad6265SDimitry Andric std::string ProfileRemappingFile, 69581ad6265SDimitry Andric ThinOrFullLTOPhase LTOPhase) { 696349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!"); 697349cc55cSDimitry Andric if (!IsCS && !DisablePreInliner) { 698349cc55cSDimitry Andric InlineParams IP; 699349cc55cSDimitry Andric 700349cc55cSDimitry Andric IP.DefaultThreshold = PreInlineThreshold; 701349cc55cSDimitry Andric 702349cc55cSDimitry Andric // FIXME: The hint threshold has the same value used by the regular inliner 703349cc55cSDimitry Andric // when not optimzing for size. This should probably be lowered after 704349cc55cSDimitry Andric // performance testing. 705349cc55cSDimitry Andric // FIXME: this comment is cargo culted from the old pass manager, revisit). 706349cc55cSDimitry Andric IP.HintThreshold = Level.isOptimizingForSize() ? PreInlineThreshold : 325; 70781ad6265SDimitry Andric ModuleInlinerWrapperPass MIWP( 70881ad6265SDimitry Andric IP, /* MandatoryFirst */ true, 70981ad6265SDimitry Andric InlineContext{LTOPhase, InlinePass::EarlyInliner}); 710349cc55cSDimitry Andric CGSCCPassManager &CGPipeline = MIWP.getPM(); 711349cc55cSDimitry Andric 712349cc55cSDimitry Andric FunctionPassManager FPM; 713*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 714349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies. 715fb03ea46SDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp( 716fb03ea46SDimitry Andric true))); // Merge & remove basic blocks. 717349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); // Combine silly sequences. 718349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 719349cc55cSDimitry Andric 720349cc55cSDimitry Andric CGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 721349cc55cSDimitry Andric std::move(FPM), PTO.EagerlyInvalidateAnalyses)); 722349cc55cSDimitry Andric 723349cc55cSDimitry Andric MPM.addPass(std::move(MIWP)); 724349cc55cSDimitry Andric 725349cc55cSDimitry Andric // Delete anything that is now dead to make sure that we don't instrument 726349cc55cSDimitry Andric // dead code. Instrumentation can end up keeping dead code around and 727349cc55cSDimitry Andric // dramatically increase code size. 728349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 729349cc55cSDimitry Andric } 730349cc55cSDimitry Andric 731349cc55cSDimitry Andric if (!RunProfileGen) { 732349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 733349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 734349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 735349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 736349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 737349cc55cSDimitry Andric return; 738349cc55cSDimitry Andric } 739349cc55cSDimitry Andric 740349cc55cSDimitry Andric // Perform PGO instrumentation. 741349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 742349cc55cSDimitry Andric 743*bdd1243dSDimitry Andric if (EnablePostPGOLoopRotation) { 744349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 74581ad6265SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 74681ad6265SDimitry Andric createFunctionToLoopPassAdaptor( 74781ad6265SDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz), 74881ad6265SDimitry Andric /*UseMemorySSA=*/false, 74981ad6265SDimitry Andric /*UseBlockFrequencyInfo=*/false), 750349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 751*bdd1243dSDimitry Andric } 752349cc55cSDimitry Andric 753349cc55cSDimitry Andric // Add the profile lowering pass. 754349cc55cSDimitry Andric InstrProfOptions Options; 755349cc55cSDimitry Andric if (!ProfileFile.empty()) 756349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 757349cc55cSDimitry Andric // Do counter promotion at Level greater than O0. 758349cc55cSDimitry Andric Options.DoCounterPromotion = true; 759349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 760349cc55cSDimitry Andric MPM.addPass(InstrProfiling(Options, IsCS)); 761349cc55cSDimitry Andric } 762349cc55cSDimitry Andric 763349cc55cSDimitry Andric void PassBuilder::addPGOInstrPassesForO0(ModulePassManager &MPM, 764349cc55cSDimitry Andric bool RunProfileGen, bool IsCS, 765349cc55cSDimitry Andric std::string ProfileFile, 766349cc55cSDimitry Andric std::string ProfileRemappingFile) { 767349cc55cSDimitry Andric if (!RunProfileGen) { 768349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 769349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 770349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 771349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 772349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 773349cc55cSDimitry Andric return; 774349cc55cSDimitry Andric } 775349cc55cSDimitry Andric 776349cc55cSDimitry Andric // Perform PGO instrumentation. 777349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 778349cc55cSDimitry Andric // Add the profile lowering pass. 779349cc55cSDimitry Andric InstrProfOptions Options; 780349cc55cSDimitry Andric if (!ProfileFile.empty()) 781349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 782349cc55cSDimitry Andric // Do not do counter promotion at O0. 783349cc55cSDimitry Andric Options.DoCounterPromotion = false; 784349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 785349cc55cSDimitry Andric MPM.addPass(InstrProfiling(Options, IsCS)); 786349cc55cSDimitry Andric } 787349cc55cSDimitry Andric 788349cc55cSDimitry Andric static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) { 789349cc55cSDimitry Andric return getInlineParams(Level.getSpeedupLevel(), Level.getSizeLevel()); 790349cc55cSDimitry Andric } 791349cc55cSDimitry Andric 792349cc55cSDimitry Andric ModuleInlinerWrapperPass 793349cc55cSDimitry Andric PassBuilder::buildInlinerPipeline(OptimizationLevel Level, 794349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 795*bdd1243dSDimitry Andric InlineParams IP; 796*bdd1243dSDimitry Andric if (PTO.InlinerThreshold == -1) 797*bdd1243dSDimitry Andric IP = getInlineParamsFromOptLevel(Level); 798*bdd1243dSDimitry Andric else 799*bdd1243dSDimitry Andric IP = getInlineParams(PTO.InlinerThreshold); 80081ad6265SDimitry Andric // For PreLinkThinLTO + SamplePGO, set hot-caller threshold to 0 to 80181ad6265SDimitry Andric // disable hot callsite inline (as much as possible [1]) because it makes 80281ad6265SDimitry Andric // profile annotation in the backend inaccurate. 80381ad6265SDimitry Andric // 80481ad6265SDimitry Andric // [1] Note the cost of a function could be below zero due to erased 80581ad6265SDimitry Andric // prologue / epilogue. 806349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 807349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 808349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 809349cc55cSDimitry Andric 810349cc55cSDimitry Andric if (PGOOpt) 811349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 812349cc55cSDimitry Andric 813*bdd1243dSDimitry Andric ModuleInlinerWrapperPass MIWP(IP, PerformMandatoryInliningsFirst, 81481ad6265SDimitry Andric InlineContext{Phase, InlinePass::CGSCCInliner}, 815349cc55cSDimitry Andric UseInlineAdvisor, MaxDevirtIterations); 816349cc55cSDimitry Andric 817349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 818349cc55cSDimitry Andric // the CGSCC pipeline. 819349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>()); 820349cc55cSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly available 821349cc55cSDimitry Andric // GlobalsAA. 822349cc55cSDimitry Andric MIWP.addModulePass( 823349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 824349cc55cSDimitry Andric 825349cc55cSDimitry Andric // Require the ProfileSummaryAnalysis for the module so we can query it within 826349cc55cSDimitry Andric // the inliner pass. 827349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 828349cc55cSDimitry Andric 829349cc55cSDimitry Andric // Now begin the main postorder CGSCC pipeline. 830349cc55cSDimitry Andric // FIXME: The current CGSCC pipeline has its origins in the legacy pass 831349cc55cSDimitry Andric // manager and trying to emulate its precise behavior. Much of this doesn't 832349cc55cSDimitry Andric // make a lot of sense and we should revisit the core CGSCC structure. 833349cc55cSDimitry Andric CGSCCPassManager &MainCGPipeline = MIWP.getPM(); 834349cc55cSDimitry Andric 835349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 836349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 837349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 838349cc55cSDimitry Andric // invoke or a call. 839349cc55cSDimitry Andric 840349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::CGSCC) 841349cc55cSDimitry Andric MainCGPipeline.addPass(AttributorCGSCCPass()); 842349cc55cSDimitry Andric 843349cc55cSDimitry Andric // Now deduce any function attributes based in the current code. 844349cc55cSDimitry Andric MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); 845349cc55cSDimitry Andric 846349cc55cSDimitry Andric // When at O3 add argument promotion to the pass pipeline. 847349cc55cSDimitry Andric // FIXME: It isn't at all clear why this should be limited to O3. 848349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 849349cc55cSDimitry Andric MainCGPipeline.addPass(ArgumentPromotionPass()); 850349cc55cSDimitry Andric 851349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations. This is a (quick!) no-op if 852349cc55cSDimitry Andric // there are no OpenMP runtime calls present in the module. 853349cc55cSDimitry Andric if (Level == OptimizationLevel::O2 || Level == OptimizationLevel::O3) 854349cc55cSDimitry Andric MainCGPipeline.addPass(OpenMPOptCGSCCPass()); 855349cc55cSDimitry Andric 856349cc55cSDimitry Andric for (auto &C : CGSCCOptimizerLateEPCallbacks) 857349cc55cSDimitry Andric C(MainCGPipeline, Level); 858349cc55cSDimitry Andric 859349cc55cSDimitry Andric // Lastly, add the core function simplification pipeline nested inside the 860349cc55cSDimitry Andric // CGSCC walk. 861349cc55cSDimitry Andric MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 862349cc55cSDimitry Andric buildFunctionSimplificationPipeline(Level, Phase), 863349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses, EnableNoRerunSimplificationPipeline)); 864349cc55cSDimitry Andric 865349cc55cSDimitry Andric MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0)); 866349cc55cSDimitry Andric 867349cc55cSDimitry Andric if (EnableNoRerunSimplificationPipeline) 868349cc55cSDimitry Andric MIWP.addLateModulePass(createModuleToFunctionPassAdaptor( 869349cc55cSDimitry Andric InvalidateAnalysisPass<ShouldNotRunFunctionPassesAnalysis>())); 870349cc55cSDimitry Andric 871349cc55cSDimitry Andric return MIWP; 872349cc55cSDimitry Andric } 873349cc55cSDimitry Andric 8740eae32dcSDimitry Andric ModulePassManager 875349cc55cSDimitry Andric PassBuilder::buildModuleInlinerPipeline(OptimizationLevel Level, 876349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 8770eae32dcSDimitry Andric ModulePassManager MPM; 8780eae32dcSDimitry Andric 879349cc55cSDimitry Andric InlineParams IP = getInlineParamsFromOptLevel(Level); 88081ad6265SDimitry Andric // For PreLinkThinLTO + SamplePGO, set hot-caller threshold to 0 to 88181ad6265SDimitry Andric // disable hot callsite inline (as much as possible [1]) because it makes 88281ad6265SDimitry Andric // profile annotation in the backend inaccurate. 88381ad6265SDimitry Andric // 88481ad6265SDimitry Andric // [1] Note the cost of a function could be below zero due to erased 88581ad6265SDimitry Andric // prologue / epilogue. 886349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 887349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 888349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 889349cc55cSDimitry Andric 890349cc55cSDimitry Andric if (PGOOpt) 891349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 892349cc55cSDimitry Andric 893349cc55cSDimitry Andric // The inline deferral logic is used to avoid losing some 894349cc55cSDimitry Andric // inlining chance in future. It is helpful in SCC inliner, in which 895349cc55cSDimitry Andric // inlining is processed in bottom-up order. 896349cc55cSDimitry Andric // While in module inliner, the inlining order is a priority-based order 897349cc55cSDimitry Andric // by default. The inline deferral is unnecessary there. So we disable the 898349cc55cSDimitry Andric // inline deferral logic in module inliner. 899349cc55cSDimitry Andric IP.EnableDeferral = false; 900349cc55cSDimitry Andric 90181ad6265SDimitry Andric MPM.addPass(ModuleInlinerPass(IP, UseInlineAdvisor, Phase)); 9020eae32dcSDimitry Andric 9030eae32dcSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 9040eae32dcSDimitry Andric buildFunctionSimplificationPipeline(Level, Phase), 9050eae32dcSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 9060eae32dcSDimitry Andric 9070eae32dcSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( 9080eae32dcSDimitry Andric CoroSplitPass(Level != OptimizationLevel::O0))); 9090eae32dcSDimitry Andric 9100eae32dcSDimitry Andric return MPM; 911349cc55cSDimitry Andric } 912349cc55cSDimitry Andric 913349cc55cSDimitry Andric ModulePassManager 914349cc55cSDimitry Andric PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, 915349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 916349cc55cSDimitry Andric ModulePassManager MPM; 917349cc55cSDimitry Andric 918349cc55cSDimitry Andric // Place pseudo probe instrumentation as the first pass of the pipeline to 919349cc55cSDimitry Andric // minimize the impact of optimization changes. 920349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 921349cc55cSDimitry Andric Phase != ThinOrFullLTOPhase::ThinLTOPostLink) 922349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 923349cc55cSDimitry Andric 924349cc55cSDimitry Andric bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse); 925349cc55cSDimitry Andric 926349cc55cSDimitry Andric // In ThinLTO mode, when flattened profile is used, all the available 927349cc55cSDimitry Andric // profile information will be annotated in PreLink phase so there is 928349cc55cSDimitry Andric // no need to load the profile again in PostLink. 929349cc55cSDimitry Andric bool LoadSampleProfile = 930349cc55cSDimitry Andric HasSampleProfile && 931349cc55cSDimitry Andric !(FlattenedProfileUsed && Phase == ThinOrFullLTOPhase::ThinLTOPostLink); 932349cc55cSDimitry Andric 933349cc55cSDimitry Andric // During the ThinLTO backend phase we perform early indirect call promotion 934349cc55cSDimitry Andric // here, before globalopt. Otherwise imported available_externally functions 935349cc55cSDimitry Andric // look unreferenced and are removed. If we are going to load the sample 936349cc55cSDimitry Andric // profile then defer until later. 937349cc55cSDimitry Andric // TODO: See if we can move later and consolidate with the location where 938349cc55cSDimitry Andric // we perform ICP when we are loading a sample profile. 939349cc55cSDimitry Andric // TODO: We pass HasSampleProfile (whether there was a sample profile file 940349cc55cSDimitry Andric // passed to the compile) to the SamplePGO flag of ICP. This is used to 941349cc55cSDimitry Andric // determine whether the new direct calls are annotated with prof metadata. 942349cc55cSDimitry Andric // Ideally this should be determined from whether the IR is annotated with 943349cc55cSDimitry Andric // sample profile, and not whether the a sample profile was provided on the 944349cc55cSDimitry Andric // command line. E.g. for flattened profiles where we will not be reloading 945349cc55cSDimitry Andric // the sample profile in the ThinLTO backend, we ideally shouldn't have to 946349cc55cSDimitry Andric // provide the sample profile file. 947349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink && !LoadSampleProfile) 948349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile)); 949349cc55cSDimitry Andric 950349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 951349cc55cSDimitry Andric // libraries and other oracles. 952349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 95381ad6265SDimitry Andric MPM.addPass(CoroEarlyPass()); 954349cc55cSDimitry Andric 955349cc55cSDimitry Andric // Create an early function pass manager to cleanup the output of the 956349cc55cSDimitry Andric // frontend. 957349cc55cSDimitry Andric FunctionPassManager EarlyFPM; 958349cc55cSDimitry Andric // Lower llvm.expect to metadata before attempting transforms. 959349cc55cSDimitry Andric // Compare/branch metadata may alter the behavior of passes like SimplifyCFG. 960349cc55cSDimitry Andric EarlyFPM.addPass(LowerExpectIntrinsicPass()); 961349cc55cSDimitry Andric EarlyFPM.addPass(SimplifyCFGPass()); 962*bdd1243dSDimitry Andric EarlyFPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 963349cc55cSDimitry Andric EarlyFPM.addPass(EarlyCSEPass()); 964349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 965349cc55cSDimitry Andric EarlyFPM.addPass(CallSiteSplittingPass()); 966349cc55cSDimitry Andric 967349cc55cSDimitry Andric // In SamplePGO ThinLTO backend, we need instcombine before profile annotation 968349cc55cSDimitry Andric // to convert bitcast to direct calls so that they can be inlined during the 969349cc55cSDimitry Andric // profile annotation prepration step. 970349cc55cSDimitry Andric // More details about SamplePGO design can be found in: 971349cc55cSDimitry Andric // https://research.google.com/pubs/pub45290.html 972349cc55cSDimitry Andric // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. 973349cc55cSDimitry Andric if (LoadSampleProfile) 974349cc55cSDimitry Andric EarlyFPM.addPass(InstCombinePass()); 975349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM), 976349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 977349cc55cSDimitry Andric 978349cc55cSDimitry Andric if (LoadSampleProfile) { 979349cc55cSDimitry Andric // Annotate sample profile right after early FPM to ensure freshness of 980349cc55cSDimitry Andric // the debug info. 981349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 982349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, Phase)); 983349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 984349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 985349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 986349cc55cSDimitry Andric // Do not invoke ICP in the LTOPrelink phase as it makes it hard 987349cc55cSDimitry Andric // for the profile annotation to be accurate in the LTO backend. 988349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink && 989349cc55cSDimitry Andric Phase != ThinOrFullLTOPhase::FullLTOPreLink) 990349cc55cSDimitry Andric // We perform early indirect call promotion here, before globalopt. 991349cc55cSDimitry Andric // This is important for the ThinLTO backend phase because otherwise 992349cc55cSDimitry Andric // imported available_externally functions look unreferenced and are 993349cc55cSDimitry Andric // removed. 994349cc55cSDimitry Andric MPM.addPass( 995349cc55cSDimitry Andric PGOIndirectCallPromotion(true /* IsInLTO */, true /* SamplePGO */)); 996349cc55cSDimitry Andric } 997349cc55cSDimitry Andric 998349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations on the module. This is a 999349cc55cSDimitry Andric // (quick!) no-op if there are no OpenMP runtime calls present in the module. 1000349cc55cSDimitry Andric if (Level != OptimizationLevel::O0) 1001349cc55cSDimitry Andric MPM.addPass(OpenMPOptPass()); 1002349cc55cSDimitry Andric 1003349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::MODULE) 1004349cc55cSDimitry Andric MPM.addPass(AttributorPass()); 1005349cc55cSDimitry Andric 1006349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic in the ThinLTO 1007349cc55cSDimitry Andric // post link pipeline after ICP. This is to enable usage of the type 1008349cc55cSDimitry Andric // tests in ICP sequences. 1009349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink) 1010349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1011349cc55cSDimitry Andric 1012349cc55cSDimitry Andric for (auto &C : PipelineEarlySimplificationEPCallbacks) 1013349cc55cSDimitry Andric C(MPM, Level); 1014349cc55cSDimitry Andric 1015349cc55cSDimitry Andric // Interprocedural constant propagation now that basic cleanup has occurred 1016349cc55cSDimitry Andric // and prior to optimizing globals. 1017349cc55cSDimitry Andric // FIXME: This position in the pipeline hasn't been carefully considered in 1018349cc55cSDimitry Andric // years, it should be re-analyzed. 1019*bdd1243dSDimitry Andric MPM.addPass(IPSCCPPass(IPSCCPOptions(/*AllowFuncSpec=*/ 1020*bdd1243dSDimitry Andric Level != OptimizationLevel::Os && 1021*bdd1243dSDimitry Andric Level != OptimizationLevel::Oz))); 1022349cc55cSDimitry Andric 1023349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 1024349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 1025349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 1026349cc55cSDimitry Andric 1027349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 1028349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1029349cc55cSDimitry Andric 1030349cc55cSDimitry Andric // Promote any localized globals to SSA registers. 1031349cc55cSDimitry Andric // FIXME: Should this instead by a run of SROA? 1032349cc55cSDimitry Andric // FIXME: We should probably run instcombine and simplifycfg afterward to 1033349cc55cSDimitry Andric // delete control flows that are dead once globals have been folded to 1034349cc55cSDimitry Andric // constants. 1035349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1036349cc55cSDimitry Andric 1037349cc55cSDimitry Andric // Create a small function pass pipeline to cleanup after all the global 1038349cc55cSDimitry Andric // optimizations. 1039349cc55cSDimitry Andric FunctionPassManager GlobalCleanupPM; 1040349cc55cSDimitry Andric GlobalCleanupPM.addPass(InstCombinePass()); 1041349cc55cSDimitry Andric invokePeepholeEPCallbacks(GlobalCleanupPM, Level); 1042349cc55cSDimitry Andric 1043fb03ea46SDimitry Andric GlobalCleanupPM.addPass( 1044fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 1045349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM), 1046349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1047349cc55cSDimitry Andric 1048349cc55cSDimitry Andric // Add all the requested passes for instrumentation PGO, if requested. 1049349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1050349cc55cSDimitry Andric (PGOOpt->Action == PGOOptions::IRInstr || 1051349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) { 1052349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, 1053349cc55cSDimitry Andric /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr, 1054349cc55cSDimitry Andric /* IsCS */ false, PGOOpt->ProfileFile, 105581ad6265SDimitry Andric PGOOpt->ProfileRemappingFile, Phase); 1056349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(false, false)); 1057349cc55cSDimitry Andric } 1058349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1059349cc55cSDimitry Andric PGOOpt->CSAction == PGOOptions::CSIRInstr) 1060349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); 1061349cc55cSDimitry Andric 1062349cc55cSDimitry Andric // Synthesize function entry counts for non-PGO compilation. 1063349cc55cSDimitry Andric if (EnableSyntheticCounts && !PGOOpt) 1064349cc55cSDimitry Andric MPM.addPass(SyntheticCountsPropagation()); 1065349cc55cSDimitry Andric 1066349cc55cSDimitry Andric if (EnableModuleInliner) 1067349cc55cSDimitry Andric MPM.addPass(buildModuleInlinerPipeline(Level, Phase)); 1068349cc55cSDimitry Andric else 1069349cc55cSDimitry Andric MPM.addPass(buildInlinerPipeline(Level, Phase)); 1070349cc55cSDimitry Andric 1071*bdd1243dSDimitry Andric // Remove any dead arguments exposed by cleanups, constant folding globals, 1072*bdd1243dSDimitry Andric // and argument promotion. 1073*bdd1243dSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 1074*bdd1243dSDimitry Andric 107581ad6265SDimitry Andric MPM.addPass(CoroCleanupPass()); 107681ad6265SDimitry Andric 1077349cc55cSDimitry Andric if (EnableMemProfiler && Phase != ThinOrFullLTOPhase::ThinLTOPreLink) { 1078349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 1079349cc55cSDimitry Andric MPM.addPass(ModuleMemProfilerPass()); 1080349cc55cSDimitry Andric } 1081349cc55cSDimitry Andric 1082349cc55cSDimitry Andric return MPM; 1083349cc55cSDimitry Andric } 1084349cc55cSDimitry Andric 1085349cc55cSDimitry Andric /// TODO: Should LTO cause any differences to this set of passes? 1086349cc55cSDimitry Andric void PassBuilder::addVectorPasses(OptimizationLevel Level, 1087349cc55cSDimitry Andric FunctionPassManager &FPM, bool IsFullLTO) { 1088349cc55cSDimitry Andric FPM.addPass(LoopVectorizePass( 1089349cc55cSDimitry Andric LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization))); 1090349cc55cSDimitry Andric 1091349cc55cSDimitry Andric if (IsFullLTO) { 1092349cc55cSDimitry Andric // The vectorizer may have significantly shortened a loop body; unroll 1093349cc55cSDimitry Andric // again. Unroll small loops to hide loop backedge latency and saturate any 1094349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 1095349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 1096349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 1097349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 1098349cc55cSDimitry Andric // across the loop nests. 1099349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1100349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) 1101349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1102349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1103349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1104349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1105349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 1106349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 1107*bdd1243dSDimitry Andric // Now that we are done with loop unrolling, be it either by LoopVectorizer, 1108*bdd1243dSDimitry Andric // or LoopUnroll passes, some variable-offset GEP's into alloca's could have 1109*bdd1243dSDimitry Andric // become constant-offset, thus enabling SROA and alloca promotion. Do so. 1110*bdd1243dSDimitry Andric // NOTE: we are very late in the pipeline, and we don't have any LICM 1111*bdd1243dSDimitry Andric // or SimplifyCFG passes scheduled after us, that would cleanup 1112*bdd1243dSDimitry Andric // the CFG mess this may created if allowed to modify CFG, so forbid that. 1113*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::PreserveCFG)); 1114349cc55cSDimitry Andric } 1115349cc55cSDimitry Andric 1116349cc55cSDimitry Andric if (!IsFullLTO) { 1117349cc55cSDimitry Andric // Eliminate loads by forwarding stores from the previous iteration to loads 1118349cc55cSDimitry Andric // of the current iteration. 1119349cc55cSDimitry Andric FPM.addPass(LoopLoadEliminationPass()); 1120349cc55cSDimitry Andric } 1121349cc55cSDimitry Andric // Cleanup after the loop optimization passes. 1122349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1123349cc55cSDimitry Andric 1124349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 11250eae32dcSDimitry Andric ExtraVectorPassManager ExtraPasses; 1126349cc55cSDimitry Andric // At higher optimization levels, try to clean up any runtime overlap and 1127349cc55cSDimitry Andric // alignment checks inserted by the vectorizer. We want to track correlated 1128349cc55cSDimitry Andric // runtime checks for two inner loops in the same outer loop, fold any 1129349cc55cSDimitry Andric // common computations, hoist loop-invariant aspects out of any outer loop, 1130349cc55cSDimitry Andric // and unswitch the runtime checks if possible. Once hoisted, we may have 1131349cc55cSDimitry Andric // dead (or speculatable) control flows or more combining opportunities. 11320eae32dcSDimitry Andric ExtraPasses.addPass(EarlyCSEPass()); 11330eae32dcSDimitry Andric ExtraPasses.addPass(CorrelatedValuePropagationPass()); 11340eae32dcSDimitry Andric ExtraPasses.addPass(InstCombinePass()); 1135349cc55cSDimitry Andric LoopPassManager LPM; 1136fb03ea46SDimitry Andric LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1137fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 1138349cc55cSDimitry Andric LPM.addPass(SimpleLoopUnswitchPass(/* NonTrivial */ Level == 1139349cc55cSDimitry Andric OptimizationLevel::O3)); 11400eae32dcSDimitry Andric ExtraPasses.addPass( 1141349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 11420eae32dcSDimitry Andric ExtraPasses.addPass( 1143349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM), /*UseMemorySSA=*/true, 1144349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 1145fb03ea46SDimitry Andric ExtraPasses.addPass( 1146fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 11470eae32dcSDimitry Andric ExtraPasses.addPass(InstCombinePass()); 11480eae32dcSDimitry Andric FPM.addPass(std::move(ExtraPasses)); 1149349cc55cSDimitry Andric } 1150349cc55cSDimitry Andric 1151349cc55cSDimitry Andric // Now that we've formed fast to execute loop structures, we do further 1152349cc55cSDimitry Andric // optimizations. These are run afterward as they might block doing complex 1153349cc55cSDimitry Andric // analyses and transforms such as what are needed for loop vectorization. 1154349cc55cSDimitry Andric 1155349cc55cSDimitry Andric // Cleanup after loop vectorization, etc. Simplification passes like CVP and 1156349cc55cSDimitry Andric // GVN, loop transforms, and others have already run, so it's now better to 1157349cc55cSDimitry Andric // convert to more optimized IR using more aggressive simplify CFG options. 1158349cc55cSDimitry Andric // The extra sinking transform can create larger basic blocks, so do this 1159349cc55cSDimitry Andric // before SLP vectorization. 1160349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 1161349cc55cSDimitry Andric .forwardSwitchCondToPhi(true) 1162fb03ea46SDimitry Andric .convertSwitchRangeToICmp(true) 1163349cc55cSDimitry Andric .convertSwitchToLookupTable(true) 1164349cc55cSDimitry Andric .needCanonicalLoops(false) 1165349cc55cSDimitry Andric .hoistCommonInsts(true) 1166349cc55cSDimitry Andric .sinkCommonInsts(true))); 1167349cc55cSDimitry Andric 1168349cc55cSDimitry Andric if (IsFullLTO) { 1169349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 1170349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1171349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 1172349cc55cSDimitry Andric } 1173349cc55cSDimitry Andric 1174349cc55cSDimitry Andric // Optimize parallel scalar instruction chains into SIMD instructions. 1175349cc55cSDimitry Andric if (PTO.SLPVectorization) { 1176349cc55cSDimitry Andric FPM.addPass(SLPVectorizerPass()); 1177349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 1178349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); 1179349cc55cSDimitry Andric } 1180349cc55cSDimitry Andric } 1181349cc55cSDimitry Andric // Enhance/cleanup vector code. 1182349cc55cSDimitry Andric FPM.addPass(VectorCombinePass()); 1183349cc55cSDimitry Andric 1184349cc55cSDimitry Andric if (!IsFullLTO) { 1185349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1186349cc55cSDimitry Andric // Unroll small loops to hide loop backedge latency and saturate any 1187349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 1188349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 1189349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 1190349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 1191349cc55cSDimitry Andric // across the loop nests. 1192349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1193349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) { 1194349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1195349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1196349cc55cSDimitry Andric } 1197349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1198349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1199349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 1200349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 1201*bdd1243dSDimitry Andric // Now that we are done with loop unrolling, be it either by LoopVectorizer, 1202*bdd1243dSDimitry Andric // or LoopUnroll passes, some variable-offset GEP's into alloca's could have 1203*bdd1243dSDimitry Andric // become constant-offset, thus enabling SROA and alloca promotion. Do so. 1204*bdd1243dSDimitry Andric // NOTE: we are very late in the pipeline, and we don't have any LICM 1205*bdd1243dSDimitry Andric // or SimplifyCFG passes scheduled after us, that would cleanup 1206*bdd1243dSDimitry Andric // the CFG mess this may created if allowed to modify CFG, so forbid that. 1207*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::PreserveCFG)); 1208349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1209349cc55cSDimitry Andric FPM.addPass( 1210349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 1211349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1212fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1213fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 1214349cc55cSDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 1215349cc55cSDimitry Andric } 1216349cc55cSDimitry Andric 1217349cc55cSDimitry Andric // Now that we've vectorized and unrolled loops, we may have more refined 1218349cc55cSDimitry Andric // alignment information, try to re-derive it here. 1219349cc55cSDimitry Andric FPM.addPass(AlignmentFromAssumptionsPass()); 1220349cc55cSDimitry Andric 1221349cc55cSDimitry Andric if (IsFullLTO) 1222349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1223349cc55cSDimitry Andric } 1224349cc55cSDimitry Andric 1225349cc55cSDimitry Andric ModulePassManager 1226349cc55cSDimitry Andric PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level, 122781ad6265SDimitry Andric ThinOrFullLTOPhase LTOPhase) { 122881ad6265SDimitry Andric const bool LTOPreLink = (LTOPhase == ThinOrFullLTOPhase::ThinLTOPreLink || 122981ad6265SDimitry Andric LTOPhase == ThinOrFullLTOPhase::FullLTOPreLink); 1230349cc55cSDimitry Andric ModulePassManager MPM; 1231349cc55cSDimitry Andric 1232349cc55cSDimitry Andric // Optimize globals now that the module is fully simplified. 1233349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1234349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1235349cc55cSDimitry Andric 1236349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1237349cc55cSDimitry Andric // large bodies. 1238349cc55cSDimitry Andric if (RunPartialInlining) 1239349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1240349cc55cSDimitry Andric 1241349cc55cSDimitry Andric // Remove avail extern fns and globals definitions since we aren't compiling 1242349cc55cSDimitry Andric // an object file for later LTO. For LTO we want to preserve these so they 1243349cc55cSDimitry Andric // are eligible for inlining at link-time. Note if they are unreferenced they 1244349cc55cSDimitry Andric // will be removed by GlobalDCE later, so this only impacts referenced 1245349cc55cSDimitry Andric // available externally globals. Eventually they will be suppressed during 1246349cc55cSDimitry Andric // codegen, but eliminating here enables more opportunity for GlobalDCE as it 1247349cc55cSDimitry Andric // may make globals referenced by available external functions dead and saves 1248349cc55cSDimitry Andric // running remaining passes on the eliminated functions. These should be 1249349cc55cSDimitry Andric // preserved during prelinking for link-time inlining decisions. 1250349cc55cSDimitry Andric if (!LTOPreLink) 1251349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1252349cc55cSDimitry Andric 1253349cc55cSDimitry Andric if (EnableOrderFileInstrumentation) 1254349cc55cSDimitry Andric MPM.addPass(InstrOrderFilePass()); 1255349cc55cSDimitry Andric 1256349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1257349cc55cSDimitry Andric // attributes where applicable. 1258349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1259349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1260349cc55cSDimitry Andric 1261349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1262349cc55cSDimitry Andric // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as 1263349cc55cSDimitry Andric // cross-module inline has not been done yet. The context sensitive 1264349cc55cSDimitry Andric // instrumentation is after all the inlines are done. 1265349cc55cSDimitry Andric if (!LTOPreLink && PGOOpt) { 1266349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1267349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1268349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->CSProfileGenFile, 126981ad6265SDimitry Andric PGOOpt->ProfileRemappingFile, LTOPhase); 1270349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1271349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1272349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->ProfileFile, 127381ad6265SDimitry Andric PGOOpt->ProfileRemappingFile, LTOPhase); 1274349cc55cSDimitry Andric } 1275349cc55cSDimitry Andric 127681ad6265SDimitry Andric // Re-compute GlobalsAA here prior to function passes. This is particularly 1277349cc55cSDimitry Andric // useful as the above will have inlined, DCE'ed, and function-attr 1278349cc55cSDimitry Andric // propagated everything. We should at this point have a reasonably minimal 1279349cc55cSDimitry Andric // and richly annotated call graph. By computing aliasing and mod/ref 1280349cc55cSDimitry Andric // information for all local globals here, the late loop passes and notably 1281349cc55cSDimitry Andric // the vectorizer will be able to use them to help recognize vectorizable 1282349cc55cSDimitry Andric // memory operations. 128381ad6265SDimitry Andric MPM.addPass(RecomputeGlobalsAAPass()); 128481ad6265SDimitry Andric 128581ad6265SDimitry Andric for (auto &C : OptimizerEarlyEPCallbacks) 128681ad6265SDimitry Andric C(MPM, Level); 1287349cc55cSDimitry Andric 1288349cc55cSDimitry Andric FunctionPassManager OptimizePM; 1289349cc55cSDimitry Andric OptimizePM.addPass(Float2IntPass()); 1290349cc55cSDimitry Andric OptimizePM.addPass(LowerConstantIntrinsicsPass()); 1291349cc55cSDimitry Andric 1292349cc55cSDimitry Andric if (EnableMatrix) { 1293349cc55cSDimitry Andric OptimizePM.addPass(LowerMatrixIntrinsicsPass()); 1294349cc55cSDimitry Andric OptimizePM.addPass(EarlyCSEPass()); 1295349cc55cSDimitry Andric } 1296349cc55cSDimitry Andric 1297349cc55cSDimitry Andric // FIXME: We need to run some loop optimizations to re-rotate loops after 1298349cc55cSDimitry Andric // simplifycfg and others undo their rotation. 1299349cc55cSDimitry Andric 1300349cc55cSDimitry Andric // Optimize the loop execution. These passes operate on entire loop nests 1301349cc55cSDimitry Andric // rather than on each loop in an inside-out manner, and so they are actually 1302349cc55cSDimitry Andric // function passes. 1303349cc55cSDimitry Andric 1304349cc55cSDimitry Andric for (auto &C : VectorizerStartEPCallbacks) 1305349cc55cSDimitry Andric C(OptimizePM, Level); 1306349cc55cSDimitry Andric 1307349cc55cSDimitry Andric LoopPassManager LPM; 1308349cc55cSDimitry Andric // First rotate loops that may have been un-rotated by prior passes. 1309349cc55cSDimitry Andric // Disable header duplication at -Oz. 1310349cc55cSDimitry Andric LPM.addPass(LoopRotatePass(Level != OptimizationLevel::Oz, LTOPreLink)); 1311349cc55cSDimitry Andric // Some loops may have become dead by now. Try to delete them. 13120eae32dcSDimitry Andric // FIXME: see discussion in https://reviews.llvm.org/D112851, 13130eae32dcSDimitry Andric // this may need to be revisited once we run GVN before loop deletion 13140eae32dcSDimitry Andric // in the simplification pipeline. 1315349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1316349cc55cSDimitry Andric OptimizePM.addPass(createFunctionToLoopPassAdaptor( 1317349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/false)); 1318349cc55cSDimitry Andric 1319349cc55cSDimitry Andric // Distribute loops to allow partial vectorization. I.e. isolate dependences 1320349cc55cSDimitry Andric // into separate loop that would otherwise inhibit vectorization. This is 1321349cc55cSDimitry Andric // currently only performed for loops marked with the metadata 1322349cc55cSDimitry Andric // llvm.loop.distribute=true or when -enable-loop-distribute is specified. 1323349cc55cSDimitry Andric OptimizePM.addPass(LoopDistributePass()); 1324349cc55cSDimitry Andric 1325349cc55cSDimitry Andric // Populates the VFABI attribute with the scalar-to-vector mappings 1326349cc55cSDimitry Andric // from the TargetLibraryInfo. 1327349cc55cSDimitry Andric OptimizePM.addPass(InjectTLIMappings()); 1328349cc55cSDimitry Andric 1329349cc55cSDimitry Andric addVectorPasses(Level, OptimizePM, /* IsFullLTO */ false); 1330349cc55cSDimitry Andric 1331349cc55cSDimitry Andric // LoopSink pass sinks instructions hoisted by LICM, which serves as a 1332349cc55cSDimitry Andric // canonicalization pass that enables other optimizations. As a result, 1333349cc55cSDimitry Andric // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 1334349cc55cSDimitry Andric // result too early. 1335349cc55cSDimitry Andric OptimizePM.addPass(LoopSinkPass()); 1336349cc55cSDimitry Andric 1337349cc55cSDimitry Andric // And finally clean up LCSSA form before generating code. 1338349cc55cSDimitry Andric OptimizePM.addPass(InstSimplifyPass()); 1339349cc55cSDimitry Andric 1340349cc55cSDimitry Andric // This hoists/decomposes div/rem ops. It should run after other sink/hoist 1341349cc55cSDimitry Andric // passes to avoid re-sinking, but before SimplifyCFG because it can allow 1342349cc55cSDimitry Andric // flattening of blocks. 1343349cc55cSDimitry Andric OptimizePM.addPass(DivRemPairsPass()); 1344349cc55cSDimitry Andric 1345972a253aSDimitry Andric // Try to annotate calls that were created during optimization. 1346972a253aSDimitry Andric OptimizePM.addPass(TailCallElimPass()); 1347972a253aSDimitry Andric 1348349cc55cSDimitry Andric // LoopSink (and other loop passes since the last simplifyCFG) might have 1349349cc55cSDimitry Andric // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. 1350fb03ea46SDimitry Andric OptimizePM.addPass( 1351fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 1352349cc55cSDimitry Andric 1353349cc55cSDimitry Andric // Add the core optimizing pipeline. 1354349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM), 1355349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1356349cc55cSDimitry Andric 1357349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1358349cc55cSDimitry Andric C(MPM, Level); 1359349cc55cSDimitry Andric 13600eae32dcSDimitry Andric // Split out cold code. Splitting is done late to avoid hiding context from 13610eae32dcSDimitry Andric // other optimizations and inadvertently regressing performance. The tradeoff 13620eae32dcSDimitry Andric // is that this has a higher code size cost than splitting early. 13630eae32dcSDimitry Andric if (EnableHotColdSplit && !LTOPreLink) 13640eae32dcSDimitry Andric MPM.addPass(HotColdSplittingPass()); 13650eae32dcSDimitry Andric 13660eae32dcSDimitry Andric // Search the code for similar regions of code. If enough similar regions can 13670eae32dcSDimitry Andric // be found where extracting the regions into their own function will decrease 13680eae32dcSDimitry Andric // the size of the program, we extract the regions, a deduplicate the 13690eae32dcSDimitry Andric // structurally similar regions. 13700eae32dcSDimitry Andric if (EnableIROutliner) 13710eae32dcSDimitry Andric MPM.addPass(IROutlinerPass()); 13720eae32dcSDimitry Andric 13730eae32dcSDimitry Andric // Merge functions if requested. 13740eae32dcSDimitry Andric if (PTO.MergeFunctions) 13750eae32dcSDimitry Andric MPM.addPass(MergeFunctionsPass()); 13760eae32dcSDimitry Andric 1377349cc55cSDimitry Andric // Now we need to do some global optimization transforms. 1378349cc55cSDimitry Andric // FIXME: It would seem like these should come first in the optimization 1379349cc55cSDimitry Andric // pipeline and maybe be the bottom of the canonicalization pipeline? Weird 1380349cc55cSDimitry Andric // ordering here. 1381349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1382349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1383349cc55cSDimitry Andric 138481ad6265SDimitry Andric if (PTO.CallGraphProfile && !LTOPreLink) 138581ad6265SDimitry Andric MPM.addPass(CGProfilePass()); 138681ad6265SDimitry Andric 1387349cc55cSDimitry Andric // TODO: Relative look table converter pass caused an issue when full lto is 1388349cc55cSDimitry Andric // enabled. See https://reviews.llvm.org/D94355 for more details. 1389349cc55cSDimitry Andric // Until the issue fixed, disable this pass during pre-linking phase. 1390349cc55cSDimitry Andric if (!LTOPreLink) 1391349cc55cSDimitry Andric MPM.addPass(RelLookupTableConverterPass()); 1392349cc55cSDimitry Andric 1393349cc55cSDimitry Andric return MPM; 1394349cc55cSDimitry Andric } 1395349cc55cSDimitry Andric 1396349cc55cSDimitry Andric ModulePassManager 1397349cc55cSDimitry Andric PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, 1398349cc55cSDimitry Andric bool LTOPreLink) { 1399349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1400349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1401349cc55cSDimitry Andric 1402349cc55cSDimitry Andric ModulePassManager MPM; 1403349cc55cSDimitry Andric 1404349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1405349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1406349cc55cSDimitry Andric 1407349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1408349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1409349cc55cSDimitry Andric 1410349cc55cSDimitry Andric // Apply module pipeline start EP callback. 1411349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1412349cc55cSDimitry Andric C(MPM, Level); 1413349cc55cSDimitry Andric 1414349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1415349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1416349cc55cSDimitry Andric 141781ad6265SDimitry Andric const ThinOrFullLTOPhase LTOPhase = LTOPreLink 141881ad6265SDimitry Andric ? ThinOrFullLTOPhase::FullLTOPreLink 141981ad6265SDimitry Andric : ThinOrFullLTOPhase::None; 1420349cc55cSDimitry Andric // Add the core simplification pipeline. 142181ad6265SDimitry Andric MPM.addPass(buildModuleSimplificationPipeline(Level, LTOPhase)); 1422349cc55cSDimitry Andric 1423349cc55cSDimitry Andric // Now add the optimization pipeline. 142481ad6265SDimitry Andric MPM.addPass(buildModuleOptimizationPipeline(Level, LTOPhase)); 1425349cc55cSDimitry Andric 1426349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1427349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1428349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1429349cc55cSDimitry Andric 1430349cc55cSDimitry Andric // Emit annotation remarks. 1431349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1432349cc55cSDimitry Andric 1433349cc55cSDimitry Andric if (LTOPreLink) 1434349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1435349cc55cSDimitry Andric 1436349cc55cSDimitry Andric return MPM; 1437349cc55cSDimitry Andric } 1438349cc55cSDimitry Andric 1439349cc55cSDimitry Andric ModulePassManager 1440349cc55cSDimitry Andric PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1441349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1442349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1443349cc55cSDimitry Andric 1444349cc55cSDimitry Andric ModulePassManager MPM; 1445349cc55cSDimitry Andric 1446349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1447349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1448349cc55cSDimitry Andric 1449349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1450349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1451349cc55cSDimitry Andric 1452349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1453349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1454349cc55cSDimitry Andric 1455349cc55cSDimitry Andric // Apply module pipeline start EP callback. 1456349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1457349cc55cSDimitry Andric C(MPM, Level); 1458349cc55cSDimitry Andric 1459349cc55cSDimitry Andric // If we are planning to perform ThinLTO later, we don't bloat the code with 1460349cc55cSDimitry Andric // unrolling/vectorization/... now. Just simplify the module as much as we 1461349cc55cSDimitry Andric // can. 1462349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1463349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPreLink)); 1464349cc55cSDimitry Andric 1465349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1466349cc55cSDimitry Andric // large bodies. 1467349cc55cSDimitry Andric // FIXME: It isn't clear whether this is really the right place to run this 1468349cc55cSDimitry Andric // in ThinLTO. Because there is another canonicalization and simplification 1469349cc55cSDimitry Andric // phase that will run after the thin link, running this here ends up with 1470349cc55cSDimitry Andric // less information than will be available later and it may grow functions in 1471349cc55cSDimitry Andric // ways that aren't beneficial. 1472349cc55cSDimitry Andric if (RunPartialInlining) 1473349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1474349cc55cSDimitry Andric 1475349cc55cSDimitry Andric // Reduce the size of the IR as much as possible. 1476349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1477349cc55cSDimitry Andric 1478349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1479349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1480349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1481349cc55cSDimitry Andric 1482*bdd1243dSDimitry Andric // Handle Optimizer{Early,Last}EPCallbacks added by clang on PreLink. Actual 1483*bdd1243dSDimitry Andric // optimization is going to be done in PostLink stage, but clang can't add 1484*bdd1243dSDimitry Andric // callbacks there in case of in-process ThinLTO called by linker. 1485*bdd1243dSDimitry Andric for (auto &C : OptimizerEarlyEPCallbacks) 1486*bdd1243dSDimitry Andric C(MPM, Level); 1487349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1488349cc55cSDimitry Andric C(MPM, Level); 1489349cc55cSDimitry Andric 1490349cc55cSDimitry Andric // Emit annotation remarks. 1491349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1492349cc55cSDimitry Andric 1493349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1494349cc55cSDimitry Andric 1495349cc55cSDimitry Andric return MPM; 1496349cc55cSDimitry Andric } 1497349cc55cSDimitry Andric 1498349cc55cSDimitry Andric ModulePassManager PassBuilder::buildThinLTODefaultPipeline( 1499349cc55cSDimitry Andric OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary) { 1500349cc55cSDimitry Andric ModulePassManager MPM; 1501349cc55cSDimitry Andric 1502349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1503349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1504349cc55cSDimitry Andric 1505349cc55cSDimitry Andric if (ImportSummary) { 1506349cc55cSDimitry Andric // These passes import type identifier resolutions for whole-program 1507349cc55cSDimitry Andric // devirtualization and CFI. They must run early because other passes may 1508349cc55cSDimitry Andric // disturb the specific instruction patterns that these passes look for, 1509349cc55cSDimitry Andric // creating dependencies on resolutions that may not appear in the summary. 1510349cc55cSDimitry Andric // 1511349cc55cSDimitry Andric // For example, GVN may transform the pattern assume(type.test) appearing in 1512349cc55cSDimitry Andric // two basic blocks into assume(phi(type.test, type.test)), which would 1513349cc55cSDimitry Andric // transform a dependency on a WPD resolution into a dependency on a type 1514349cc55cSDimitry Andric // identifier resolution for CFI. 1515349cc55cSDimitry Andric // 1516349cc55cSDimitry Andric // Also, WPD has access to more precise information than ICP and can 1517349cc55cSDimitry Andric // devirtualize more effectively, so it should operate on the IR first. 1518349cc55cSDimitry Andric // 1519349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1520349cc55cSDimitry Andric // metadata and intrinsics. 1521349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary)); 1522349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary)); 1523349cc55cSDimitry Andric } 1524349cc55cSDimitry Andric 1525349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1526349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1527349cc55cSDimitry Andric // in ICP. 1528349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1529349cc55cSDimitry Andric // Drop available_externally and unreferenced globals. This is necessary 1530349cc55cSDimitry Andric // with ThinLTO in order to avoid leaving undefined references to dead 1531349cc55cSDimitry Andric // globals in the object file. 1532349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1533349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1534349cc55cSDimitry Andric return MPM; 1535349cc55cSDimitry Andric } 1536349cc55cSDimitry Andric 1537349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1538349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1539349cc55cSDimitry Andric 1540349cc55cSDimitry Andric // Add the core simplification pipeline. 1541349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1542349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1543349cc55cSDimitry Andric 1544349cc55cSDimitry Andric // Now add the optimization pipeline. 154581ad6265SDimitry Andric MPM.addPass(buildModuleOptimizationPipeline( 154681ad6265SDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1547349cc55cSDimitry Andric 1548349cc55cSDimitry Andric // Emit annotation remarks. 1549349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1550349cc55cSDimitry Andric 1551349cc55cSDimitry Andric return MPM; 1552349cc55cSDimitry Andric } 1553349cc55cSDimitry Andric 1554349cc55cSDimitry Andric ModulePassManager 1555349cc55cSDimitry Andric PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1556349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1557349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1558349cc55cSDimitry Andric // FIXME: We should use a customized pre-link pipeline! 1559349cc55cSDimitry Andric return buildPerModuleDefaultPipeline(Level, 1560349cc55cSDimitry Andric /* LTOPreLink */ true); 1561349cc55cSDimitry Andric } 1562349cc55cSDimitry Andric 1563349cc55cSDimitry Andric ModulePassManager 1564349cc55cSDimitry Andric PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, 1565349cc55cSDimitry Andric ModuleSummaryIndex *ExportSummary) { 1566349cc55cSDimitry Andric ModulePassManager MPM; 1567349cc55cSDimitry Andric 1568349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1569349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1570349cc55cSDimitry Andric 157181ad6265SDimitry Andric for (auto &C : FullLinkTimeOptimizationEarlyEPCallbacks) 157281ad6265SDimitry Andric C(MPM, Level); 157381ad6265SDimitry Andric 1574349cc55cSDimitry Andric // Create a function that performs CFI checks for cross-DSO calls with targets 1575349cc55cSDimitry Andric // in the current module. 1576349cc55cSDimitry Andric MPM.addPass(CrossDSOCFIPass()); 1577349cc55cSDimitry Andric 1578349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1579349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1580349cc55cSDimitry Andric // metadata and intrinsics. 1581349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1582349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1583349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1584349cc55cSDimitry Andric // in ICP. 1585349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1586349cc55cSDimitry Andric 158781ad6265SDimitry Andric for (auto &C : FullLinkTimeOptimizationLastEPCallbacks) 158881ad6265SDimitry Andric C(MPM, Level); 158981ad6265SDimitry Andric 1590349cc55cSDimitry Andric // Emit annotation remarks. 1591349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1592349cc55cSDimitry Andric 1593349cc55cSDimitry Andric return MPM; 1594349cc55cSDimitry Andric } 1595349cc55cSDimitry Andric 1596349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) { 1597349cc55cSDimitry Andric // Load sample profile before running the LTO optimization pipeline. 1598349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1599349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, 1600349cc55cSDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink)); 1601349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1602349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 1603349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1604349cc55cSDimitry Andric } 1605349cc55cSDimitry Andric 16061fd87a68SDimitry Andric // Try to run OpenMP optimizations, quick no-op if no OpenMP metadata present. 16071fd87a68SDimitry Andric MPM.addPass(OpenMPOptPass()); 16081fd87a68SDimitry Andric 1609349cc55cSDimitry Andric // Remove unused virtual tables to improve the quality of code generated by 1610349cc55cSDimitry Andric // whole-program devirtualization and bitset lowering. 1611349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1612349cc55cSDimitry Andric 1613349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1614349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1615349cc55cSDimitry Andric 1616349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 1617349cc55cSDimitry Andric // libraries and other oracles. 1618349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 1619349cc55cSDimitry Andric 1620349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1) { 1621349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 162281ad6265SDimitry Andric CallSiteSplittingPass(), PTO.EagerlyInvalidateAnalyses)); 1623349cc55cSDimitry Andric 1624349cc55cSDimitry Andric // Indirect call promotion. This should promote all the targets that are 1625349cc55cSDimitry Andric // left by the earlier promotion pass that promotes intra-module targets. 1626349cc55cSDimitry Andric // This two-step promotion is to save the compile time. For LTO, it should 1627349cc55cSDimitry Andric // produce the same result as if we only do promotion here. 1628349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion( 1629349cc55cSDimitry Andric true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse)); 1630349cc55cSDimitry Andric 1631349cc55cSDimitry Andric // Propagate constants at call sites into the functions they call. This 1632349cc55cSDimitry Andric // opens opportunities for globalopt (and inlining) by substituting function 1633349cc55cSDimitry Andric // pointers passed as arguments to direct uses of functions. 1634*bdd1243dSDimitry Andric MPM.addPass(IPSCCPPass(IPSCCPOptions(/*AllowFuncSpec=*/ 1635*bdd1243dSDimitry Andric Level != OptimizationLevel::Os && 1636*bdd1243dSDimitry Andric Level != OptimizationLevel::Oz))); 1637349cc55cSDimitry Andric 1638349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 1639349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 1640349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 1641349cc55cSDimitry Andric } 1642349cc55cSDimitry Andric 1643349cc55cSDimitry Andric // Now deduce any function attributes based in the current code. 1644349cc55cSDimitry Andric MPM.addPass( 1645349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1646349cc55cSDimitry Andric 1647349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1648349cc55cSDimitry Andric // attributes where applicable. 1649349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1650349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1651349cc55cSDimitry Andric 1652349cc55cSDimitry Andric // Use in-range annotations on GEP indices to split globals where beneficial. 1653349cc55cSDimitry Andric MPM.addPass(GlobalSplitPass()); 1654349cc55cSDimitry Andric 1655349cc55cSDimitry Andric // Run whole program optimization of virtual call when the list of callees 1656349cc55cSDimitry Andric // is fixed. 1657349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1658349cc55cSDimitry Andric 1659349cc55cSDimitry Andric // Stop here at -O1. 1660349cc55cSDimitry Andric if (Level == OptimizationLevel::O1) { 1661349cc55cSDimitry Andric // The LowerTypeTestsPass needs to run to lower type metadata and the 1662349cc55cSDimitry Andric // type.test intrinsics. The pass does nothing if CFI is disabled. 1663349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1664349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1665349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO 1666349cc55cSDimitry Andric // pipeline). 1667349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1668349cc55cSDimitry Andric 166981ad6265SDimitry Andric for (auto &C : FullLinkTimeOptimizationLastEPCallbacks) 167081ad6265SDimitry Andric C(MPM, Level); 167181ad6265SDimitry Andric 1672349cc55cSDimitry Andric // Emit annotation remarks. 1673349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1674349cc55cSDimitry Andric 1675349cc55cSDimitry Andric return MPM; 1676349cc55cSDimitry Andric } 1677349cc55cSDimitry Andric 1678349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 1679349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1680349cc55cSDimitry Andric 1681349cc55cSDimitry Andric // Promote any localized globals to SSA registers. 1682349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1683349cc55cSDimitry Andric 1684349cc55cSDimitry Andric // Linking modules together can lead to duplicate global constant, only 1685349cc55cSDimitry Andric // keep one copy of each constant. 1686349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1687349cc55cSDimitry Andric 1688349cc55cSDimitry Andric // Reduce the code after globalopt and ipsccp. Both can open up significant 1689349cc55cSDimitry Andric // simplification opportunities, and both can propagate functions through 1690349cc55cSDimitry Andric // function pointers. When this happens, we often have to resolve varargs 1691349cc55cSDimitry Andric // calls, etc, so let instcombine do this. 1692349cc55cSDimitry Andric FunctionPassManager PeepholeFPM; 16930eae32dcSDimitry Andric PeepholeFPM.addPass(InstCombinePass()); 1694349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 1695349cc55cSDimitry Andric PeepholeFPM.addPass(AggressiveInstCombinePass()); 1696349cc55cSDimitry Andric invokePeepholeEPCallbacks(PeepholeFPM, Level); 1697349cc55cSDimitry Andric 1698349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM), 1699349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1700349cc55cSDimitry Andric 1701349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 1702349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 1703349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 1704349cc55cSDimitry Andric // invoke or a call. 1705349cc55cSDimitry Andric // Run the inliner now. 170681ad6265SDimitry Andric MPM.addPass(ModuleInlinerWrapperPass( 170781ad6265SDimitry Andric getInlineParamsFromOptLevel(Level), 170881ad6265SDimitry Andric /* MandatoryFirst */ true, 170981ad6265SDimitry Andric InlineContext{ThinOrFullLTOPhase::FullLTOPostLink, 171081ad6265SDimitry Andric InlinePass::CGSCCInliner})); 1711349cc55cSDimitry Andric 1712349cc55cSDimitry Andric // Optimize globals again after we ran the inliner. 1713349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1714349cc55cSDimitry Andric 1715349cc55cSDimitry Andric // Garbage collect dead functions. 1716349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1717349cc55cSDimitry Andric 1718349cc55cSDimitry Andric // If we didn't decide to inline a function, check to see if we can 1719349cc55cSDimitry Andric // transform it to pass arguments by value instead of by reference. 1720349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(ArgumentPromotionPass())); 1721349cc55cSDimitry Andric 1722*bdd1243dSDimitry Andric // Remove unused arguments from functions. 1723*bdd1243dSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 1724*bdd1243dSDimitry Andric 1725349cc55cSDimitry Andric FunctionPassManager FPM; 1726349cc55cSDimitry Andric // The IPO Passes may leave cruft around. Clean up after them. 1727349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1728349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 1729349cc55cSDimitry Andric 1730*bdd1243dSDimitry Andric if (EnableConstraintElimination) 1731*bdd1243dSDimitry Andric FPM.addPass(ConstraintEliminationPass()); 1732*bdd1243dSDimitry Andric 173381ad6265SDimitry Andric FPM.addPass(JumpThreadingPass()); 1734349cc55cSDimitry Andric 1735349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1736349cc55cSDimitry Andric // sensitive PGO pass. 1737349cc55cSDimitry Andric if (PGOOpt) { 1738349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1739349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1740349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->CSProfileGenFile, 174181ad6265SDimitry Andric PGOOpt->ProfileRemappingFile, 174281ad6265SDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink); 1743349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1744349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1745349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->ProfileFile, 174681ad6265SDimitry Andric PGOOpt->ProfileRemappingFile, 174781ad6265SDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink); 1748349cc55cSDimitry Andric } 1749349cc55cSDimitry Andric 1750349cc55cSDimitry Andric // Break up allocas 1751*bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 1752349cc55cSDimitry Andric 1753349cc55cSDimitry Andric // LTO provides additional opportunities for tailcall elimination due to 1754349cc55cSDimitry Andric // link-time inlining, and visibility of nocapture attribute. 1755349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 1756349cc55cSDimitry Andric 1757349cc55cSDimitry Andric // Run a few AA driver optimizations here and now to cleanup the code. 1758349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM), 1759349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1760349cc55cSDimitry Andric 1761349cc55cSDimitry Andric MPM.addPass( 1762349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1763349cc55cSDimitry Andric 1764349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 1765349cc55cSDimitry Andric // MainFPM. 1766349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1767349cc55cSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly available 1768349cc55cSDimitry Andric // GlobalsAA. 1769349cc55cSDimitry Andric MPM.addPass( 1770349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 1771349cc55cSDimitry Andric 1772349cc55cSDimitry Andric FunctionPassManager MainFPM; 1773349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1774fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1775fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 1776349cc55cSDimitry Andric /*USeMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 1777349cc55cSDimitry Andric 1778349cc55cSDimitry Andric if (RunNewGVN) 1779349cc55cSDimitry Andric MainFPM.addPass(NewGVNPass()); 1780349cc55cSDimitry Andric else 1781349cc55cSDimitry Andric MainFPM.addPass(GVNPass()); 1782349cc55cSDimitry Andric 1783349cc55cSDimitry Andric // Remove dead memcpy()'s. 1784349cc55cSDimitry Andric MainFPM.addPass(MemCpyOptPass()); 1785349cc55cSDimitry Andric 1786349cc55cSDimitry Andric // Nuke dead stores. 1787349cc55cSDimitry Andric MainFPM.addPass(DSEPass()); 1788349cc55cSDimitry Andric MainFPM.addPass(MergedLoadStoreMotionPass()); 1789349cc55cSDimitry Andric 1790349cc55cSDimitry Andric LoopPassManager LPM; 179104eeddc0SDimitry Andric if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) 179204eeddc0SDimitry Andric LPM.addPass(LoopFlattenPass()); 1793349cc55cSDimitry Andric LPM.addPass(IndVarSimplifyPass()); 1794349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1795349cc55cSDimitry Andric // FIXME: Add loop interchange. 1796349cc55cSDimitry Andric 1797349cc55cSDimitry Andric // Unroll small loops and perform peeling. 1798349cc55cSDimitry Andric LPM.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 1799349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 1800349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 1801349cc55cSDimitry Andric // The loop passes in LPM (LoopFullUnrollPass) do not preserve MemorySSA. 1802349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 1803349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1804349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/true)); 1805349cc55cSDimitry Andric 1806349cc55cSDimitry Andric MainFPM.addPass(LoopDistributePass()); 1807349cc55cSDimitry Andric 1808349cc55cSDimitry Andric addVectorPasses(Level, MainFPM, /* IsFullLTO */ true); 1809349cc55cSDimitry Andric 18101fd87a68SDimitry Andric // Run the OpenMPOpt CGSCC pass again late. 1811*bdd1243dSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(OpenMPOptCGSCCPass())); 18121fd87a68SDimitry Andric 1813349cc55cSDimitry Andric invokePeepholeEPCallbacks(MainFPM, Level); 181481ad6265SDimitry Andric MainFPM.addPass(JumpThreadingPass()); 1815349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM), 1816349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1817349cc55cSDimitry Andric 1818349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic. This pass supports 1819349cc55cSDimitry Andric // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs 1820349cc55cSDimitry Andric // to be run at link time if CFI is enabled. This pass does nothing if 1821349cc55cSDimitry Andric // CFI is disabled. 1822349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1823349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1824349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO pipeline). 1825349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1826349cc55cSDimitry Andric 1827753f127fSDimitry Andric // Enable splitting late in the FullLTO post-link pipeline. 1828349cc55cSDimitry Andric if (EnableHotColdSplit) 1829349cc55cSDimitry Andric MPM.addPass(HotColdSplittingPass()); 1830349cc55cSDimitry Andric 1831349cc55cSDimitry Andric // Add late LTO optimization passes. 1832349cc55cSDimitry Andric // Delete basic blocks, which optimization passes may have killed. 1833fb03ea46SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass( 1834fb03ea46SDimitry Andric SimplifyCFGOptions().convertSwitchRangeToICmp(true).hoistCommonInsts( 1835fb03ea46SDimitry Andric true)))); 1836349cc55cSDimitry Andric 1837349cc55cSDimitry Andric // Drop bodies of available eternally objects to improve GlobalDCE. 1838349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1839349cc55cSDimitry Andric 1840349cc55cSDimitry Andric // Now that we have optimized the program, discard unreachable functions. 1841349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1842349cc55cSDimitry Andric 1843349cc55cSDimitry Andric if (PTO.MergeFunctions) 1844349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1845349cc55cSDimitry Andric 184681ad6265SDimitry Andric if (PTO.CallGraphProfile) 184781ad6265SDimitry Andric MPM.addPass(CGProfilePass()); 184881ad6265SDimitry Andric 184981ad6265SDimitry Andric for (auto &C : FullLinkTimeOptimizationLastEPCallbacks) 185081ad6265SDimitry Andric C(MPM, Level); 185181ad6265SDimitry Andric 1852349cc55cSDimitry Andric // Emit annotation remarks. 1853349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1854349cc55cSDimitry Andric 1855349cc55cSDimitry Andric return MPM; 1856349cc55cSDimitry Andric } 1857349cc55cSDimitry Andric 1858349cc55cSDimitry Andric ModulePassManager PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level, 1859349cc55cSDimitry Andric bool LTOPreLink) { 1860349cc55cSDimitry Andric assert(Level == OptimizationLevel::O0 && 1861349cc55cSDimitry Andric "buildO0DefaultPipeline should only be used with O0"); 1862349cc55cSDimitry Andric 1863349cc55cSDimitry Andric ModulePassManager MPM; 1864349cc55cSDimitry Andric 1865349cc55cSDimitry Andric // Perform pseudo probe instrumentation in O0 mode. This is for the 1866349cc55cSDimitry Andric // consistency between different build modes. For example, a LTO build can be 1867349cc55cSDimitry Andric // mixed with an O0 prelink and an O2 postlink. Loading a sample profile in 1868349cc55cSDimitry Andric // the postlink will require pseudo probe instrumentation in the prelink. 1869349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling) 1870349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 1871349cc55cSDimitry Andric 1872349cc55cSDimitry Andric if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr || 1873349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) 1874349cc55cSDimitry Andric addPGOInstrPassesForO0( 1875349cc55cSDimitry Andric MPM, 1876349cc55cSDimitry Andric /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr), 1877349cc55cSDimitry Andric /* IsCS */ false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile); 1878349cc55cSDimitry Andric 1879349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1880349cc55cSDimitry Andric C(MPM, Level); 1881349cc55cSDimitry Andric 1882349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1883349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1884349cc55cSDimitry Andric 1885349cc55cSDimitry Andric for (auto &C : PipelineEarlySimplificationEPCallbacks) 1886349cc55cSDimitry Andric C(MPM, Level); 1887349cc55cSDimitry Andric 1888349cc55cSDimitry Andric // Build a minimal pipeline based on the semantics required by LLVM, 1889349cc55cSDimitry Andric // which is just that always inlining occurs. Further, disable generating 1890349cc55cSDimitry Andric // lifetime intrinsics to avoid enabling further optimizations during 1891349cc55cSDimitry Andric // code generation. 1892349cc55cSDimitry Andric MPM.addPass(AlwaysInlinerPass( 1893349cc55cSDimitry Andric /*InsertLifetimeIntrinsics=*/false)); 1894349cc55cSDimitry Andric 1895349cc55cSDimitry Andric if (PTO.MergeFunctions) 1896349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1897349cc55cSDimitry Andric 1898349cc55cSDimitry Andric if (EnableMatrix) 1899349cc55cSDimitry Andric MPM.addPass( 1900349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true))); 1901349cc55cSDimitry Andric 1902349cc55cSDimitry Andric if (!CGSCCOptimizerLateEPCallbacks.empty()) { 1903349cc55cSDimitry Andric CGSCCPassManager CGPM; 1904349cc55cSDimitry Andric for (auto &C : CGSCCOptimizerLateEPCallbacks) 1905349cc55cSDimitry Andric C(CGPM, Level); 1906349cc55cSDimitry Andric if (!CGPM.isEmpty()) 1907349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 1908349cc55cSDimitry Andric } 1909349cc55cSDimitry Andric if (!LateLoopOptimizationsEPCallbacks.empty()) { 1910349cc55cSDimitry Andric LoopPassManager LPM; 1911349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 1912349cc55cSDimitry Andric C(LPM, Level); 1913349cc55cSDimitry Andric if (!LPM.isEmpty()) { 1914349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1915349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 1916349cc55cSDimitry Andric } 1917349cc55cSDimitry Andric } 1918349cc55cSDimitry Andric if (!LoopOptimizerEndEPCallbacks.empty()) { 1919349cc55cSDimitry Andric LoopPassManager LPM; 1920349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 1921349cc55cSDimitry Andric C(LPM, Level); 1922349cc55cSDimitry Andric if (!LPM.isEmpty()) { 1923349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1924349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 1925349cc55cSDimitry Andric } 1926349cc55cSDimitry Andric } 1927349cc55cSDimitry Andric if (!ScalarOptimizerLateEPCallbacks.empty()) { 1928349cc55cSDimitry Andric FunctionPassManager FPM; 1929349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 1930349cc55cSDimitry Andric C(FPM, Level); 1931349cc55cSDimitry Andric if (!FPM.isEmpty()) 1932349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1933349cc55cSDimitry Andric } 193481ad6265SDimitry Andric 193581ad6265SDimitry Andric for (auto &C : OptimizerEarlyEPCallbacks) 193681ad6265SDimitry Andric C(MPM, Level); 193781ad6265SDimitry Andric 1938349cc55cSDimitry Andric if (!VectorizerStartEPCallbacks.empty()) { 1939349cc55cSDimitry Andric FunctionPassManager FPM; 1940349cc55cSDimitry Andric for (auto &C : VectorizerStartEPCallbacks) 1941349cc55cSDimitry Andric C(FPM, Level); 1942349cc55cSDimitry Andric if (!FPM.isEmpty()) 1943349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1944349cc55cSDimitry Andric } 1945349cc55cSDimitry Andric 194681ad6265SDimitry Andric ModulePassManager CoroPM; 194781ad6265SDimitry Andric CoroPM.addPass(CoroEarlyPass()); 1948349cc55cSDimitry Andric CGSCCPassManager CGPM; 1949349cc55cSDimitry Andric CGPM.addPass(CoroSplitPass()); 195081ad6265SDimitry Andric CoroPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 195181ad6265SDimitry Andric CoroPM.addPass(CoroCleanupPass()); 195281ad6265SDimitry Andric CoroPM.addPass(GlobalDCEPass()); 195381ad6265SDimitry Andric MPM.addPass(CoroConditionalWrapper(std::move(CoroPM))); 1954349cc55cSDimitry Andric 1955349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1956349cc55cSDimitry Andric C(MPM, Level); 1957349cc55cSDimitry Andric 1958349cc55cSDimitry Andric if (LTOPreLink) 1959349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1960349cc55cSDimitry Andric 19614824e7fdSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass())); 19624824e7fdSDimitry Andric 1963349cc55cSDimitry Andric return MPM; 1964349cc55cSDimitry Andric } 1965349cc55cSDimitry Andric 1966349cc55cSDimitry Andric AAManager PassBuilder::buildDefaultAAPipeline() { 1967349cc55cSDimitry Andric AAManager AA; 1968349cc55cSDimitry Andric 1969349cc55cSDimitry Andric // The order in which these are registered determines their priority when 1970349cc55cSDimitry Andric // being queried. 1971349cc55cSDimitry Andric 1972349cc55cSDimitry Andric // First we register the basic alias analysis that provides the majority of 1973349cc55cSDimitry Andric // per-function local AA logic. This is a stateless, on-demand local set of 1974349cc55cSDimitry Andric // AA techniques. 1975349cc55cSDimitry Andric AA.registerFunctionAnalysis<BasicAA>(); 1976349cc55cSDimitry Andric 1977349cc55cSDimitry Andric // Next we query fast, specialized alias analyses that wrap IR-embedded 1978349cc55cSDimitry Andric // information about aliasing. 1979349cc55cSDimitry Andric AA.registerFunctionAnalysis<ScopedNoAliasAA>(); 1980349cc55cSDimitry Andric AA.registerFunctionAnalysis<TypeBasedAA>(); 1981349cc55cSDimitry Andric 1982349cc55cSDimitry Andric // Add support for querying global aliasing information when available. 1983349cc55cSDimitry Andric // Because the `AAManager` is a function analysis and `GlobalsAA` is a module 1984349cc55cSDimitry Andric // analysis, all that the `AAManager` can do is query for any *cached* 1985349cc55cSDimitry Andric // results from `GlobalsAA` through a readonly proxy. 1986*bdd1243dSDimitry Andric if (EnableGlobalAnalyses) 1987349cc55cSDimitry Andric AA.registerModuleAnalysis<GlobalsAA>(); 1988349cc55cSDimitry Andric 1989349cc55cSDimitry Andric // Add target-specific alias analyses. 1990349cc55cSDimitry Andric if (TM) 1991349cc55cSDimitry Andric TM->registerDefaultAliasAnalyses(AA); 1992349cc55cSDimitry Andric 1993349cc55cSDimitry Andric return AA; 1994349cc55cSDimitry Andric } 1995