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 1706c3fb27SDimitry Andric #include "llvm/ADT/Statistic.h" 18349cc55cSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 19349cc55cSDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h" 20349cc55cSDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h" 21349cc55cSDimitry Andric #include "llvm/Analysis/GlobalsModRef.h" 22349cc55cSDimitry Andric #include "llvm/Analysis/InlineAdvisor.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" 3206c3fb27SDimitry Andric #include "llvm/Support/VirtualFileSystem.h" 33349cc55cSDimitry Andric #include "llvm/Target/TargetMachine.h" 34349cc55cSDimitry Andric #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" 35349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroCleanup.h" 3681ad6265SDimitry Andric #include "llvm/Transforms/Coroutines/CoroConditionalWrapper.h" 37349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroEarly.h" 38349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroElide.h" 39349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroSplit.h" 40*5f757f3fSDimitry Andric #include "llvm/Transforms/HipStdPar/HipStdPar.h" 41349cc55cSDimitry Andric #include "llvm/Transforms/IPO/AlwaysInliner.h" 42349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Annotation2Metadata.h" 43349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ArgumentPromotion.h" 44349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Attributor.h" 45349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CalledValuePropagation.h" 46349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ConstantMerge.h" 47349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CrossDSOCFI.h" 48349cc55cSDimitry Andric #include "llvm/Transforms/IPO/DeadArgumentElimination.h" 49349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ElimAvailExtern.h" 5006c3fb27SDimitry Andric #include "llvm/Transforms/IPO/EmbedBitcodePass.h" 51349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 52349cc55cSDimitry Andric #include "llvm/Transforms/IPO/FunctionAttrs.h" 53349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalDCE.h" 54349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalOpt.h" 55349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalSplit.h" 56349cc55cSDimitry Andric #include "llvm/Transforms/IPO/HotColdSplitting.h" 57349cc55cSDimitry Andric #include "llvm/Transforms/IPO/IROutliner.h" 58349cc55cSDimitry Andric #include "llvm/Transforms/IPO/InferFunctionAttrs.h" 59349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Inliner.h" 60349cc55cSDimitry Andric #include "llvm/Transforms/IPO/LowerTypeTests.h" 6106c3fb27SDimitry Andric #include "llvm/Transforms/IPO/MemProfContextDisambiguation.h" 62349cc55cSDimitry Andric #include "llvm/Transforms/IPO/MergeFunctions.h" 63349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ModuleInliner.h" 64349cc55cSDimitry Andric #include "llvm/Transforms/IPO/OpenMPOpt.h" 65349cc55cSDimitry Andric #include "llvm/Transforms/IPO/PartialInlining.h" 66349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SCCP.h" 67349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfile.h" 68349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfileProbe.h" 69349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" 70349cc55cSDimitry Andric #include "llvm/Transforms/IPO/WholeProgramDevirt.h" 71349cc55cSDimitry Andric #include "llvm/Transforms/InstCombine/InstCombine.h" 72349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/CGProfile.h" 73349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h" 74349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrOrderFile.h" 75349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 76349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/MemProfiler.h" 77349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 78349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ADCE.h" 79349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" 80349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AnnotationRemarks.h" 81349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/BDCE.h" 82349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CallSiteSplitting.h" 83349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ConstraintElimination.h" 84349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" 85349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DFAJumpThreading.h" 86349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DeadStoreElimination.h" 87349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DivRemPairs.h" 88349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/EarlyCSE.h" 89349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Float2Int.h" 90349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/GVN.h" 91349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/IndVarSimplify.h" 92*5f757f3fSDimitry Andric #include "llvm/Transforms/Scalar/InferAlignment.h" 93349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/InstSimplifyPass.h" 94349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/JumpThreading.h" 95349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LICM.h" 96349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDeletion.h" 97349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDistribute.h" 98349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopFlatten.h" 99349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" 100349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 101349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInterchange.h" 102349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopLoadElimination.h" 103349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h" 104349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopRotation.h" 105349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 106349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSink.h" 107349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" 108349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollPass.h" 109*5f757f3fSDimitry Andric #include "llvm/Transforms/Scalar/LoopVersioningLICM.h" 110349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h" 111349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 112349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h" 113349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" 114349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" 115349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/NewGVN.h" 116349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Reassociate.h" 117349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SCCP.h" 118349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SROA.h" 119349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 120349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimplifyCFG.h" 121349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SpeculativeExecution.h" 122349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/TailRecursionElimination.h" 123349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" 124349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AddDiscriminators.h" 125349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 126349cc55cSDimitry Andric #include "llvm/Transforms/Utils/CanonicalizeAliases.h" 12706c3fb27SDimitry Andric #include "llvm/Transforms/Utils/CountVisits.h" 128349cc55cSDimitry Andric #include "llvm/Transforms/Utils/InjectTLIMappings.h" 129349cc55cSDimitry Andric #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" 130349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Mem2Reg.h" 13106c3fb27SDimitry Andric #include "llvm/Transforms/Utils/MoveAutoInit.h" 132349cc55cSDimitry Andric #include "llvm/Transforms/Utils/NameAnonGlobals.h" 133349cc55cSDimitry Andric #include "llvm/Transforms/Utils/RelLookupTableConverter.h" 134349cc55cSDimitry Andric #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" 135349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/LoopVectorize.h" 136349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/SLPVectorizer.h" 137349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/VectorCombine.h" 138349cc55cSDimitry Andric 139349cc55cSDimitry Andric using namespace llvm; 140349cc55cSDimitry Andric 141349cc55cSDimitry Andric static cl::opt<InliningAdvisorMode> UseInlineAdvisor( 142349cc55cSDimitry Andric "enable-ml-inliner", cl::init(InliningAdvisorMode::Default), cl::Hidden, 143349cc55cSDimitry Andric cl::desc("Enable ML policy for inliner. Currently trained for -Oz only"), 144349cc55cSDimitry Andric cl::values(clEnumValN(InliningAdvisorMode::Default, "default", 145bdd1243dSDimitry Andric "Heuristics-based inliner version"), 146349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Development, "development", 147bdd1243dSDimitry Andric "Use development mode (runtime-loadable model)"), 148349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Release, "release", 149bdd1243dSDimitry Andric "Use release mode (AOT-compiled model)"))); 150349cc55cSDimitry Andric 151349cc55cSDimitry Andric static cl::opt<bool> EnableSyntheticCounts( 15281ad6265SDimitry Andric "enable-npm-synthetic-counts", cl::Hidden, 153349cc55cSDimitry Andric cl::desc("Run synthetic function entry count generation " 154349cc55cSDimitry Andric "pass")); 155349cc55cSDimitry Andric 156349cc55cSDimitry Andric /// Flag to enable inline deferral during PGO. 157349cc55cSDimitry Andric static cl::opt<bool> 158349cc55cSDimitry Andric EnablePGOInlineDeferral("enable-npm-pgo-inline-deferral", cl::init(true), 159349cc55cSDimitry Andric cl::Hidden, 160349cc55cSDimitry Andric cl::desc("Enable inline deferral during PGO")); 161349cc55cSDimitry Andric 162349cc55cSDimitry Andric static cl::opt<bool> EnableModuleInliner("enable-module-inliner", 163349cc55cSDimitry Andric cl::init(false), cl::Hidden, 164349cc55cSDimitry Andric cl::desc("Enable module inliner")); 165349cc55cSDimitry Andric 166349cc55cSDimitry Andric static cl::opt<bool> PerformMandatoryInliningsFirst( 167*5f757f3fSDimitry Andric "mandatory-inlining-first", cl::init(false), cl::Hidden, 168349cc55cSDimitry Andric cl::desc("Perform mandatory inlinings module-wide, before performing " 169bdd1243dSDimitry Andric "inlining")); 170349cc55cSDimitry Andric 171349cc55cSDimitry Andric static cl::opt<bool> EnableEagerlyInvalidateAnalyses( 172349cc55cSDimitry Andric "eagerly-invalidate-analyses", cl::init(true), cl::Hidden, 173349cc55cSDimitry Andric cl::desc("Eagerly invalidate more analyses in default pipelines")); 174349cc55cSDimitry Andric 1750eae32dcSDimitry Andric static cl::opt<bool> EnableMergeFunctions( 1760eae32dcSDimitry Andric "enable-merge-functions", cl::init(false), cl::Hidden, 1770eae32dcSDimitry Andric cl::desc("Enable function merging as part of the optimization pipeline")); 1780eae32dcSDimitry Andric 179bdd1243dSDimitry Andric static cl::opt<bool> EnablePostPGOLoopRotation( 180bdd1243dSDimitry Andric "enable-post-pgo-loop-rotation", cl::init(true), cl::Hidden, 181bdd1243dSDimitry Andric cl::desc("Run the loop rotation transformation after PGO instrumentation")); 182bdd1243dSDimitry Andric 183bdd1243dSDimitry Andric static cl::opt<bool> EnableGlobalAnalyses( 184bdd1243dSDimitry Andric "enable-global-analyses", cl::init(true), cl::Hidden, 185bdd1243dSDimitry Andric cl::desc("Enable inter-procedural analyses")); 186bdd1243dSDimitry Andric 187bdd1243dSDimitry Andric static cl::opt<bool> 188bdd1243dSDimitry Andric RunPartialInlining("enable-partial-inlining", cl::init(false), cl::Hidden, 189bdd1243dSDimitry Andric cl::desc("Run Partial inlinining pass")); 190bdd1243dSDimitry Andric 191bdd1243dSDimitry Andric static cl::opt<bool> ExtraVectorizerPasses( 192bdd1243dSDimitry Andric "extra-vectorizer-passes", cl::init(false), cl::Hidden, 193bdd1243dSDimitry Andric cl::desc("Run cleanup optimization passes after vectorization")); 194bdd1243dSDimitry Andric 195bdd1243dSDimitry Andric static cl::opt<bool> RunNewGVN("enable-newgvn", cl::init(false), cl::Hidden, 196bdd1243dSDimitry Andric cl::desc("Run the NewGVN pass")); 197bdd1243dSDimitry Andric 198bdd1243dSDimitry Andric static cl::opt<bool> EnableLoopInterchange( 199bdd1243dSDimitry Andric "enable-loopinterchange", cl::init(false), cl::Hidden, 200bdd1243dSDimitry Andric cl::desc("Enable the experimental LoopInterchange Pass")); 201bdd1243dSDimitry Andric 202bdd1243dSDimitry Andric static cl::opt<bool> EnableUnrollAndJam("enable-unroll-and-jam", 203bdd1243dSDimitry Andric cl::init(false), cl::Hidden, 204bdd1243dSDimitry Andric cl::desc("Enable Unroll And Jam Pass")); 205bdd1243dSDimitry Andric 206bdd1243dSDimitry Andric static cl::opt<bool> EnableLoopFlatten("enable-loop-flatten", cl::init(false), 207bdd1243dSDimitry Andric cl::Hidden, 208bdd1243dSDimitry Andric cl::desc("Enable the LoopFlatten Pass")); 209bdd1243dSDimitry Andric 210bdd1243dSDimitry Andric static cl::opt<bool> 211bdd1243dSDimitry Andric EnableDFAJumpThreading("enable-dfa-jump-thread", 212bdd1243dSDimitry Andric cl::desc("Enable DFA jump threading"), 213bdd1243dSDimitry Andric cl::init(false), cl::Hidden); 214bdd1243dSDimitry Andric 215bdd1243dSDimitry Andric static cl::opt<bool> 216bdd1243dSDimitry Andric EnableHotColdSplit("hot-cold-split", 217bdd1243dSDimitry Andric cl::desc("Enable hot-cold splitting pass")); 218bdd1243dSDimitry Andric 219bdd1243dSDimitry Andric static cl::opt<bool> EnableIROutliner("ir-outliner", cl::init(false), 220bdd1243dSDimitry Andric cl::Hidden, 221bdd1243dSDimitry Andric cl::desc("Enable ir outliner pass")); 222bdd1243dSDimitry Andric 223bdd1243dSDimitry Andric static cl::opt<bool> 224bdd1243dSDimitry Andric DisablePreInliner("disable-preinline", cl::init(false), cl::Hidden, 225bdd1243dSDimitry Andric cl::desc("Disable pre-instrumentation inliner")); 226bdd1243dSDimitry Andric 227bdd1243dSDimitry Andric static cl::opt<int> PreInlineThreshold( 228bdd1243dSDimitry Andric "preinline-threshold", cl::Hidden, cl::init(75), 229bdd1243dSDimitry Andric cl::desc("Control the amount of inlining in pre-instrumentation inliner " 230bdd1243dSDimitry Andric "(default = 75)")); 231bdd1243dSDimitry Andric 232bdd1243dSDimitry Andric static cl::opt<bool> 233bdd1243dSDimitry Andric EnableGVNHoist("enable-gvn-hoist", 234bdd1243dSDimitry Andric cl::desc("Enable the GVN hoisting pass (default = off)")); 235bdd1243dSDimitry Andric 236bdd1243dSDimitry Andric static cl::opt<bool> 237bdd1243dSDimitry Andric EnableGVNSink("enable-gvn-sink", 238bdd1243dSDimitry Andric cl::desc("Enable the GVN sinking pass (default = off)")); 239bdd1243dSDimitry Andric 240bdd1243dSDimitry Andric // This option is used in simplifying testing SampleFDO optimizations for 241bdd1243dSDimitry Andric // profile loading. 242bdd1243dSDimitry Andric static cl::opt<bool> 243bdd1243dSDimitry Andric EnableCHR("enable-chr", cl::init(true), cl::Hidden, 244bdd1243dSDimitry Andric cl::desc("Enable control height reduction optimization (CHR)")); 245bdd1243dSDimitry Andric 246bdd1243dSDimitry Andric static cl::opt<bool> FlattenedProfileUsed( 247bdd1243dSDimitry Andric "flattened-profile-used", cl::init(false), cl::Hidden, 248bdd1243dSDimitry Andric cl::desc("Indicate the sample profile being used is flattened, i.e., " 249bdd1243dSDimitry Andric "no inline hierachy exists in the profile")); 250bdd1243dSDimitry Andric 251bdd1243dSDimitry Andric static cl::opt<bool> EnableOrderFileInstrumentation( 252bdd1243dSDimitry Andric "enable-order-file-instrumentation", cl::init(false), cl::Hidden, 253bdd1243dSDimitry Andric cl::desc("Enable order file instrumentation (default = off)")); 254bdd1243dSDimitry Andric 255bdd1243dSDimitry Andric static cl::opt<bool> 256bdd1243dSDimitry Andric EnableMatrix("enable-matrix", cl::init(false), cl::Hidden, 257bdd1243dSDimitry Andric cl::desc("Enable lowering of the matrix intrinsics")); 258bdd1243dSDimitry Andric 259bdd1243dSDimitry Andric static cl::opt<bool> EnableConstraintElimination( 26006c3fb27SDimitry Andric "enable-constraint-elimination", cl::init(true), cl::Hidden, 261bdd1243dSDimitry Andric cl::desc( 262bdd1243dSDimitry Andric "Enable pass to eliminate conditions based on linear constraints")); 263bdd1243dSDimitry Andric 264bdd1243dSDimitry Andric static cl::opt<AttributorRunOption> AttributorRun( 265bdd1243dSDimitry Andric "attributor-enable", cl::Hidden, cl::init(AttributorRunOption::NONE), 266bdd1243dSDimitry Andric cl::desc("Enable the attributor inter-procedural deduction pass"), 267bdd1243dSDimitry Andric cl::values(clEnumValN(AttributorRunOption::ALL, "all", 268bdd1243dSDimitry Andric "enable all attributor runs"), 269bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::MODULE, "module", 270bdd1243dSDimitry Andric "enable module-wide attributor runs"), 271bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::CGSCC, "cgscc", 272bdd1243dSDimitry Andric "enable call graph SCC attributor runs"), 273bdd1243dSDimitry Andric clEnumValN(AttributorRunOption::NONE, "none", 274bdd1243dSDimitry Andric "disable attributor runs"))); 275bdd1243dSDimitry Andric 276*5f757f3fSDimitry Andric static cl::opt<bool> UseLoopVersioningLICM( 277*5f757f3fSDimitry Andric "enable-loop-versioning-licm", cl::init(false), cl::Hidden, 278*5f757f3fSDimitry Andric cl::desc("Enable the experimental Loop Versioning LICM pass")); 279*5f757f3fSDimitry Andric 280*5f757f3fSDimitry Andric namespace llvm { 28106c3fb27SDimitry Andric cl::opt<bool> EnableMemProfContextDisambiguation( 28206c3fb27SDimitry Andric "enable-memprof-context-disambiguation", cl::init(false), cl::Hidden, 28306c3fb27SDimitry Andric cl::ZeroOrMore, cl::desc("Enable MemProf context disambiguation")); 28406c3fb27SDimitry Andric 285*5f757f3fSDimitry Andric extern cl::opt<bool> EnableInferAlignmentPass; 286*5f757f3fSDimitry Andric } // namespace llvm 287*5f757f3fSDimitry Andric 288349cc55cSDimitry Andric PipelineTuningOptions::PipelineTuningOptions() { 289349cc55cSDimitry Andric LoopInterleaving = true; 290349cc55cSDimitry Andric LoopVectorization = true; 291349cc55cSDimitry Andric SLPVectorization = false; 292349cc55cSDimitry Andric LoopUnrolling = true; 293349cc55cSDimitry Andric ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll; 294349cc55cSDimitry Andric LicmMssaOptCap = SetLicmMssaOptCap; 295349cc55cSDimitry Andric LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; 296349cc55cSDimitry Andric CallGraphProfile = true; 29706c3fb27SDimitry Andric UnifiedLTO = false; 2980eae32dcSDimitry Andric MergeFunctions = EnableMergeFunctions; 299bdd1243dSDimitry Andric InlinerThreshold = -1; 300349cc55cSDimitry Andric EagerlyInvalidateAnalyses = EnableEagerlyInvalidateAnalyses; 301349cc55cSDimitry Andric } 302349cc55cSDimitry Andric 303349cc55cSDimitry Andric namespace llvm { 304349cc55cSDimitry Andric extern cl::opt<unsigned> MaxDevirtIterations; 305349cc55cSDimitry Andric } // namespace llvm 306349cc55cSDimitry Andric 307349cc55cSDimitry Andric void PassBuilder::invokePeepholeEPCallbacks(FunctionPassManager &FPM, 308349cc55cSDimitry Andric OptimizationLevel Level) { 309349cc55cSDimitry Andric for (auto &C : PeepholeEPCallbacks) 310349cc55cSDimitry Andric C(FPM, Level); 311349cc55cSDimitry Andric } 31206c3fb27SDimitry Andric void PassBuilder::invokeLateLoopOptimizationsEPCallbacks( 31306c3fb27SDimitry Andric LoopPassManager &LPM, OptimizationLevel Level) { 31406c3fb27SDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 31506c3fb27SDimitry Andric C(LPM, Level); 31606c3fb27SDimitry Andric } 31706c3fb27SDimitry Andric void PassBuilder::invokeLoopOptimizerEndEPCallbacks(LoopPassManager &LPM, 31806c3fb27SDimitry Andric OptimizationLevel Level) { 31906c3fb27SDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 32006c3fb27SDimitry Andric C(LPM, Level); 32106c3fb27SDimitry Andric } 32206c3fb27SDimitry Andric void PassBuilder::invokeScalarOptimizerLateEPCallbacks( 32306c3fb27SDimitry Andric FunctionPassManager &FPM, OptimizationLevel Level) { 32406c3fb27SDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 32506c3fb27SDimitry Andric C(FPM, Level); 32606c3fb27SDimitry Andric } 32706c3fb27SDimitry Andric void PassBuilder::invokeCGSCCOptimizerLateEPCallbacks(CGSCCPassManager &CGPM, 32806c3fb27SDimitry Andric OptimizationLevel Level) { 32906c3fb27SDimitry Andric for (auto &C : CGSCCOptimizerLateEPCallbacks) 33006c3fb27SDimitry Andric C(CGPM, Level); 33106c3fb27SDimitry Andric } 33206c3fb27SDimitry Andric void PassBuilder::invokeVectorizerStartEPCallbacks(FunctionPassManager &FPM, 33306c3fb27SDimitry Andric OptimizationLevel Level) { 33406c3fb27SDimitry Andric for (auto &C : VectorizerStartEPCallbacks) 33506c3fb27SDimitry Andric C(FPM, Level); 33606c3fb27SDimitry Andric } 33706c3fb27SDimitry Andric void PassBuilder::invokeOptimizerEarlyEPCallbacks(ModulePassManager &MPM, 33806c3fb27SDimitry Andric OptimizationLevel Level) { 33906c3fb27SDimitry Andric for (auto &C : OptimizerEarlyEPCallbacks) 34006c3fb27SDimitry Andric C(MPM, Level); 34106c3fb27SDimitry Andric } 34206c3fb27SDimitry Andric void PassBuilder::invokeOptimizerLastEPCallbacks(ModulePassManager &MPM, 34306c3fb27SDimitry Andric OptimizationLevel Level) { 34406c3fb27SDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 34506c3fb27SDimitry Andric C(MPM, Level); 34606c3fb27SDimitry Andric } 34706c3fb27SDimitry Andric void PassBuilder::invokeFullLinkTimeOptimizationEarlyEPCallbacks( 34806c3fb27SDimitry Andric ModulePassManager &MPM, OptimizationLevel Level) { 34906c3fb27SDimitry Andric for (auto &C : FullLinkTimeOptimizationEarlyEPCallbacks) 35006c3fb27SDimitry Andric C(MPM, Level); 35106c3fb27SDimitry Andric } 35206c3fb27SDimitry Andric void PassBuilder::invokeFullLinkTimeOptimizationLastEPCallbacks( 35306c3fb27SDimitry Andric ModulePassManager &MPM, OptimizationLevel Level) { 35406c3fb27SDimitry Andric for (auto &C : FullLinkTimeOptimizationLastEPCallbacks) 35506c3fb27SDimitry Andric C(MPM, Level); 35606c3fb27SDimitry Andric } 35706c3fb27SDimitry Andric void PassBuilder::invokePipelineStartEPCallbacks(ModulePassManager &MPM, 35806c3fb27SDimitry Andric OptimizationLevel Level) { 35906c3fb27SDimitry Andric for (auto &C : PipelineStartEPCallbacks) 36006c3fb27SDimitry Andric C(MPM, Level); 36106c3fb27SDimitry Andric } 36206c3fb27SDimitry Andric void PassBuilder::invokePipelineEarlySimplificationEPCallbacks( 36306c3fb27SDimitry Andric ModulePassManager &MPM, OptimizationLevel Level) { 36406c3fb27SDimitry Andric for (auto &C : PipelineEarlySimplificationEPCallbacks) 36506c3fb27SDimitry Andric C(MPM, Level); 36606c3fb27SDimitry Andric } 367349cc55cSDimitry Andric 368349cc55cSDimitry Andric // Helper to add AnnotationRemarksPass. 369349cc55cSDimitry Andric static void addAnnotationRemarksPass(ModulePassManager &MPM) { 37081ad6265SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass())); 371349cc55cSDimitry Andric } 372349cc55cSDimitry Andric 373349cc55cSDimitry Andric // Helper to check if the current compilation phase is preparing for LTO 374349cc55cSDimitry Andric static bool isLTOPreLink(ThinOrFullLTOPhase Phase) { 375349cc55cSDimitry Andric return Phase == ThinOrFullLTOPhase::ThinLTOPreLink || 376349cc55cSDimitry Andric Phase == ThinOrFullLTOPhase::FullLTOPreLink; 377349cc55cSDimitry Andric } 378349cc55cSDimitry Andric 379349cc55cSDimitry Andric // TODO: Investigate the cost/benefit of tail call elimination on debugging. 380349cc55cSDimitry Andric FunctionPassManager 381349cc55cSDimitry Andric PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, 382349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 383349cc55cSDimitry Andric 384349cc55cSDimitry Andric FunctionPassManager FPM; 385349cc55cSDimitry Andric 38606c3fb27SDimitry Andric if (AreStatisticsEnabled()) 38706c3fb27SDimitry Andric FPM.addPass(CountVisitsPass()); 38806c3fb27SDimitry Andric 389349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 390349cc55cSDimitry Andric // scalars. 391bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 392349cc55cSDimitry Andric 393349cc55cSDimitry Andric // Catch trivial redundancies 394349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 395349cc55cSDimitry Andric 396349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 397fb03ea46SDimitry Andric FPM.addPass( 398fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 399349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 400349cc55cSDimitry Andric 401349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 402349cc55cSDimitry Andric 403349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 404349cc55cSDimitry Andric 405fb03ea46SDimitry Andric FPM.addPass( 406fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 407349cc55cSDimitry Andric 408349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 409349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 410349cc55cSDimitry Andric // minimal multiplication trees. 411349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 412349cc55cSDimitry Andric 413349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 414349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 415349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 416349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 417349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 418349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 419349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 420349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 421349cc55cSDimitry Andric // `LoopInstSimplify`. 422349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 423349cc55cSDimitry Andric 424349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 425349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 426349cc55cSDimitry Andric // implications on the outer loop. 427349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 428349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 429349cc55cSDimitry Andric 430349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 431fb03ea46SDimitry Andric // to reduce amount of IR that will have to be duplicated. However, 432fb03ea46SDimitry Andric // do not perform speculative hoisting the first time as LICM 433fb03ea46SDimitry Andric // will destroy metadata that may not need to be destroyed if run 434fb03ea46SDimitry Andric // after loop rotation. 435349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 436fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 437fb03ea46SDimitry Andric /*AllowSpeculation=*/false)); 438349cc55cSDimitry Andric 439349cc55cSDimitry Andric LPM1.addPass(LoopRotatePass(/* Disable header duplication */ true, 440349cc55cSDimitry Andric isLTOPreLink(Phase))); 441349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 442fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 443fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 444349cc55cSDimitry Andric LPM1.addPass(SimpleLoopUnswitchPass()); 44504eeddc0SDimitry Andric if (EnableLoopFlatten) 44604eeddc0SDimitry Andric LPM1.addPass(LoopFlattenPass()); 447349cc55cSDimitry Andric 448349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 449349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 450349cc55cSDimitry Andric 45106c3fb27SDimitry Andric invokeLateLoopOptimizationsEPCallbacks(LPM2, Level); 452349cc55cSDimitry Andric 453349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 454349cc55cSDimitry Andric 455349cc55cSDimitry Andric if (EnableLoopInterchange) 456349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 457349cc55cSDimitry Andric 458349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 459349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 460349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 461349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 462349cc55cSDimitry Andric // attention to it. 463349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 464349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 465349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 466349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 467349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 468349cc55cSDimitry Andric 46906c3fb27SDimitry Andric invokeLoopOptimizerEndEPCallbacks(LPM2, Level); 470349cc55cSDimitry Andric 471349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 472349cc55cSDimitry Andric /*UseMemorySSA=*/true, 473349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 474fb03ea46SDimitry Andric FPM.addPass( 475fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 476349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 477349cc55cSDimitry Andric // The loop passes in LPM2 (LoopFullUnrollPass) do not preserve MemorySSA. 478349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 479349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 480349cc55cSDimitry Andric /*UseMemorySSA=*/false, 481349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 482349cc55cSDimitry Andric 483349cc55cSDimitry Andric // Delete small array after loop unroll. 484bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 485349cc55cSDimitry Andric 486349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 487349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 488349cc55cSDimitry Andric 489349cc55cSDimitry Andric // Sparse conditional constant propagation. 490349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 491349cc55cSDimitry Andric // before... 492349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 493349cc55cSDimitry Andric 494349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 495349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 496349cc55cSDimitry Andric // opportunities that creates). 497349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 498349cc55cSDimitry Andric 499349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 500349cc55cSDimitry Andric // opportunities opened up by them. 501349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 502349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 503349cc55cSDimitry Andric 504349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 505349cc55cSDimitry Andric 50606c3fb27SDimitry Andric invokeScalarOptimizerLateEPCallbacks(FPM, Level); 507349cc55cSDimitry Andric 508349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 509349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 510349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 511349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 512fb03ea46SDimitry Andric FPM.addPass( 513fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 514349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 515349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 516349cc55cSDimitry Andric 517349cc55cSDimitry Andric return FPM; 518349cc55cSDimitry Andric } 519349cc55cSDimitry Andric 520349cc55cSDimitry Andric FunctionPassManager 521349cc55cSDimitry Andric PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, 522349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 523349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Must request optimizations!"); 524349cc55cSDimitry Andric 525349cc55cSDimitry Andric // The O1 pipeline has a separate pipeline creation function to simplify 526349cc55cSDimitry Andric // construction readability. 527349cc55cSDimitry Andric if (Level.getSpeedupLevel() == 1) 528349cc55cSDimitry Andric return buildO1FunctionSimplificationPipeline(Level, Phase); 529349cc55cSDimitry Andric 530349cc55cSDimitry Andric FunctionPassManager FPM; 531349cc55cSDimitry Andric 53206c3fb27SDimitry Andric if (AreStatisticsEnabled()) 53306c3fb27SDimitry Andric FPM.addPass(CountVisitsPass()); 53406c3fb27SDimitry Andric 535349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 536349cc55cSDimitry Andric // scalars. 537bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 538349cc55cSDimitry Andric 539349cc55cSDimitry Andric // Catch trivial redundancies 540349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 541349cc55cSDimitry Andric if (EnableKnowledgeRetention) 542349cc55cSDimitry Andric FPM.addPass(AssumeSimplifyPass()); 543349cc55cSDimitry Andric 544349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 545349cc55cSDimitry Andric if (EnableGVNHoist) 546349cc55cSDimitry Andric FPM.addPass(GVNHoistPass()); 547349cc55cSDimitry Andric 548349cc55cSDimitry Andric // Global value numbering based sinking. 549349cc55cSDimitry Andric if (EnableGVNSink) { 550349cc55cSDimitry Andric FPM.addPass(GVNSinkPass()); 551fb03ea46SDimitry Andric FPM.addPass( 552fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 553349cc55cSDimitry Andric } 554349cc55cSDimitry Andric 555349cc55cSDimitry Andric // Speculative execution if the target has divergent branches; otherwise nop. 556349cc55cSDimitry Andric FPM.addPass(SpeculativeExecutionPass(/* OnlyIfDivergentTarget =*/true)); 557349cc55cSDimitry Andric 558349cc55cSDimitry Andric // Optimize based on known information about branches, and cleanup afterward. 559349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 560349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 561349cc55cSDimitry Andric 562fb03ea46SDimitry Andric FPM.addPass( 563fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 5640eae32dcSDimitry Andric FPM.addPass(InstCombinePass()); 565349cc55cSDimitry Andric FPM.addPass(AggressiveInstCombinePass()); 566349cc55cSDimitry Andric 567349cc55cSDimitry Andric if (!Level.isOptimizingForSize()) 568349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 569349cc55cSDimitry Andric 570349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 571349cc55cSDimitry Andric 572349cc55cSDimitry Andric // For PGO use pipeline, try to optimize memory intrinsics such as memcpy 573349cc55cSDimitry Andric // using the size value profile. Don't perform this when optimizing for size. 574349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse && 575349cc55cSDimitry Andric !Level.isOptimizingForSize()) 576349cc55cSDimitry Andric FPM.addPass(PGOMemOPSizeOpt()); 577349cc55cSDimitry Andric 578349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 579fb03ea46SDimitry Andric FPM.addPass( 580fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 581349cc55cSDimitry Andric 582349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 583349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 584349cc55cSDimitry Andric // minimal multiplication trees. 585349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 586349cc55cSDimitry Andric 587*5f757f3fSDimitry Andric if (EnableConstraintElimination) 588*5f757f3fSDimitry Andric FPM.addPass(ConstraintEliminationPass()); 589*5f757f3fSDimitry Andric 590349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 591349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 592349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 593349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 594349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 595349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 596349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 597349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 598349cc55cSDimitry Andric // `LoopInstSimplify`. 599349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 600349cc55cSDimitry Andric 601349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 602349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 603349cc55cSDimitry Andric // implications on the outer loop. 604349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 605349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 606349cc55cSDimitry Andric 607349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 608fb03ea46SDimitry Andric // to reduce amount of IR that will have to be duplicated. However, 609fb03ea46SDimitry Andric // do not perform speculative hoisting the first time as LICM 610fb03ea46SDimitry Andric // will destroy metadata that may not need to be destroyed if run 611fb03ea46SDimitry Andric // after loop rotation. 612349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 613fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 614fb03ea46SDimitry Andric /*AllowSpeculation=*/false)); 615349cc55cSDimitry Andric 616349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 617349cc55cSDimitry Andric LPM1.addPass( 618349cc55cSDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz, isLTOPreLink(Phase))); 619349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 620fb03ea46SDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 621fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 622349cc55cSDimitry Andric LPM1.addPass( 62306c3fb27SDimitry Andric SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3)); 62404eeddc0SDimitry Andric if (EnableLoopFlatten) 62504eeddc0SDimitry Andric LPM1.addPass(LoopFlattenPass()); 62604eeddc0SDimitry Andric 627349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 628349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 629349cc55cSDimitry Andric 63006c3fb27SDimitry Andric invokeLateLoopOptimizationsEPCallbacks(LPM2, Level); 631349cc55cSDimitry Andric 632349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 633349cc55cSDimitry Andric 634349cc55cSDimitry Andric if (EnableLoopInterchange) 635349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 636349cc55cSDimitry Andric 637349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 638349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 639349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 640349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 641349cc55cSDimitry Andric // attention to it. 642349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 643349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 644349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 645349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 646349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 647349cc55cSDimitry Andric 64806c3fb27SDimitry Andric invokeLoopOptimizerEndEPCallbacks(LPM2, Level); 649349cc55cSDimitry Andric 650349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 651349cc55cSDimitry Andric /*UseMemorySSA=*/true, 652349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 653fb03ea46SDimitry Andric FPM.addPass( 654fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 655349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 656349cc55cSDimitry Andric // The loop passes in LPM2 (LoopIdiomRecognizePass, IndVarSimplifyPass, 657349cc55cSDimitry Andric // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA. 658349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 659349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 660349cc55cSDimitry Andric /*UseMemorySSA=*/false, 661349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 662349cc55cSDimitry Andric 663349cc55cSDimitry Andric // Delete small array after loop unroll. 664bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 665349cc55cSDimitry Andric 666bdd1243dSDimitry Andric // Try vectorization/scalarization transforms that are both improvements 667bdd1243dSDimitry Andric // themselves and can allow further folds with GVN and InstCombine. 668bdd1243dSDimitry Andric FPM.addPass(VectorCombinePass(/*TryEarlyFoldsOnly=*/true)); 669349cc55cSDimitry Andric 670349cc55cSDimitry Andric // Eliminate redundancies. 671349cc55cSDimitry Andric FPM.addPass(MergedLoadStoreMotionPass()); 672349cc55cSDimitry Andric if (RunNewGVN) 673349cc55cSDimitry Andric FPM.addPass(NewGVNPass()); 674349cc55cSDimitry Andric else 675349cc55cSDimitry Andric FPM.addPass(GVNPass()); 676349cc55cSDimitry Andric 677349cc55cSDimitry Andric // Sparse conditional constant propagation. 678349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 679349cc55cSDimitry Andric // before... 680349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 681349cc55cSDimitry Andric 682349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 683349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 684349cc55cSDimitry Andric // opportunities that creates). 685349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 686349cc55cSDimitry Andric 687349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 688349cc55cSDimitry Andric // opportunities opened up by them. 689349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 690349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 691349cc55cSDimitry Andric 692349cc55cSDimitry Andric // Re-consider control flow based optimizations after redundancy elimination, 693349cc55cSDimitry Andric // redo DCE, etc. 694349cc55cSDimitry Andric if (EnableDFAJumpThreading && Level.getSizeLevel() == 0) 695349cc55cSDimitry Andric FPM.addPass(DFAJumpThreadingPass()); 696349cc55cSDimitry Andric 697349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 698349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 699349cc55cSDimitry Andric 700349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 701349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 702349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 703349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 704349cc55cSDimitry Andric 705349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 706349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 707349cc55cSDimitry Andric 708349cc55cSDimitry Andric FPM.addPass(DSEPass()); 70906c3fb27SDimitry Andric FPM.addPass(MoveAutoInitPass()); 71006c3fb27SDimitry Andric 711349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 712fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 713fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 71406c3fb27SDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/false)); 715349cc55cSDimitry Andric 716349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 717349cc55cSDimitry Andric 71806c3fb27SDimitry Andric invokeScalarOptimizerLateEPCallbacks(FPM, Level); 719349cc55cSDimitry Andric 720fb03ea46SDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 721fb03ea46SDimitry Andric .convertSwitchRangeToICmp(true) 722fb03ea46SDimitry Andric .hoistCommonInsts(true) 723fb03ea46SDimitry Andric .sinkCommonInsts(true))); 724349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 725349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 726349cc55cSDimitry Andric 727349cc55cSDimitry Andric return FPM; 728349cc55cSDimitry Andric } 729349cc55cSDimitry Andric 730349cc55cSDimitry Andric void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) { 731349cc55cSDimitry Andric MPM.addPass(CanonicalizeAliasesPass()); 732349cc55cSDimitry Andric MPM.addPass(NameAnonGlobalPass()); 733349cc55cSDimitry Andric } 734349cc55cSDimitry Andric 735*5f757f3fSDimitry Andric void PassBuilder::addPreInlinerPasses(ModulePassManager &MPM, 736*5f757f3fSDimitry Andric OptimizationLevel Level, 737*5f757f3fSDimitry Andric ThinOrFullLTOPhase LTOPhase) { 738349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!"); 739*5f757f3fSDimitry Andric if (DisablePreInliner) 740*5f757f3fSDimitry Andric return; 741349cc55cSDimitry Andric InlineParams IP; 742349cc55cSDimitry Andric 743349cc55cSDimitry Andric IP.DefaultThreshold = PreInlineThreshold; 744349cc55cSDimitry Andric 745349cc55cSDimitry Andric // FIXME: The hint threshold has the same value used by the regular inliner 746349cc55cSDimitry Andric // when not optimzing for size. This should probably be lowered after 747349cc55cSDimitry Andric // performance testing. 748349cc55cSDimitry Andric // FIXME: this comment is cargo culted from the old pass manager, revisit). 749349cc55cSDimitry Andric IP.HintThreshold = Level.isOptimizingForSize() ? PreInlineThreshold : 325; 75081ad6265SDimitry Andric ModuleInlinerWrapperPass MIWP( 75181ad6265SDimitry Andric IP, /* MandatoryFirst */ true, 75281ad6265SDimitry Andric InlineContext{LTOPhase, InlinePass::EarlyInliner}); 753349cc55cSDimitry Andric CGSCCPassManager &CGPipeline = MIWP.getPM(); 754349cc55cSDimitry Andric 755349cc55cSDimitry Andric FunctionPassManager FPM; 756bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 757349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies. 758fb03ea46SDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp( 759fb03ea46SDimitry Andric true))); // Merge & remove basic blocks. 760349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); // Combine silly sequences. 761349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 762349cc55cSDimitry Andric 763349cc55cSDimitry Andric CGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 764349cc55cSDimitry Andric std::move(FPM), PTO.EagerlyInvalidateAnalyses)); 765349cc55cSDimitry Andric 766349cc55cSDimitry Andric MPM.addPass(std::move(MIWP)); 767349cc55cSDimitry Andric 768349cc55cSDimitry Andric // Delete anything that is now dead to make sure that we don't instrument 769349cc55cSDimitry Andric // dead code. Instrumentation can end up keeping dead code around and 770349cc55cSDimitry Andric // dramatically increase code size. 771349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 772349cc55cSDimitry Andric } 773349cc55cSDimitry Andric 774*5f757f3fSDimitry Andric void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, 775*5f757f3fSDimitry Andric OptimizationLevel Level, bool RunProfileGen, 776*5f757f3fSDimitry Andric bool IsCS, bool AtomicCounterUpdate, 777*5f757f3fSDimitry Andric std::string ProfileFile, 778*5f757f3fSDimitry Andric std::string ProfileRemappingFile, 779*5f757f3fSDimitry Andric IntrusiveRefCntPtr<vfs::FileSystem> FS) { 780*5f757f3fSDimitry Andric assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!"); 781*5f757f3fSDimitry Andric 782349cc55cSDimitry Andric if (!RunProfileGen) { 783349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 78406c3fb27SDimitry Andric MPM.addPass( 78506c3fb27SDimitry Andric PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS, FS)); 786349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 787349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 788349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 789349cc55cSDimitry Andric return; 790349cc55cSDimitry Andric } 791349cc55cSDimitry Andric 792349cc55cSDimitry Andric // Perform PGO instrumentation. 793349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 794349cc55cSDimitry Andric 795bdd1243dSDimitry Andric if (EnablePostPGOLoopRotation) { 796349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 79781ad6265SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 79881ad6265SDimitry Andric createFunctionToLoopPassAdaptor( 79981ad6265SDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz), 80081ad6265SDimitry Andric /*UseMemorySSA=*/false, 80181ad6265SDimitry Andric /*UseBlockFrequencyInfo=*/false), 802349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 803bdd1243dSDimitry Andric } 804349cc55cSDimitry Andric 805349cc55cSDimitry Andric // Add the profile lowering pass. 806349cc55cSDimitry Andric InstrProfOptions Options; 807349cc55cSDimitry Andric if (!ProfileFile.empty()) 808349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 809349cc55cSDimitry Andric // Do counter promotion at Level greater than O0. 810349cc55cSDimitry Andric Options.DoCounterPromotion = true; 811349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 812*5f757f3fSDimitry Andric Options.Atomic = AtomicCounterUpdate; 813*5f757f3fSDimitry Andric MPM.addPass(InstrProfilingLoweringPass(Options, IsCS)); 814349cc55cSDimitry Andric } 815349cc55cSDimitry Andric 81606c3fb27SDimitry Andric void PassBuilder::addPGOInstrPassesForO0( 81706c3fb27SDimitry Andric ModulePassManager &MPM, bool RunProfileGen, bool IsCS, 818*5f757f3fSDimitry Andric bool AtomicCounterUpdate, std::string ProfileFile, 819*5f757f3fSDimitry Andric std::string ProfileRemappingFile, IntrusiveRefCntPtr<vfs::FileSystem> FS) { 820349cc55cSDimitry Andric if (!RunProfileGen) { 821349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 82206c3fb27SDimitry Andric MPM.addPass( 82306c3fb27SDimitry Andric PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS, FS)); 824349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 825349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 826349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 827349cc55cSDimitry Andric return; 828349cc55cSDimitry Andric } 829349cc55cSDimitry Andric 830349cc55cSDimitry Andric // Perform PGO instrumentation. 831349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 832349cc55cSDimitry Andric // Add the profile lowering pass. 833349cc55cSDimitry Andric InstrProfOptions Options; 834349cc55cSDimitry Andric if (!ProfileFile.empty()) 835349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 836349cc55cSDimitry Andric // Do not do counter promotion at O0. 837349cc55cSDimitry Andric Options.DoCounterPromotion = false; 838349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 839*5f757f3fSDimitry Andric Options.Atomic = AtomicCounterUpdate; 840*5f757f3fSDimitry Andric MPM.addPass(InstrProfilingLoweringPass(Options, IsCS)); 841349cc55cSDimitry Andric } 842349cc55cSDimitry Andric 843349cc55cSDimitry Andric static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) { 844349cc55cSDimitry Andric return getInlineParams(Level.getSpeedupLevel(), Level.getSizeLevel()); 845349cc55cSDimitry Andric } 846349cc55cSDimitry Andric 847349cc55cSDimitry Andric ModuleInlinerWrapperPass 848349cc55cSDimitry Andric PassBuilder::buildInlinerPipeline(OptimizationLevel Level, 849349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 850bdd1243dSDimitry Andric InlineParams IP; 851bdd1243dSDimitry Andric if (PTO.InlinerThreshold == -1) 852bdd1243dSDimitry Andric IP = getInlineParamsFromOptLevel(Level); 853bdd1243dSDimitry Andric else 854bdd1243dSDimitry Andric IP = getInlineParams(PTO.InlinerThreshold); 85581ad6265SDimitry Andric // For PreLinkThinLTO + SamplePGO, set hot-caller threshold to 0 to 85681ad6265SDimitry Andric // disable hot callsite inline (as much as possible [1]) because it makes 85781ad6265SDimitry Andric // profile annotation in the backend inaccurate. 85881ad6265SDimitry Andric // 85981ad6265SDimitry Andric // [1] Note the cost of a function could be below zero due to erased 86081ad6265SDimitry Andric // prologue / epilogue. 861349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 862349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 863349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 864349cc55cSDimitry Andric 865349cc55cSDimitry Andric if (PGOOpt) 866349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 867349cc55cSDimitry Andric 868bdd1243dSDimitry Andric ModuleInlinerWrapperPass MIWP(IP, PerformMandatoryInliningsFirst, 86981ad6265SDimitry Andric InlineContext{Phase, InlinePass::CGSCCInliner}, 870349cc55cSDimitry Andric UseInlineAdvisor, MaxDevirtIterations); 871349cc55cSDimitry Andric 872349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 873349cc55cSDimitry Andric // the CGSCC pipeline. 874*5f757f3fSDimitry Andric if (EnableGlobalAnalyses) { 875349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>()); 876*5f757f3fSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly 877*5f757f3fSDimitry Andric // available GlobalsAA. 878349cc55cSDimitry Andric MIWP.addModulePass( 879349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 880*5f757f3fSDimitry Andric } 881349cc55cSDimitry Andric 882349cc55cSDimitry Andric // Require the ProfileSummaryAnalysis for the module so we can query it within 883349cc55cSDimitry Andric // the inliner pass. 884349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 885349cc55cSDimitry Andric 886349cc55cSDimitry Andric // Now begin the main postorder CGSCC pipeline. 887349cc55cSDimitry Andric // FIXME: The current CGSCC pipeline has its origins in the legacy pass 888349cc55cSDimitry Andric // manager and trying to emulate its precise behavior. Much of this doesn't 889349cc55cSDimitry Andric // make a lot of sense and we should revisit the core CGSCC structure. 890349cc55cSDimitry Andric CGSCCPassManager &MainCGPipeline = MIWP.getPM(); 891349cc55cSDimitry Andric 892349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 893349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 894349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 895349cc55cSDimitry Andric // invoke or a call. 896349cc55cSDimitry Andric 897349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::CGSCC) 898349cc55cSDimitry Andric MainCGPipeline.addPass(AttributorCGSCCPass()); 899349cc55cSDimitry Andric 90006c3fb27SDimitry Andric // Deduce function attributes. We do another run of this after the function 90106c3fb27SDimitry Andric // simplification pipeline, so this only needs to run when it could affect the 90206c3fb27SDimitry Andric // function simplification pipeline, which is only the case with recursive 90306c3fb27SDimitry Andric // functions. 90406c3fb27SDimitry Andric MainCGPipeline.addPass(PostOrderFunctionAttrsPass(/*SkipNonRecursive*/ true)); 905349cc55cSDimitry Andric 906349cc55cSDimitry Andric // When at O3 add argument promotion to the pass pipeline. 907349cc55cSDimitry Andric // FIXME: It isn't at all clear why this should be limited to O3. 908349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 909349cc55cSDimitry Andric MainCGPipeline.addPass(ArgumentPromotionPass()); 910349cc55cSDimitry Andric 911349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations. This is a (quick!) no-op if 912349cc55cSDimitry Andric // there are no OpenMP runtime calls present in the module. 913349cc55cSDimitry Andric if (Level == OptimizationLevel::O2 || Level == OptimizationLevel::O3) 914349cc55cSDimitry Andric MainCGPipeline.addPass(OpenMPOptCGSCCPass()); 915349cc55cSDimitry Andric 91606c3fb27SDimitry Andric invokeCGSCCOptimizerLateEPCallbacks(MainCGPipeline, Level); 917349cc55cSDimitry Andric 91806c3fb27SDimitry Andric // Add the core function simplification pipeline nested inside the 919349cc55cSDimitry Andric // CGSCC walk. 920349cc55cSDimitry Andric MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 921349cc55cSDimitry Andric buildFunctionSimplificationPipeline(Level, Phase), 92206c3fb27SDimitry Andric PTO.EagerlyInvalidateAnalyses, /*NoRerun=*/true)); 92306c3fb27SDimitry Andric 92406c3fb27SDimitry Andric // Finally, deduce any function attributes based on the fully simplified 92506c3fb27SDimitry Andric // function. 92606c3fb27SDimitry Andric MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); 92706c3fb27SDimitry Andric 92806c3fb27SDimitry Andric // Mark that the function is fully simplified and that it shouldn't be 92906c3fb27SDimitry Andric // simplified again if we somehow revisit it due to CGSCC mutations unless 93006c3fb27SDimitry Andric // it's been modified since. 93106c3fb27SDimitry Andric MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 93206c3fb27SDimitry Andric RequireAnalysisPass<ShouldNotRunFunctionPassesAnalysis, Function>())); 933349cc55cSDimitry Andric 934349cc55cSDimitry Andric MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0)); 935349cc55cSDimitry Andric 93606c3fb27SDimitry Andric // Make sure we don't affect potential future NoRerun CGSCC adaptors. 937349cc55cSDimitry Andric MIWP.addLateModulePass(createModuleToFunctionPassAdaptor( 938349cc55cSDimitry Andric InvalidateAnalysisPass<ShouldNotRunFunctionPassesAnalysis>())); 939349cc55cSDimitry Andric 940349cc55cSDimitry Andric return MIWP; 941349cc55cSDimitry Andric } 942349cc55cSDimitry Andric 9430eae32dcSDimitry Andric ModulePassManager 944349cc55cSDimitry Andric PassBuilder::buildModuleInlinerPipeline(OptimizationLevel Level, 945349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 9460eae32dcSDimitry Andric ModulePassManager MPM; 9470eae32dcSDimitry Andric 948349cc55cSDimitry Andric InlineParams IP = getInlineParamsFromOptLevel(Level); 94981ad6265SDimitry Andric // For PreLinkThinLTO + SamplePGO, set hot-caller threshold to 0 to 95081ad6265SDimitry Andric // disable hot callsite inline (as much as possible [1]) because it makes 95181ad6265SDimitry Andric // profile annotation in the backend inaccurate. 95281ad6265SDimitry Andric // 95381ad6265SDimitry Andric // [1] Note the cost of a function could be below zero due to erased 95481ad6265SDimitry Andric // prologue / epilogue. 955349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 956349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 957349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 958349cc55cSDimitry Andric 959349cc55cSDimitry Andric if (PGOOpt) 960349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 961349cc55cSDimitry Andric 962349cc55cSDimitry Andric // The inline deferral logic is used to avoid losing some 963349cc55cSDimitry Andric // inlining chance in future. It is helpful in SCC inliner, in which 964349cc55cSDimitry Andric // inlining is processed in bottom-up order. 965349cc55cSDimitry Andric // While in module inliner, the inlining order is a priority-based order 966349cc55cSDimitry Andric // by default. The inline deferral is unnecessary there. So we disable the 967349cc55cSDimitry Andric // inline deferral logic in module inliner. 968349cc55cSDimitry Andric IP.EnableDeferral = false; 969349cc55cSDimitry Andric 97081ad6265SDimitry Andric MPM.addPass(ModuleInlinerPass(IP, UseInlineAdvisor, Phase)); 9710eae32dcSDimitry Andric 9720eae32dcSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 9730eae32dcSDimitry Andric buildFunctionSimplificationPipeline(Level, Phase), 9740eae32dcSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 9750eae32dcSDimitry Andric 9760eae32dcSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( 9770eae32dcSDimitry Andric CoroSplitPass(Level != OptimizationLevel::O0))); 9780eae32dcSDimitry Andric 9790eae32dcSDimitry Andric return MPM; 980349cc55cSDimitry Andric } 981349cc55cSDimitry Andric 982349cc55cSDimitry Andric ModulePassManager 983349cc55cSDimitry Andric PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, 984349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 98506c3fb27SDimitry Andric assert(Level != OptimizationLevel::O0 && 98606c3fb27SDimitry Andric "Should not be used for O0 pipeline"); 98706c3fb27SDimitry Andric 98806c3fb27SDimitry Andric assert(Phase != ThinOrFullLTOPhase::FullLTOPostLink && 98906c3fb27SDimitry Andric "FullLTOPostLink shouldn't call buildModuleSimplificationPipeline!"); 99006c3fb27SDimitry Andric 991349cc55cSDimitry Andric ModulePassManager MPM; 992349cc55cSDimitry Andric 993349cc55cSDimitry Andric // Place pseudo probe instrumentation as the first pass of the pipeline to 994349cc55cSDimitry Andric // minimize the impact of optimization changes. 995349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 996349cc55cSDimitry Andric Phase != ThinOrFullLTOPhase::ThinLTOPostLink) 997349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 998349cc55cSDimitry Andric 999349cc55cSDimitry Andric bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse); 1000349cc55cSDimitry Andric 1001349cc55cSDimitry Andric // In ThinLTO mode, when flattened profile is used, all the available 1002349cc55cSDimitry Andric // profile information will be annotated in PreLink phase so there is 1003349cc55cSDimitry Andric // no need to load the profile again in PostLink. 1004349cc55cSDimitry Andric bool LoadSampleProfile = 1005349cc55cSDimitry Andric HasSampleProfile && 1006349cc55cSDimitry Andric !(FlattenedProfileUsed && Phase == ThinOrFullLTOPhase::ThinLTOPostLink); 1007349cc55cSDimitry Andric 1008349cc55cSDimitry Andric // During the ThinLTO backend phase we perform early indirect call promotion 1009349cc55cSDimitry Andric // here, before globalopt. Otherwise imported available_externally functions 1010349cc55cSDimitry Andric // look unreferenced and are removed. If we are going to load the sample 1011349cc55cSDimitry Andric // profile then defer until later. 1012349cc55cSDimitry Andric // TODO: See if we can move later and consolidate with the location where 1013349cc55cSDimitry Andric // we perform ICP when we are loading a sample profile. 1014349cc55cSDimitry Andric // TODO: We pass HasSampleProfile (whether there was a sample profile file 1015349cc55cSDimitry Andric // passed to the compile) to the SamplePGO flag of ICP. This is used to 1016349cc55cSDimitry Andric // determine whether the new direct calls are annotated with prof metadata. 1017349cc55cSDimitry Andric // Ideally this should be determined from whether the IR is annotated with 1018349cc55cSDimitry Andric // sample profile, and not whether the a sample profile was provided on the 1019349cc55cSDimitry Andric // command line. E.g. for flattened profiles where we will not be reloading 1020349cc55cSDimitry Andric // the sample profile in the ThinLTO backend, we ideally shouldn't have to 1021349cc55cSDimitry Andric // provide the sample profile file. 1022349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink && !LoadSampleProfile) 1023349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile)); 1024349cc55cSDimitry Andric 102506c3fb27SDimitry Andric // Create an early function pass manager to cleanup the output of the 102606c3fb27SDimitry Andric // frontend. Not necessary with LTO post link pipelines since the pre link 102706c3fb27SDimitry Andric // pipeline already cleaned up the frontend output. 102806c3fb27SDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPostLink) { 1029349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 1030349cc55cSDimitry Andric // libraries and other oracles. 1031349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 103281ad6265SDimitry Andric MPM.addPass(CoroEarlyPass()); 1033349cc55cSDimitry Andric 1034349cc55cSDimitry Andric FunctionPassManager EarlyFPM; 1035349cc55cSDimitry Andric // Lower llvm.expect to metadata before attempting transforms. 103606c3fb27SDimitry Andric // Compare/branch metadata may alter the behavior of passes like 103706c3fb27SDimitry Andric // SimplifyCFG. 1038349cc55cSDimitry Andric EarlyFPM.addPass(LowerExpectIntrinsicPass()); 1039349cc55cSDimitry Andric EarlyFPM.addPass(SimplifyCFGPass()); 1040bdd1243dSDimitry Andric EarlyFPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 1041349cc55cSDimitry Andric EarlyFPM.addPass(EarlyCSEPass()); 1042349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 1043349cc55cSDimitry Andric EarlyFPM.addPass(CallSiteSplittingPass()); 104406c3fb27SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 104506c3fb27SDimitry Andric std::move(EarlyFPM), PTO.EagerlyInvalidateAnalyses)); 104606c3fb27SDimitry Andric } 1047349cc55cSDimitry Andric 1048349cc55cSDimitry Andric if (LoadSampleProfile) { 1049349cc55cSDimitry Andric // Annotate sample profile right after early FPM to ensure freshness of 1050349cc55cSDimitry Andric // the debug info. 1051349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1052349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, Phase)); 1053349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1054349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 1055349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1056349cc55cSDimitry Andric // Do not invoke ICP in the LTOPrelink phase as it makes it hard 1057349cc55cSDimitry Andric // for the profile annotation to be accurate in the LTO backend. 105806c3fb27SDimitry Andric if (!isLTOPreLink(Phase)) 1059349cc55cSDimitry Andric // We perform early indirect call promotion here, before globalopt. 1060349cc55cSDimitry Andric // This is important for the ThinLTO backend phase because otherwise 1061349cc55cSDimitry Andric // imported available_externally functions look unreferenced and are 1062349cc55cSDimitry Andric // removed. 1063349cc55cSDimitry Andric MPM.addPass( 1064349cc55cSDimitry Andric PGOIndirectCallPromotion(true /* IsInLTO */, true /* SamplePGO */)); 1065349cc55cSDimitry Andric } 1066349cc55cSDimitry Andric 1067349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations on the module. This is a 1068349cc55cSDimitry Andric // (quick!) no-op if there are no OpenMP runtime calls present in the module. 1069349cc55cSDimitry Andric MPM.addPass(OpenMPOptPass()); 1070349cc55cSDimitry Andric 1071349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::MODULE) 1072349cc55cSDimitry Andric MPM.addPass(AttributorPass()); 1073349cc55cSDimitry Andric 1074349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic in the ThinLTO 1075349cc55cSDimitry Andric // post link pipeline after ICP. This is to enable usage of the type 1076349cc55cSDimitry Andric // tests in ICP sequences. 1077349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink) 1078349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1079349cc55cSDimitry Andric 108006c3fb27SDimitry Andric invokePipelineEarlySimplificationEPCallbacks(MPM, Level); 1081349cc55cSDimitry Andric 1082349cc55cSDimitry Andric // Interprocedural constant propagation now that basic cleanup has occurred 1083349cc55cSDimitry Andric // and prior to optimizing globals. 1084349cc55cSDimitry Andric // FIXME: This position in the pipeline hasn't been carefully considered in 1085349cc55cSDimitry Andric // years, it should be re-analyzed. 108606c3fb27SDimitry Andric MPM.addPass(IPSCCPPass( 108706c3fb27SDimitry Andric IPSCCPOptions(/*AllowFuncSpec=*/ 1088bdd1243dSDimitry Andric Level != OptimizationLevel::Os && 108906c3fb27SDimitry Andric Level != OptimizationLevel::Oz && 109006c3fb27SDimitry Andric !isLTOPreLink(Phase)))); 1091349cc55cSDimitry Andric 1092349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 1093349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 1094349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 1095349cc55cSDimitry Andric 1096349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 1097349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1098349cc55cSDimitry Andric 1099349cc55cSDimitry Andric // Create a small function pass pipeline to cleanup after all the global 1100349cc55cSDimitry Andric // optimizations. 1101349cc55cSDimitry Andric FunctionPassManager GlobalCleanupPM; 110206c3fb27SDimitry Andric // FIXME: Should this instead by a run of SROA? 110306c3fb27SDimitry Andric GlobalCleanupPM.addPass(PromotePass()); 1104349cc55cSDimitry Andric GlobalCleanupPM.addPass(InstCombinePass()); 1105349cc55cSDimitry Andric invokePeepholeEPCallbacks(GlobalCleanupPM, Level); 1106fb03ea46SDimitry Andric GlobalCleanupPM.addPass( 1107fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 1108349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM), 1109349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1110349cc55cSDimitry Andric 1111*5f757f3fSDimitry Andric // Invoke the pre-inliner passes for instrumentation PGO or MemProf. 1112*5f757f3fSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1113*5f757f3fSDimitry Andric (PGOOpt->Action == PGOOptions::IRInstr || 1114*5f757f3fSDimitry Andric PGOOpt->Action == PGOOptions::IRUse || !PGOOpt->MemoryProfile.empty())) 1115*5f757f3fSDimitry Andric addPreInlinerPasses(MPM, Level, Phase); 1116*5f757f3fSDimitry Andric 1117349cc55cSDimitry Andric // Add all the requested passes for instrumentation PGO, if requested. 1118349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1119349cc55cSDimitry Andric (PGOOpt->Action == PGOOptions::IRInstr || 1120349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) { 1121349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, 1122*5f757f3fSDimitry Andric /*RunProfileGen=*/PGOOpt->Action == PGOOptions::IRInstr, 1123*5f757f3fSDimitry Andric /*IsCS=*/false, PGOOpt->AtomicCounterUpdate, 1124*5f757f3fSDimitry Andric PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile, 1125*5f757f3fSDimitry Andric PGOOpt->FS); 1126349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(false, false)); 1127349cc55cSDimitry Andric } 1128349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 1129349cc55cSDimitry Andric PGOOpt->CSAction == PGOOptions::CSIRInstr) 1130349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); 1131349cc55cSDimitry Andric 113206c3fb27SDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 113306c3fb27SDimitry Andric !PGOOpt->MemoryProfile.empty()) 113406c3fb27SDimitry Andric MPM.addPass(MemProfUsePass(PGOOpt->MemoryProfile, PGOOpt->FS)); 113506c3fb27SDimitry Andric 1136349cc55cSDimitry Andric // Synthesize function entry counts for non-PGO compilation. 1137349cc55cSDimitry Andric if (EnableSyntheticCounts && !PGOOpt) 1138349cc55cSDimitry Andric MPM.addPass(SyntheticCountsPropagation()); 1139349cc55cSDimitry Andric 1140*5f757f3fSDimitry Andric MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/true)); 1141*5f757f3fSDimitry Andric 1142349cc55cSDimitry Andric if (EnableModuleInliner) 1143349cc55cSDimitry Andric MPM.addPass(buildModuleInlinerPipeline(Level, Phase)); 1144349cc55cSDimitry Andric else 1145349cc55cSDimitry Andric MPM.addPass(buildInlinerPipeline(Level, Phase)); 1146349cc55cSDimitry Andric 1147bdd1243dSDimitry Andric // Remove any dead arguments exposed by cleanups, constant folding globals, 1148bdd1243dSDimitry Andric // and argument promotion. 1149bdd1243dSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 1150bdd1243dSDimitry Andric 115181ad6265SDimitry Andric MPM.addPass(CoroCleanupPass()); 115281ad6265SDimitry Andric 115306c3fb27SDimitry Andric // Optimize globals now that functions are fully simplified. 115406c3fb27SDimitry Andric MPM.addPass(GlobalOptPass()); 115506c3fb27SDimitry Andric MPM.addPass(GlobalDCEPass()); 1156349cc55cSDimitry Andric 1157349cc55cSDimitry Andric return MPM; 1158349cc55cSDimitry Andric } 1159349cc55cSDimitry Andric 1160349cc55cSDimitry Andric /// TODO: Should LTO cause any differences to this set of passes? 1161349cc55cSDimitry Andric void PassBuilder::addVectorPasses(OptimizationLevel Level, 1162349cc55cSDimitry Andric FunctionPassManager &FPM, bool IsFullLTO) { 1163349cc55cSDimitry Andric FPM.addPass(LoopVectorizePass( 1164349cc55cSDimitry Andric LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization))); 1165349cc55cSDimitry Andric 1166*5f757f3fSDimitry Andric if (EnableInferAlignmentPass) 1167*5f757f3fSDimitry Andric FPM.addPass(InferAlignmentPass()); 1168349cc55cSDimitry Andric if (IsFullLTO) { 1169349cc55cSDimitry Andric // The vectorizer may have significantly shortened a loop body; unroll 1170349cc55cSDimitry Andric // again. Unroll small loops to hide loop backedge latency and saturate any 1171349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 1172349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 1173349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 1174349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 1175349cc55cSDimitry Andric // across the loop nests. 1176349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1177349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) 1178349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1179349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1180349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1181349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1182349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 1183349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 1184bdd1243dSDimitry Andric // Now that we are done with loop unrolling, be it either by LoopVectorizer, 1185bdd1243dSDimitry Andric // or LoopUnroll passes, some variable-offset GEP's into alloca's could have 1186bdd1243dSDimitry Andric // become constant-offset, thus enabling SROA and alloca promotion. Do so. 1187bdd1243dSDimitry Andric // NOTE: we are very late in the pipeline, and we don't have any LICM 1188bdd1243dSDimitry Andric // or SimplifyCFG passes scheduled after us, that would cleanup 1189bdd1243dSDimitry Andric // the CFG mess this may created if allowed to modify CFG, so forbid that. 1190bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::PreserveCFG)); 1191349cc55cSDimitry Andric } 1192349cc55cSDimitry Andric 1193349cc55cSDimitry Andric if (!IsFullLTO) { 1194349cc55cSDimitry Andric // Eliminate loads by forwarding stores from the previous iteration to loads 1195349cc55cSDimitry Andric // of the current iteration. 1196349cc55cSDimitry Andric FPM.addPass(LoopLoadEliminationPass()); 1197349cc55cSDimitry Andric } 1198349cc55cSDimitry Andric // Cleanup after the loop optimization passes. 1199349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1200349cc55cSDimitry Andric 1201349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 12020eae32dcSDimitry Andric ExtraVectorPassManager ExtraPasses; 1203349cc55cSDimitry Andric // At higher optimization levels, try to clean up any runtime overlap and 1204349cc55cSDimitry Andric // alignment checks inserted by the vectorizer. We want to track correlated 1205349cc55cSDimitry Andric // runtime checks for two inner loops in the same outer loop, fold any 1206349cc55cSDimitry Andric // common computations, hoist loop-invariant aspects out of any outer loop, 1207349cc55cSDimitry Andric // and unswitch the runtime checks if possible. Once hoisted, we may have 1208349cc55cSDimitry Andric // dead (or speculatable) control flows or more combining opportunities. 12090eae32dcSDimitry Andric ExtraPasses.addPass(EarlyCSEPass()); 12100eae32dcSDimitry Andric ExtraPasses.addPass(CorrelatedValuePropagationPass()); 12110eae32dcSDimitry Andric ExtraPasses.addPass(InstCombinePass()); 1212349cc55cSDimitry Andric LoopPassManager LPM; 1213fb03ea46SDimitry Andric LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1214fb03ea46SDimitry Andric /*AllowSpeculation=*/true)); 1215349cc55cSDimitry Andric LPM.addPass(SimpleLoopUnswitchPass(/* NonTrivial */ Level == 1216349cc55cSDimitry Andric OptimizationLevel::O3)); 12170eae32dcSDimitry Andric ExtraPasses.addPass( 1218349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM), /*UseMemorySSA=*/true, 1219349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 1220fb03ea46SDimitry Andric ExtraPasses.addPass( 1221fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 12220eae32dcSDimitry Andric ExtraPasses.addPass(InstCombinePass()); 12230eae32dcSDimitry Andric FPM.addPass(std::move(ExtraPasses)); 1224349cc55cSDimitry Andric } 1225349cc55cSDimitry Andric 1226349cc55cSDimitry Andric // Now that we've formed fast to execute loop structures, we do further 1227349cc55cSDimitry Andric // optimizations. These are run afterward as they might block doing complex 1228349cc55cSDimitry Andric // analyses and transforms such as what are needed for loop vectorization. 1229349cc55cSDimitry Andric 1230349cc55cSDimitry Andric // Cleanup after loop vectorization, etc. Simplification passes like CVP and 1231349cc55cSDimitry Andric // GVN, loop transforms, and others have already run, so it's now better to 1232349cc55cSDimitry Andric // convert to more optimized IR using more aggressive simplify CFG options. 1233349cc55cSDimitry Andric // The extra sinking transform can create larger basic blocks, so do this 1234349cc55cSDimitry Andric // before SLP vectorization. 1235349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 1236349cc55cSDimitry Andric .forwardSwitchCondToPhi(true) 1237fb03ea46SDimitry Andric .convertSwitchRangeToICmp(true) 1238349cc55cSDimitry Andric .convertSwitchToLookupTable(true) 1239349cc55cSDimitry Andric .needCanonicalLoops(false) 1240349cc55cSDimitry Andric .hoistCommonInsts(true) 1241349cc55cSDimitry Andric .sinkCommonInsts(true))); 1242349cc55cSDimitry Andric 1243349cc55cSDimitry Andric if (IsFullLTO) { 1244349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 1245349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1246349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 1247349cc55cSDimitry Andric } 1248349cc55cSDimitry Andric 1249349cc55cSDimitry Andric // Optimize parallel scalar instruction chains into SIMD instructions. 1250349cc55cSDimitry Andric if (PTO.SLPVectorization) { 1251349cc55cSDimitry Andric FPM.addPass(SLPVectorizerPass()); 1252349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 1253349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); 1254349cc55cSDimitry Andric } 1255349cc55cSDimitry Andric } 1256349cc55cSDimitry Andric // Enhance/cleanup vector code. 1257349cc55cSDimitry Andric FPM.addPass(VectorCombinePass()); 1258349cc55cSDimitry Andric 1259349cc55cSDimitry Andric if (!IsFullLTO) { 1260349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1261349cc55cSDimitry Andric // Unroll small loops to hide loop backedge latency and saturate any 1262349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 1263349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 1264349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 1265349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 1266349cc55cSDimitry Andric // across the loop nests. 1267349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1268349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) { 1269349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1270349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1271349cc55cSDimitry Andric } 1272349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1273349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1274349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 1275349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 1276bdd1243dSDimitry Andric // Now that we are done with loop unrolling, be it either by LoopVectorizer, 1277bdd1243dSDimitry Andric // or LoopUnroll passes, some variable-offset GEP's into alloca's could have 1278bdd1243dSDimitry Andric // become constant-offset, thus enabling SROA and alloca promotion. Do so. 1279bdd1243dSDimitry Andric // NOTE: we are very late in the pipeline, and we don't have any LICM 1280bdd1243dSDimitry Andric // or SimplifyCFG passes scheduled after us, that would cleanup 1281bdd1243dSDimitry Andric // the CFG mess this may created if allowed to modify CFG, so forbid that. 1282bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::PreserveCFG)); 128306c3fb27SDimitry Andric } 128406c3fb27SDimitry Andric 1285*5f757f3fSDimitry Andric if (EnableInferAlignmentPass) 1286*5f757f3fSDimitry Andric FPM.addPass(InferAlignmentPass()); 1287349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 128806c3fb27SDimitry Andric 128906c3fb27SDimitry Andric // This is needed for two reasons: 129006c3fb27SDimitry Andric // 1. It works around problems that instcombine introduces, such as sinking 129106c3fb27SDimitry Andric // expensive FP divides into loops containing multiplications using the 129206c3fb27SDimitry Andric // divide result. 129306c3fb27SDimitry Andric // 2. It helps to clean up some loop-invariant code created by the loop 129406c3fb27SDimitry Andric // unroll pass when IsFullLTO=false. 1295349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1296fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1297fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 129806c3fb27SDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/false)); 1299349cc55cSDimitry Andric 1300349cc55cSDimitry Andric // Now that we've vectorized and unrolled loops, we may have more refined 1301349cc55cSDimitry Andric // alignment information, try to re-derive it here. 1302349cc55cSDimitry Andric FPM.addPass(AlignmentFromAssumptionsPass()); 1303349cc55cSDimitry Andric } 1304349cc55cSDimitry Andric 1305349cc55cSDimitry Andric ModulePassManager 1306349cc55cSDimitry Andric PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level, 130781ad6265SDimitry Andric ThinOrFullLTOPhase LTOPhase) { 130806c3fb27SDimitry Andric const bool LTOPreLink = isLTOPreLink(LTOPhase); 1309349cc55cSDimitry Andric ModulePassManager MPM; 1310349cc55cSDimitry Andric 1311349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1312349cc55cSDimitry Andric // large bodies. 1313349cc55cSDimitry Andric if (RunPartialInlining) 1314349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1315349cc55cSDimitry Andric 1316349cc55cSDimitry Andric // Remove avail extern fns and globals definitions since we aren't compiling 1317349cc55cSDimitry Andric // an object file for later LTO. For LTO we want to preserve these so they 1318349cc55cSDimitry Andric // are eligible for inlining at link-time. Note if they are unreferenced they 1319349cc55cSDimitry Andric // will be removed by GlobalDCE later, so this only impacts referenced 1320349cc55cSDimitry Andric // available externally globals. Eventually they will be suppressed during 1321349cc55cSDimitry Andric // codegen, but eliminating here enables more opportunity for GlobalDCE as it 1322349cc55cSDimitry Andric // may make globals referenced by available external functions dead and saves 1323349cc55cSDimitry Andric // running remaining passes on the eliminated functions. These should be 1324349cc55cSDimitry Andric // preserved during prelinking for link-time inlining decisions. 1325349cc55cSDimitry Andric if (!LTOPreLink) 1326349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1327349cc55cSDimitry Andric 1328349cc55cSDimitry Andric if (EnableOrderFileInstrumentation) 1329349cc55cSDimitry Andric MPM.addPass(InstrOrderFilePass()); 1330349cc55cSDimitry Andric 1331349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1332349cc55cSDimitry Andric // attributes where applicable. 1333349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1334349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1335349cc55cSDimitry Andric 1336349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1337349cc55cSDimitry Andric // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as 1338349cc55cSDimitry Andric // cross-module inline has not been done yet. The context sensitive 1339349cc55cSDimitry Andric // instrumentation is after all the inlines are done. 1340349cc55cSDimitry Andric if (!LTOPreLink && PGOOpt) { 1341349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1342*5f757f3fSDimitry Andric addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/true, 1343*5f757f3fSDimitry Andric /*IsCS=*/true, PGOOpt->AtomicCounterUpdate, 1344*5f757f3fSDimitry Andric PGOOpt->CSProfileGenFile, PGOOpt->ProfileRemappingFile, 1345*5f757f3fSDimitry Andric PGOOpt->FS); 1346349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1347*5f757f3fSDimitry Andric addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/false, 1348*5f757f3fSDimitry Andric /*IsCS=*/true, PGOOpt->AtomicCounterUpdate, 1349*5f757f3fSDimitry Andric PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile, 1350*5f757f3fSDimitry Andric PGOOpt->FS); 1351349cc55cSDimitry Andric } 1352349cc55cSDimitry Andric 135381ad6265SDimitry Andric // Re-compute GlobalsAA here prior to function passes. This is particularly 1354349cc55cSDimitry Andric // useful as the above will have inlined, DCE'ed, and function-attr 1355349cc55cSDimitry Andric // propagated everything. We should at this point have a reasonably minimal 1356349cc55cSDimitry Andric // and richly annotated call graph. By computing aliasing and mod/ref 1357349cc55cSDimitry Andric // information for all local globals here, the late loop passes and notably 1358349cc55cSDimitry Andric // the vectorizer will be able to use them to help recognize vectorizable 1359349cc55cSDimitry Andric // memory operations. 1360*5f757f3fSDimitry Andric if (EnableGlobalAnalyses) 136181ad6265SDimitry Andric MPM.addPass(RecomputeGlobalsAAPass()); 136281ad6265SDimitry Andric 136306c3fb27SDimitry Andric invokeOptimizerEarlyEPCallbacks(MPM, Level); 1364349cc55cSDimitry Andric 1365349cc55cSDimitry Andric FunctionPassManager OptimizePM; 1366*5f757f3fSDimitry Andric // Scheduling LoopVersioningLICM when inlining is over, because after that 1367*5f757f3fSDimitry Andric // we may see more accurate aliasing. Reason to run this late is that too 1368*5f757f3fSDimitry Andric // early versioning may prevent further inlining due to increase of code 1369*5f757f3fSDimitry Andric // size. Other optimizations which runs later might get benefit of no-alias 1370*5f757f3fSDimitry Andric // assumption in clone loop. 1371*5f757f3fSDimitry Andric if (UseLoopVersioningLICM) { 1372*5f757f3fSDimitry Andric OptimizePM.addPass( 1373*5f757f3fSDimitry Andric createFunctionToLoopPassAdaptor(LoopVersioningLICMPass())); 1374*5f757f3fSDimitry Andric // LoopVersioningLICM pass might increase new LICM opportunities. 1375*5f757f3fSDimitry Andric OptimizePM.addPass(createFunctionToLoopPassAdaptor( 1376*5f757f3fSDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1377*5f757f3fSDimitry Andric /*AllowSpeculation=*/true), 1378*5f757f3fSDimitry Andric /*USeMemorySSA=*/true, /*UseBlockFrequencyInfo=*/false)); 1379*5f757f3fSDimitry Andric } 1380*5f757f3fSDimitry Andric 1381349cc55cSDimitry Andric OptimizePM.addPass(Float2IntPass()); 1382349cc55cSDimitry Andric OptimizePM.addPass(LowerConstantIntrinsicsPass()); 1383349cc55cSDimitry Andric 1384349cc55cSDimitry Andric if (EnableMatrix) { 1385349cc55cSDimitry Andric OptimizePM.addPass(LowerMatrixIntrinsicsPass()); 1386349cc55cSDimitry Andric OptimizePM.addPass(EarlyCSEPass()); 1387349cc55cSDimitry Andric } 1388349cc55cSDimitry Andric 138906c3fb27SDimitry Andric // CHR pass should only be applied with the profile information. 139006c3fb27SDimitry Andric // The check is to check the profile summary information in CHR. 139106c3fb27SDimitry Andric if (EnableCHR && Level == OptimizationLevel::O3) 139206c3fb27SDimitry Andric OptimizePM.addPass(ControlHeightReductionPass()); 139306c3fb27SDimitry Andric 1394349cc55cSDimitry Andric // FIXME: We need to run some loop optimizations to re-rotate loops after 1395349cc55cSDimitry Andric // simplifycfg and others undo their rotation. 1396349cc55cSDimitry Andric 1397349cc55cSDimitry Andric // Optimize the loop execution. These passes operate on entire loop nests 1398349cc55cSDimitry Andric // rather than on each loop in an inside-out manner, and so they are actually 1399349cc55cSDimitry Andric // function passes. 1400349cc55cSDimitry Andric 140106c3fb27SDimitry Andric invokeVectorizerStartEPCallbacks(OptimizePM, Level); 1402349cc55cSDimitry Andric 1403349cc55cSDimitry Andric LoopPassManager LPM; 1404349cc55cSDimitry Andric // First rotate loops that may have been un-rotated by prior passes. 1405349cc55cSDimitry Andric // Disable header duplication at -Oz. 1406349cc55cSDimitry Andric LPM.addPass(LoopRotatePass(Level != OptimizationLevel::Oz, LTOPreLink)); 1407349cc55cSDimitry Andric // Some loops may have become dead by now. Try to delete them. 14080eae32dcSDimitry Andric // FIXME: see discussion in https://reviews.llvm.org/D112851, 14090eae32dcSDimitry Andric // this may need to be revisited once we run GVN before loop deletion 14100eae32dcSDimitry Andric // in the simplification pipeline. 1411349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1412349cc55cSDimitry Andric OptimizePM.addPass(createFunctionToLoopPassAdaptor( 1413349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/false)); 1414349cc55cSDimitry Andric 1415349cc55cSDimitry Andric // Distribute loops to allow partial vectorization. I.e. isolate dependences 1416349cc55cSDimitry Andric // into separate loop that would otherwise inhibit vectorization. This is 1417349cc55cSDimitry Andric // currently only performed for loops marked with the metadata 1418349cc55cSDimitry Andric // llvm.loop.distribute=true or when -enable-loop-distribute is specified. 1419349cc55cSDimitry Andric OptimizePM.addPass(LoopDistributePass()); 1420349cc55cSDimitry Andric 1421349cc55cSDimitry Andric // Populates the VFABI attribute with the scalar-to-vector mappings 1422349cc55cSDimitry Andric // from the TargetLibraryInfo. 1423349cc55cSDimitry Andric OptimizePM.addPass(InjectTLIMappings()); 1424349cc55cSDimitry Andric 1425349cc55cSDimitry Andric addVectorPasses(Level, OptimizePM, /* IsFullLTO */ false); 1426349cc55cSDimitry Andric 1427349cc55cSDimitry Andric // LoopSink pass sinks instructions hoisted by LICM, which serves as a 1428349cc55cSDimitry Andric // canonicalization pass that enables other optimizations. As a result, 1429349cc55cSDimitry Andric // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 1430349cc55cSDimitry Andric // result too early. 1431349cc55cSDimitry Andric OptimizePM.addPass(LoopSinkPass()); 1432349cc55cSDimitry Andric 1433349cc55cSDimitry Andric // And finally clean up LCSSA form before generating code. 1434349cc55cSDimitry Andric OptimizePM.addPass(InstSimplifyPass()); 1435349cc55cSDimitry Andric 1436349cc55cSDimitry Andric // This hoists/decomposes div/rem ops. It should run after other sink/hoist 1437349cc55cSDimitry Andric // passes to avoid re-sinking, but before SimplifyCFG because it can allow 1438349cc55cSDimitry Andric // flattening of blocks. 1439349cc55cSDimitry Andric OptimizePM.addPass(DivRemPairsPass()); 1440349cc55cSDimitry Andric 1441972a253aSDimitry Andric // Try to annotate calls that were created during optimization. 1442972a253aSDimitry Andric OptimizePM.addPass(TailCallElimPass()); 1443972a253aSDimitry Andric 1444349cc55cSDimitry Andric // LoopSink (and other loop passes since the last simplifyCFG) might have 1445349cc55cSDimitry Andric // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. 1446fb03ea46SDimitry Andric OptimizePM.addPass( 1447fb03ea46SDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true))); 1448349cc55cSDimitry Andric 1449349cc55cSDimitry Andric // Add the core optimizing pipeline. 1450349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM), 1451349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1452349cc55cSDimitry Andric 145306c3fb27SDimitry Andric invokeOptimizerLastEPCallbacks(MPM, Level); 1454349cc55cSDimitry Andric 14550eae32dcSDimitry Andric // Split out cold code. Splitting is done late to avoid hiding context from 14560eae32dcSDimitry Andric // other optimizations and inadvertently regressing performance. The tradeoff 14570eae32dcSDimitry Andric // is that this has a higher code size cost than splitting early. 14580eae32dcSDimitry Andric if (EnableHotColdSplit && !LTOPreLink) 14590eae32dcSDimitry Andric MPM.addPass(HotColdSplittingPass()); 14600eae32dcSDimitry Andric 14610eae32dcSDimitry Andric // Search the code for similar regions of code. If enough similar regions can 14620eae32dcSDimitry Andric // be found where extracting the regions into their own function will decrease 14630eae32dcSDimitry Andric // the size of the program, we extract the regions, a deduplicate the 14640eae32dcSDimitry Andric // structurally similar regions. 14650eae32dcSDimitry Andric if (EnableIROutliner) 14660eae32dcSDimitry Andric MPM.addPass(IROutlinerPass()); 14670eae32dcSDimitry Andric 14680eae32dcSDimitry Andric // Merge functions if requested. 14690eae32dcSDimitry Andric if (PTO.MergeFunctions) 14700eae32dcSDimitry Andric MPM.addPass(MergeFunctionsPass()); 14710eae32dcSDimitry Andric 1472349cc55cSDimitry Andric // Now we need to do some global optimization transforms. 1473349cc55cSDimitry Andric // FIXME: It would seem like these should come first in the optimization 1474349cc55cSDimitry Andric // pipeline and maybe be the bottom of the canonicalization pipeline? Weird 1475349cc55cSDimitry Andric // ordering here. 1476349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1477349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1478349cc55cSDimitry Andric 147981ad6265SDimitry Andric if (PTO.CallGraphProfile && !LTOPreLink) 148081ad6265SDimitry Andric MPM.addPass(CGProfilePass()); 148181ad6265SDimitry Andric 1482349cc55cSDimitry Andric // TODO: Relative look table converter pass caused an issue when full lto is 1483349cc55cSDimitry Andric // enabled. See https://reviews.llvm.org/D94355 for more details. 1484349cc55cSDimitry Andric // Until the issue fixed, disable this pass during pre-linking phase. 1485349cc55cSDimitry Andric if (!LTOPreLink) 1486349cc55cSDimitry Andric MPM.addPass(RelLookupTableConverterPass()); 1487349cc55cSDimitry Andric 1488349cc55cSDimitry Andric return MPM; 1489349cc55cSDimitry Andric } 1490349cc55cSDimitry Andric 1491349cc55cSDimitry Andric ModulePassManager 1492349cc55cSDimitry Andric PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, 1493349cc55cSDimitry Andric bool LTOPreLink) { 149406c3fb27SDimitry Andric if (Level == OptimizationLevel::O0) 149506c3fb27SDimitry Andric return buildO0DefaultPipeline(Level, LTOPreLink); 1496349cc55cSDimitry Andric 1497349cc55cSDimitry Andric ModulePassManager MPM; 1498349cc55cSDimitry Andric 1499349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1500349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1501349cc55cSDimitry Andric 1502349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1503349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1504349cc55cSDimitry Andric 1505349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1506349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1507349cc55cSDimitry Andric 150806c3fb27SDimitry Andric // Apply module pipeline start EP callback. 150906c3fb27SDimitry Andric invokePipelineStartEPCallbacks(MPM, Level); 151006c3fb27SDimitry Andric 151181ad6265SDimitry Andric const ThinOrFullLTOPhase LTOPhase = LTOPreLink 151281ad6265SDimitry Andric ? ThinOrFullLTOPhase::FullLTOPreLink 151381ad6265SDimitry Andric : ThinOrFullLTOPhase::None; 1514349cc55cSDimitry Andric // Add the core simplification pipeline. 151581ad6265SDimitry Andric MPM.addPass(buildModuleSimplificationPipeline(Level, LTOPhase)); 1516349cc55cSDimitry Andric 1517349cc55cSDimitry Andric // Now add the optimization pipeline. 151881ad6265SDimitry Andric MPM.addPass(buildModuleOptimizationPipeline(Level, LTOPhase)); 1519349cc55cSDimitry Andric 1520349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1521349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1522349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1523349cc55cSDimitry Andric 1524349cc55cSDimitry Andric // Emit annotation remarks. 1525349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1526349cc55cSDimitry Andric 1527349cc55cSDimitry Andric if (LTOPreLink) 1528349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 152906c3fb27SDimitry Andric return MPM; 153006c3fb27SDimitry Andric } 1531349cc55cSDimitry Andric 153206c3fb27SDimitry Andric ModulePassManager 1533*5f757f3fSDimitry Andric PassBuilder::buildFatLTODefaultPipeline(OptimizationLevel Level) { 153406c3fb27SDimitry Andric ModulePassManager MPM; 1535*5f757f3fSDimitry Andric // FatLTO always uses UnifiedLTO, so use the ThinLTOPreLink pipeline 1536*5f757f3fSDimitry Andric MPM.addPass(buildThinLTOPreLinkDefaultPipeline(Level)); 1537*5f757f3fSDimitry Andric MPM.addPass(EmbedBitcodePass()); 1538*5f757f3fSDimitry Andric 1539*5f757f3fSDimitry Andric // Use the ThinLTO post-link pipeline with sample profiling, other 1540*5f757f3fSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) 1541*5f757f3fSDimitry Andric MPM.addPass(buildThinLTODefaultPipeline(Level, /*ImportSummary=*/nullptr)); 1542*5f757f3fSDimitry Andric else { 1543*5f757f3fSDimitry Andric // otherwise, just use module optimization 1544*5f757f3fSDimitry Andric MPM.addPass( 1545*5f757f3fSDimitry Andric buildModuleOptimizationPipeline(Level, ThinOrFullLTOPhase::None)); 1546*5f757f3fSDimitry Andric // Emit annotation remarks. 1547*5f757f3fSDimitry Andric addAnnotationRemarksPass(MPM); 1548*5f757f3fSDimitry Andric } 1549349cc55cSDimitry Andric return MPM; 1550349cc55cSDimitry Andric } 1551349cc55cSDimitry Andric 1552349cc55cSDimitry Andric ModulePassManager 1553349cc55cSDimitry Andric PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 155406c3fb27SDimitry Andric if (Level == OptimizationLevel::O0) 155506c3fb27SDimitry Andric return buildO0DefaultPipeline(Level, /*LTOPreLink*/true); 1556349cc55cSDimitry Andric 1557349cc55cSDimitry Andric ModulePassManager MPM; 1558349cc55cSDimitry Andric 1559349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1560349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1561349cc55cSDimitry Andric 1562349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1563349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1564349cc55cSDimitry Andric 1565349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1566349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1567349cc55cSDimitry Andric 1568349cc55cSDimitry Andric // Apply module pipeline start EP callback. 156906c3fb27SDimitry Andric invokePipelineStartEPCallbacks(MPM, Level); 1570349cc55cSDimitry Andric 1571349cc55cSDimitry Andric // If we are planning to perform ThinLTO later, we don't bloat the code with 1572349cc55cSDimitry Andric // unrolling/vectorization/... now. Just simplify the module as much as we 1573349cc55cSDimitry Andric // can. 1574349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1575349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPreLink)); 1576349cc55cSDimitry Andric 1577349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1578349cc55cSDimitry Andric // large bodies. 1579349cc55cSDimitry Andric // FIXME: It isn't clear whether this is really the right place to run this 1580349cc55cSDimitry Andric // in ThinLTO. Because there is another canonicalization and simplification 1581349cc55cSDimitry Andric // phase that will run after the thin link, running this here ends up with 1582349cc55cSDimitry Andric // less information than will be available later and it may grow functions in 1583349cc55cSDimitry Andric // ways that aren't beneficial. 1584349cc55cSDimitry Andric if (RunPartialInlining) 1585349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1586349cc55cSDimitry Andric 1587349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1588349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1589349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1590349cc55cSDimitry Andric 1591bdd1243dSDimitry Andric // Handle Optimizer{Early,Last}EPCallbacks added by clang on PreLink. Actual 1592bdd1243dSDimitry Andric // optimization is going to be done in PostLink stage, but clang can't add 1593bdd1243dSDimitry Andric // callbacks there in case of in-process ThinLTO called by linker. 159406c3fb27SDimitry Andric invokeOptimizerEarlyEPCallbacks(MPM, Level); 159506c3fb27SDimitry Andric invokeOptimizerLastEPCallbacks(MPM, Level); 1596349cc55cSDimitry Andric 1597349cc55cSDimitry Andric // Emit annotation remarks. 1598349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1599349cc55cSDimitry Andric 1600349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1601349cc55cSDimitry Andric 1602349cc55cSDimitry Andric return MPM; 1603349cc55cSDimitry Andric } 1604349cc55cSDimitry Andric 1605349cc55cSDimitry Andric ModulePassManager PassBuilder::buildThinLTODefaultPipeline( 1606349cc55cSDimitry Andric OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary) { 1607349cc55cSDimitry Andric ModulePassManager MPM; 1608349cc55cSDimitry Andric 1609349cc55cSDimitry Andric if (ImportSummary) { 161006c3fb27SDimitry Andric // For ThinLTO we must apply the context disambiguation decisions early, to 161106c3fb27SDimitry Andric // ensure we can correctly match the callsites to summary data. 161206c3fb27SDimitry Andric if (EnableMemProfContextDisambiguation) 161306c3fb27SDimitry Andric MPM.addPass(MemProfContextDisambiguation(ImportSummary)); 161406c3fb27SDimitry Andric 1615349cc55cSDimitry Andric // These passes import type identifier resolutions for whole-program 1616349cc55cSDimitry Andric // devirtualization and CFI. They must run early because other passes may 1617349cc55cSDimitry Andric // disturb the specific instruction patterns that these passes look for, 1618349cc55cSDimitry Andric // creating dependencies on resolutions that may not appear in the summary. 1619349cc55cSDimitry Andric // 1620349cc55cSDimitry Andric // For example, GVN may transform the pattern assume(type.test) appearing in 1621349cc55cSDimitry Andric // two basic blocks into assume(phi(type.test, type.test)), which would 1622349cc55cSDimitry Andric // transform a dependency on a WPD resolution into a dependency on a type 1623349cc55cSDimitry Andric // identifier resolution for CFI. 1624349cc55cSDimitry Andric // 1625349cc55cSDimitry Andric // Also, WPD has access to more precise information than ICP and can 1626349cc55cSDimitry Andric // devirtualize more effectively, so it should operate on the IR first. 1627349cc55cSDimitry Andric // 1628349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1629349cc55cSDimitry Andric // metadata and intrinsics. 1630349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary)); 1631349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary)); 1632349cc55cSDimitry Andric } 1633349cc55cSDimitry Andric 1634349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1635349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1636349cc55cSDimitry Andric // in ICP. 1637349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1638349cc55cSDimitry Andric // Drop available_externally and unreferenced globals. This is necessary 1639349cc55cSDimitry Andric // with ThinLTO in order to avoid leaving undefined references to dead 1640349cc55cSDimitry Andric // globals in the object file. 1641349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1642349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1643349cc55cSDimitry Andric return MPM; 1644349cc55cSDimitry Andric } 1645349cc55cSDimitry Andric 1646349cc55cSDimitry Andric // Add the core simplification pipeline. 1647349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1648349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1649349cc55cSDimitry Andric 1650349cc55cSDimitry Andric // Now add the optimization pipeline. 165181ad6265SDimitry Andric MPM.addPass(buildModuleOptimizationPipeline( 165281ad6265SDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1653349cc55cSDimitry Andric 1654349cc55cSDimitry Andric // Emit annotation remarks. 1655349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1656349cc55cSDimitry Andric 1657349cc55cSDimitry Andric return MPM; 1658349cc55cSDimitry Andric } 1659349cc55cSDimitry Andric 1660349cc55cSDimitry Andric ModulePassManager 1661349cc55cSDimitry Andric PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1662349cc55cSDimitry Andric // FIXME: We should use a customized pre-link pipeline! 1663349cc55cSDimitry Andric return buildPerModuleDefaultPipeline(Level, 1664349cc55cSDimitry Andric /* LTOPreLink */ true); 1665349cc55cSDimitry Andric } 1666349cc55cSDimitry Andric 1667349cc55cSDimitry Andric ModulePassManager 1668349cc55cSDimitry Andric PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, 1669349cc55cSDimitry Andric ModuleSummaryIndex *ExportSummary) { 1670349cc55cSDimitry Andric ModulePassManager MPM; 1671349cc55cSDimitry Andric 167206c3fb27SDimitry Andric invokeFullLinkTimeOptimizationEarlyEPCallbacks(MPM, Level); 167381ad6265SDimitry Andric 1674349cc55cSDimitry Andric // Create a function that performs CFI checks for cross-DSO calls with targets 1675349cc55cSDimitry Andric // in the current module. 1676349cc55cSDimitry Andric MPM.addPass(CrossDSOCFIPass()); 1677349cc55cSDimitry Andric 1678349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1679349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1680349cc55cSDimitry Andric // metadata and intrinsics. 1681349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1682349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1683349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1684349cc55cSDimitry Andric // in ICP. 1685349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1686349cc55cSDimitry Andric 168706c3fb27SDimitry Andric invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level); 168881ad6265SDimitry Andric 1689349cc55cSDimitry Andric // Emit annotation remarks. 1690349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1691349cc55cSDimitry Andric 1692349cc55cSDimitry Andric return MPM; 1693349cc55cSDimitry Andric } 1694349cc55cSDimitry Andric 1695349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) { 1696349cc55cSDimitry Andric // Load sample profile before running the LTO optimization pipeline. 1697349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1698349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, 1699349cc55cSDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink)); 1700349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1701349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 1702349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1703349cc55cSDimitry Andric } 1704349cc55cSDimitry Andric 17051fd87a68SDimitry Andric // Try to run OpenMP optimizations, quick no-op if no OpenMP metadata present. 17061ac55f4cSDimitry Andric MPM.addPass(OpenMPOptPass(ThinOrFullLTOPhase::FullLTOPostLink)); 17071fd87a68SDimitry Andric 1708349cc55cSDimitry Andric // Remove unused virtual tables to improve the quality of code generated by 1709349cc55cSDimitry Andric // whole-program devirtualization and bitset lowering. 171006c3fb27SDimitry Andric MPM.addPass(GlobalDCEPass(/*InLTOPostLink=*/true)); 1711349cc55cSDimitry Andric 1712349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 1713349cc55cSDimitry Andric // libraries and other oracles. 1714349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 1715349cc55cSDimitry Andric 1716349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1) { 1717349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 171881ad6265SDimitry Andric CallSiteSplittingPass(), PTO.EagerlyInvalidateAnalyses)); 1719349cc55cSDimitry Andric 1720349cc55cSDimitry Andric // Indirect call promotion. This should promote all the targets that are 1721349cc55cSDimitry Andric // left by the earlier promotion pass that promotes intra-module targets. 1722349cc55cSDimitry Andric // This two-step promotion is to save the compile time. For LTO, it should 1723349cc55cSDimitry Andric // produce the same result as if we only do promotion here. 1724349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion( 1725349cc55cSDimitry Andric true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse)); 1726349cc55cSDimitry Andric 1727349cc55cSDimitry Andric // Propagate constants at call sites into the functions they call. This 1728349cc55cSDimitry Andric // opens opportunities for globalopt (and inlining) by substituting function 1729349cc55cSDimitry Andric // pointers passed as arguments to direct uses of functions. 1730bdd1243dSDimitry Andric MPM.addPass(IPSCCPPass(IPSCCPOptions(/*AllowFuncSpec=*/ 1731bdd1243dSDimitry Andric Level != OptimizationLevel::Os && 1732bdd1243dSDimitry Andric Level != OptimizationLevel::Oz))); 1733349cc55cSDimitry Andric 1734349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 1735349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 1736349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 1737349cc55cSDimitry Andric } 1738349cc55cSDimitry Andric 1739349cc55cSDimitry Andric // Now deduce any function attributes based in the current code. 1740349cc55cSDimitry Andric MPM.addPass( 1741349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1742349cc55cSDimitry Andric 1743349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1744349cc55cSDimitry Andric // attributes where applicable. 1745349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1746349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1747349cc55cSDimitry Andric 1748349cc55cSDimitry Andric // Use in-range annotations on GEP indices to split globals where beneficial. 1749349cc55cSDimitry Andric MPM.addPass(GlobalSplitPass()); 1750349cc55cSDimitry Andric 1751349cc55cSDimitry Andric // Run whole program optimization of virtual call when the list of callees 1752349cc55cSDimitry Andric // is fixed. 1753349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1754349cc55cSDimitry Andric 1755349cc55cSDimitry Andric // Stop here at -O1. 1756349cc55cSDimitry Andric if (Level == OptimizationLevel::O1) { 1757349cc55cSDimitry Andric // The LowerTypeTestsPass needs to run to lower type metadata and the 1758349cc55cSDimitry Andric // type.test intrinsics. The pass does nothing if CFI is disabled. 1759349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1760349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1761349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO 1762349cc55cSDimitry Andric // pipeline). 1763349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1764349cc55cSDimitry Andric 176506c3fb27SDimitry Andric invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level); 176681ad6265SDimitry Andric 1767349cc55cSDimitry Andric // Emit annotation remarks. 1768349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1769349cc55cSDimitry Andric 1770349cc55cSDimitry Andric return MPM; 1771349cc55cSDimitry Andric } 1772349cc55cSDimitry Andric 1773349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 1774349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1775349cc55cSDimitry Andric 1776349cc55cSDimitry Andric // Promote any localized globals to SSA registers. 1777349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1778349cc55cSDimitry Andric 1779349cc55cSDimitry Andric // Linking modules together can lead to duplicate global constant, only 1780349cc55cSDimitry Andric // keep one copy of each constant. 1781349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1782349cc55cSDimitry Andric 17831ac55f4cSDimitry Andric // Remove unused arguments from functions. 17841ac55f4cSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 17851ac55f4cSDimitry Andric 1786349cc55cSDimitry Andric // Reduce the code after globalopt and ipsccp. Both can open up significant 1787349cc55cSDimitry Andric // simplification opportunities, and both can propagate functions through 1788349cc55cSDimitry Andric // function pointers. When this happens, we often have to resolve varargs 1789349cc55cSDimitry Andric // calls, etc, so let instcombine do this. 1790349cc55cSDimitry Andric FunctionPassManager PeepholeFPM; 17910eae32dcSDimitry Andric PeepholeFPM.addPass(InstCombinePass()); 179206c3fb27SDimitry Andric if (Level.getSpeedupLevel() > 1) 1793349cc55cSDimitry Andric PeepholeFPM.addPass(AggressiveInstCombinePass()); 1794349cc55cSDimitry Andric invokePeepholeEPCallbacks(PeepholeFPM, Level); 1795349cc55cSDimitry Andric 1796349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM), 1797349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1798349cc55cSDimitry Andric 1799349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 1800349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 1801349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 1802349cc55cSDimitry Andric // invoke or a call. 1803349cc55cSDimitry Andric // Run the inliner now. 180406c3fb27SDimitry Andric if (EnableModuleInliner) { 180506c3fb27SDimitry Andric MPM.addPass(ModuleInlinerPass(getInlineParamsFromOptLevel(Level), 180606c3fb27SDimitry Andric UseInlineAdvisor, 180706c3fb27SDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink)); 180806c3fb27SDimitry Andric } else { 180981ad6265SDimitry Andric MPM.addPass(ModuleInlinerWrapperPass( 181081ad6265SDimitry Andric getInlineParamsFromOptLevel(Level), 181181ad6265SDimitry Andric /* MandatoryFirst */ true, 181281ad6265SDimitry Andric InlineContext{ThinOrFullLTOPhase::FullLTOPostLink, 181381ad6265SDimitry Andric InlinePass::CGSCCInliner})); 181406c3fb27SDimitry Andric } 181506c3fb27SDimitry Andric 181606c3fb27SDimitry Andric // Perform context disambiguation after inlining, since that would reduce the 181706c3fb27SDimitry Andric // amount of additional cloning required to distinguish the allocation 181806c3fb27SDimitry Andric // contexts. 181906c3fb27SDimitry Andric if (EnableMemProfContextDisambiguation) 182006c3fb27SDimitry Andric MPM.addPass(MemProfContextDisambiguation()); 1821349cc55cSDimitry Andric 1822349cc55cSDimitry Andric // Optimize globals again after we ran the inliner. 1823349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1824349cc55cSDimitry Andric 18251ac55f4cSDimitry Andric // Run the OpenMPOpt pass again after global optimizations. 18261ac55f4cSDimitry Andric MPM.addPass(OpenMPOptPass(ThinOrFullLTOPhase::FullLTOPostLink)); 18271ac55f4cSDimitry Andric 1828349cc55cSDimitry Andric // Garbage collect dead functions. 182906c3fb27SDimitry Andric MPM.addPass(GlobalDCEPass(/*InLTOPostLink=*/true)); 1830349cc55cSDimitry Andric 1831349cc55cSDimitry Andric // If we didn't decide to inline a function, check to see if we can 1832349cc55cSDimitry Andric // transform it to pass arguments by value instead of by reference. 1833349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(ArgumentPromotionPass())); 1834349cc55cSDimitry Andric 1835349cc55cSDimitry Andric FunctionPassManager FPM; 1836349cc55cSDimitry Andric // The IPO Passes may leave cruft around. Clean up after them. 1837349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1838349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 1839349cc55cSDimitry Andric 1840bdd1243dSDimitry Andric if (EnableConstraintElimination) 1841bdd1243dSDimitry Andric FPM.addPass(ConstraintEliminationPass()); 1842bdd1243dSDimitry Andric 184381ad6265SDimitry Andric FPM.addPass(JumpThreadingPass()); 1844349cc55cSDimitry Andric 1845349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1846349cc55cSDimitry Andric // sensitive PGO pass. 1847349cc55cSDimitry Andric if (PGOOpt) { 1848349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1849*5f757f3fSDimitry Andric addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/true, 1850*5f757f3fSDimitry Andric /*IsCS=*/true, PGOOpt->AtomicCounterUpdate, 1851*5f757f3fSDimitry Andric PGOOpt->CSProfileGenFile, PGOOpt->ProfileRemappingFile, 1852*5f757f3fSDimitry Andric PGOOpt->FS); 1853349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1854*5f757f3fSDimitry Andric addPGOInstrPasses(MPM, Level, /*RunProfileGen=*/false, 1855*5f757f3fSDimitry Andric /*IsCS=*/true, PGOOpt->AtomicCounterUpdate, 1856*5f757f3fSDimitry Andric PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile, 1857*5f757f3fSDimitry Andric PGOOpt->FS); 1858349cc55cSDimitry Andric } 1859349cc55cSDimitry Andric 1860349cc55cSDimitry Andric // Break up allocas 1861bdd1243dSDimitry Andric FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); 1862349cc55cSDimitry Andric 1863349cc55cSDimitry Andric // LTO provides additional opportunities for tailcall elimination due to 1864349cc55cSDimitry Andric // link-time inlining, and visibility of nocapture attribute. 1865349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 1866349cc55cSDimitry Andric 1867349cc55cSDimitry Andric // Run a few AA driver optimizations here and now to cleanup the code. 1868349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM), 1869349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1870349cc55cSDimitry Andric 1871349cc55cSDimitry Andric MPM.addPass( 1872349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1873349cc55cSDimitry Andric 1874349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 1875349cc55cSDimitry Andric // MainFPM. 1876*5f757f3fSDimitry Andric if (EnableGlobalAnalyses) { 1877349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1878*5f757f3fSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly 1879*5f757f3fSDimitry Andric // available GlobalsAA. 1880349cc55cSDimitry Andric MPM.addPass( 1881349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 1882*5f757f3fSDimitry Andric } 1883349cc55cSDimitry Andric 1884349cc55cSDimitry Andric FunctionPassManager MainFPM; 1885349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1886fb03ea46SDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap, 1887fb03ea46SDimitry Andric /*AllowSpeculation=*/true), 188806c3fb27SDimitry Andric /*USeMemorySSA=*/true, /*UseBlockFrequencyInfo=*/false)); 1889349cc55cSDimitry Andric 1890349cc55cSDimitry Andric if (RunNewGVN) 1891349cc55cSDimitry Andric MainFPM.addPass(NewGVNPass()); 1892349cc55cSDimitry Andric else 1893349cc55cSDimitry Andric MainFPM.addPass(GVNPass()); 1894349cc55cSDimitry Andric 1895349cc55cSDimitry Andric // Remove dead memcpy()'s. 1896349cc55cSDimitry Andric MainFPM.addPass(MemCpyOptPass()); 1897349cc55cSDimitry Andric 1898349cc55cSDimitry Andric // Nuke dead stores. 1899349cc55cSDimitry Andric MainFPM.addPass(DSEPass()); 190006c3fb27SDimitry Andric MainFPM.addPass(MoveAutoInitPass()); 1901349cc55cSDimitry Andric MainFPM.addPass(MergedLoadStoreMotionPass()); 1902349cc55cSDimitry Andric 1903349cc55cSDimitry Andric LoopPassManager LPM; 190404eeddc0SDimitry Andric if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) 190504eeddc0SDimitry Andric LPM.addPass(LoopFlattenPass()); 1906349cc55cSDimitry Andric LPM.addPass(IndVarSimplifyPass()); 1907349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1908349cc55cSDimitry Andric // FIXME: Add loop interchange. 1909349cc55cSDimitry Andric 1910349cc55cSDimitry Andric // Unroll small loops and perform peeling. 1911349cc55cSDimitry Andric LPM.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 1912349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 1913349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 1914349cc55cSDimitry Andric // The loop passes in LPM (LoopFullUnrollPass) do not preserve MemorySSA. 1915349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 1916349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1917349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/true)); 1918349cc55cSDimitry Andric 1919349cc55cSDimitry Andric MainFPM.addPass(LoopDistributePass()); 1920349cc55cSDimitry Andric 1921349cc55cSDimitry Andric addVectorPasses(Level, MainFPM, /* IsFullLTO */ true); 1922349cc55cSDimitry Andric 19231fd87a68SDimitry Andric // Run the OpenMPOpt CGSCC pass again late. 19241ac55f4cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( 19251ac55f4cSDimitry Andric OpenMPOptCGSCCPass(ThinOrFullLTOPhase::FullLTOPostLink))); 19261fd87a68SDimitry Andric 1927349cc55cSDimitry Andric invokePeepholeEPCallbacks(MainFPM, Level); 192881ad6265SDimitry Andric MainFPM.addPass(JumpThreadingPass()); 1929349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM), 1930349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1931349cc55cSDimitry Andric 1932349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic. This pass supports 1933349cc55cSDimitry Andric // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs 1934349cc55cSDimitry Andric // to be run at link time if CFI is enabled. This pass does nothing if 1935349cc55cSDimitry Andric // CFI is disabled. 1936349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1937349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1938349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO pipeline). 1939349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1940349cc55cSDimitry Andric 1941753f127fSDimitry Andric // Enable splitting late in the FullLTO post-link pipeline. 1942349cc55cSDimitry Andric if (EnableHotColdSplit) 1943349cc55cSDimitry Andric MPM.addPass(HotColdSplittingPass()); 1944349cc55cSDimitry Andric 1945349cc55cSDimitry Andric // Add late LTO optimization passes. 194606c3fb27SDimitry Andric FunctionPassManager LateFPM; 194706c3fb27SDimitry Andric 194806c3fb27SDimitry Andric // LoopSink pass sinks instructions hoisted by LICM, which serves as a 194906c3fb27SDimitry Andric // canonicalization pass that enables other optimizations. As a result, 195006c3fb27SDimitry Andric // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 195106c3fb27SDimitry Andric // result too early. 195206c3fb27SDimitry Andric LateFPM.addPass(LoopSinkPass()); 195306c3fb27SDimitry Andric 195406c3fb27SDimitry Andric // This hoists/decomposes div/rem ops. It should run after other sink/hoist 195506c3fb27SDimitry Andric // passes to avoid re-sinking, but before SimplifyCFG because it can allow 195606c3fb27SDimitry Andric // flattening of blocks. 195706c3fb27SDimitry Andric LateFPM.addPass(DivRemPairsPass()); 195806c3fb27SDimitry Andric 1959349cc55cSDimitry Andric // Delete basic blocks, which optimization passes may have killed. 196006c3fb27SDimitry Andric LateFPM.addPass(SimplifyCFGPass( 1961fb03ea46SDimitry Andric SimplifyCFGOptions().convertSwitchRangeToICmp(true).hoistCommonInsts( 196206c3fb27SDimitry Andric true))); 196306c3fb27SDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM))); 1964349cc55cSDimitry Andric 1965349cc55cSDimitry Andric // Drop bodies of available eternally objects to improve GlobalDCE. 1966349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1967349cc55cSDimitry Andric 1968349cc55cSDimitry Andric // Now that we have optimized the program, discard unreachable functions. 196906c3fb27SDimitry Andric MPM.addPass(GlobalDCEPass(/*InLTOPostLink=*/true)); 1970349cc55cSDimitry Andric 1971349cc55cSDimitry Andric if (PTO.MergeFunctions) 1972349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1973349cc55cSDimitry Andric 197481ad6265SDimitry Andric if (PTO.CallGraphProfile) 197581ad6265SDimitry Andric MPM.addPass(CGProfilePass()); 197681ad6265SDimitry Andric 197706c3fb27SDimitry Andric invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level); 197881ad6265SDimitry Andric 1979349cc55cSDimitry Andric // Emit annotation remarks. 1980349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1981349cc55cSDimitry Andric 1982349cc55cSDimitry Andric return MPM; 1983349cc55cSDimitry Andric } 1984349cc55cSDimitry Andric 1985349cc55cSDimitry Andric ModulePassManager PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level, 1986349cc55cSDimitry Andric bool LTOPreLink) { 1987349cc55cSDimitry Andric assert(Level == OptimizationLevel::O0 && 1988349cc55cSDimitry Andric "buildO0DefaultPipeline should only be used with O0"); 1989349cc55cSDimitry Andric 1990349cc55cSDimitry Andric ModulePassManager MPM; 1991349cc55cSDimitry Andric 1992349cc55cSDimitry Andric // Perform pseudo probe instrumentation in O0 mode. This is for the 1993349cc55cSDimitry Andric // consistency between different build modes. For example, a LTO build can be 1994349cc55cSDimitry Andric // mixed with an O0 prelink and an O2 postlink. Loading a sample profile in 1995349cc55cSDimitry Andric // the postlink will require pseudo probe instrumentation in the prelink. 1996349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling) 1997349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 1998349cc55cSDimitry Andric 1999349cc55cSDimitry Andric if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr || 2000349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) 2001349cc55cSDimitry Andric addPGOInstrPassesForO0( 2002349cc55cSDimitry Andric MPM, 2003*5f757f3fSDimitry Andric /*RunProfileGen=*/(PGOOpt->Action == PGOOptions::IRInstr), 2004*5f757f3fSDimitry Andric /*IsCS=*/false, PGOOpt->AtomicCounterUpdate, PGOOpt->ProfileFile, 2005*5f757f3fSDimitry Andric PGOOpt->ProfileRemappingFile, PGOOpt->FS); 2006349cc55cSDimitry Andric 200706c3fb27SDimitry Andric invokePipelineStartEPCallbacks(MPM, Level); 2008349cc55cSDimitry Andric 2009349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 2010349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 2011349cc55cSDimitry Andric 201206c3fb27SDimitry Andric invokePipelineEarlySimplificationEPCallbacks(MPM, Level); 2013349cc55cSDimitry Andric 2014349cc55cSDimitry Andric // Build a minimal pipeline based on the semantics required by LLVM, 2015349cc55cSDimitry Andric // which is just that always inlining occurs. Further, disable generating 2016349cc55cSDimitry Andric // lifetime intrinsics to avoid enabling further optimizations during 2017349cc55cSDimitry Andric // code generation. 2018349cc55cSDimitry Andric MPM.addPass(AlwaysInlinerPass( 2019349cc55cSDimitry Andric /*InsertLifetimeIntrinsics=*/false)); 2020349cc55cSDimitry Andric 2021349cc55cSDimitry Andric if (PTO.MergeFunctions) 2022349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 2023349cc55cSDimitry Andric 2024349cc55cSDimitry Andric if (EnableMatrix) 2025349cc55cSDimitry Andric MPM.addPass( 2026349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true))); 2027349cc55cSDimitry Andric 2028349cc55cSDimitry Andric if (!CGSCCOptimizerLateEPCallbacks.empty()) { 2029349cc55cSDimitry Andric CGSCCPassManager CGPM; 203006c3fb27SDimitry Andric invokeCGSCCOptimizerLateEPCallbacks(CGPM, Level); 2031349cc55cSDimitry Andric if (!CGPM.isEmpty()) 2032349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 2033349cc55cSDimitry Andric } 2034349cc55cSDimitry Andric if (!LateLoopOptimizationsEPCallbacks.empty()) { 2035349cc55cSDimitry Andric LoopPassManager LPM; 203606c3fb27SDimitry Andric invokeLateLoopOptimizationsEPCallbacks(LPM, Level); 2037349cc55cSDimitry Andric if (!LPM.isEmpty()) { 2038349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 2039349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 2040349cc55cSDimitry Andric } 2041349cc55cSDimitry Andric } 2042349cc55cSDimitry Andric if (!LoopOptimizerEndEPCallbacks.empty()) { 2043349cc55cSDimitry Andric LoopPassManager LPM; 204406c3fb27SDimitry Andric invokeLoopOptimizerEndEPCallbacks(LPM, Level); 2045349cc55cSDimitry Andric if (!LPM.isEmpty()) { 2046349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 2047349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 2048349cc55cSDimitry Andric } 2049349cc55cSDimitry Andric } 2050349cc55cSDimitry Andric if (!ScalarOptimizerLateEPCallbacks.empty()) { 2051349cc55cSDimitry Andric FunctionPassManager FPM; 205206c3fb27SDimitry Andric invokeScalarOptimizerLateEPCallbacks(FPM, Level); 2053349cc55cSDimitry Andric if (!FPM.isEmpty()) 2054349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 2055349cc55cSDimitry Andric } 205681ad6265SDimitry Andric 205706c3fb27SDimitry Andric invokeOptimizerEarlyEPCallbacks(MPM, Level); 205881ad6265SDimitry Andric 2059349cc55cSDimitry Andric if (!VectorizerStartEPCallbacks.empty()) { 2060349cc55cSDimitry Andric FunctionPassManager FPM; 206106c3fb27SDimitry Andric invokeVectorizerStartEPCallbacks(FPM, Level); 2062349cc55cSDimitry Andric if (!FPM.isEmpty()) 2063349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 2064349cc55cSDimitry Andric } 2065349cc55cSDimitry Andric 206681ad6265SDimitry Andric ModulePassManager CoroPM; 206781ad6265SDimitry Andric CoroPM.addPass(CoroEarlyPass()); 2068349cc55cSDimitry Andric CGSCCPassManager CGPM; 2069349cc55cSDimitry Andric CGPM.addPass(CoroSplitPass()); 207081ad6265SDimitry Andric CoroPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 207181ad6265SDimitry Andric CoroPM.addPass(CoroCleanupPass()); 207281ad6265SDimitry Andric CoroPM.addPass(GlobalDCEPass()); 207381ad6265SDimitry Andric MPM.addPass(CoroConditionalWrapper(std::move(CoroPM))); 2074349cc55cSDimitry Andric 207506c3fb27SDimitry Andric invokeOptimizerLastEPCallbacks(MPM, Level); 2076349cc55cSDimitry Andric 2077349cc55cSDimitry Andric if (LTOPreLink) 2078349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 2079349cc55cSDimitry Andric 20804824e7fdSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass())); 20814824e7fdSDimitry Andric 2082349cc55cSDimitry Andric return MPM; 2083349cc55cSDimitry Andric } 2084349cc55cSDimitry Andric 2085349cc55cSDimitry Andric AAManager PassBuilder::buildDefaultAAPipeline() { 2086349cc55cSDimitry Andric AAManager AA; 2087349cc55cSDimitry Andric 2088349cc55cSDimitry Andric // The order in which these are registered determines their priority when 2089349cc55cSDimitry Andric // being queried. 2090349cc55cSDimitry Andric 2091349cc55cSDimitry Andric // First we register the basic alias analysis that provides the majority of 2092349cc55cSDimitry Andric // per-function local AA logic. This is a stateless, on-demand local set of 2093349cc55cSDimitry Andric // AA techniques. 2094349cc55cSDimitry Andric AA.registerFunctionAnalysis<BasicAA>(); 2095349cc55cSDimitry Andric 2096349cc55cSDimitry Andric // Next we query fast, specialized alias analyses that wrap IR-embedded 2097349cc55cSDimitry Andric // information about aliasing. 2098349cc55cSDimitry Andric AA.registerFunctionAnalysis<ScopedNoAliasAA>(); 2099349cc55cSDimitry Andric AA.registerFunctionAnalysis<TypeBasedAA>(); 2100349cc55cSDimitry Andric 2101349cc55cSDimitry Andric // Add support for querying global aliasing information when available. 2102349cc55cSDimitry Andric // Because the `AAManager` is a function analysis and `GlobalsAA` is a module 2103349cc55cSDimitry Andric // analysis, all that the `AAManager` can do is query for any *cached* 2104349cc55cSDimitry Andric // results from `GlobalsAA` through a readonly proxy. 2105bdd1243dSDimitry Andric if (EnableGlobalAnalyses) 2106349cc55cSDimitry Andric AA.registerModuleAnalysis<GlobalsAA>(); 2107349cc55cSDimitry Andric 2108349cc55cSDimitry Andric // Add target-specific alias analyses. 2109349cc55cSDimitry Andric if (TM) 2110349cc55cSDimitry Andric TM->registerDefaultAliasAnalyses(AA); 2111349cc55cSDimitry Andric 2112349cc55cSDimitry Andric return AA; 2113349cc55cSDimitry Andric } 2114