1349cc55cSDimitry Andric //===- Construction of pass pipelines -------------------------------------===// 2349cc55cSDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6349cc55cSDimitry Andric // 7349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8349cc55cSDimitry Andric /// \file 9349cc55cSDimitry Andric /// 10349cc55cSDimitry Andric /// This file provides the implementation of the PassBuilder based on our 11349cc55cSDimitry Andric /// static pass registry as well as related functionality. It also provides 12349cc55cSDimitry Andric /// helpers to aid in analyzing, debugging, and testing passes and pass 13349cc55cSDimitry Andric /// pipelines. 14349cc55cSDimitry Andric /// 15349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 16349cc55cSDimitry Andric 17349cc55cSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 18349cc55cSDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h" 19349cc55cSDimitry Andric #include "llvm/Analysis/CGSCCPassManager.h" 20349cc55cSDimitry Andric #include "llvm/Analysis/GlobalsModRef.h" 21349cc55cSDimitry Andric #include "llvm/Analysis/InlineAdvisor.h" 22349cc55cSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 23349cc55cSDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h" 24349cc55cSDimitry Andric #include "llvm/Analysis/ScopedNoAliasAA.h" 25349cc55cSDimitry Andric #include "llvm/Analysis/TypeBasedAliasAnalysis.h" 26349cc55cSDimitry Andric #include "llvm/IR/PassManager.h" 27349cc55cSDimitry Andric #include "llvm/Passes/OptimizationLevel.h" 28349cc55cSDimitry Andric #include "llvm/Passes/PassBuilder.h" 29349cc55cSDimitry Andric #include "llvm/Support/CommandLine.h" 30349cc55cSDimitry Andric #include "llvm/Support/ErrorHandling.h" 31349cc55cSDimitry Andric #include "llvm/Support/PGOOptions.h" 32349cc55cSDimitry Andric #include "llvm/Target/TargetMachine.h" 33349cc55cSDimitry Andric #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" 34349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroCleanup.h" 35349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroEarly.h" 36349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroElide.h" 37349cc55cSDimitry Andric #include "llvm/Transforms/Coroutines/CoroSplit.h" 38349cc55cSDimitry Andric #include "llvm/Transforms/IPO/AlwaysInliner.h" 39349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Annotation2Metadata.h" 40349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ArgumentPromotion.h" 41349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Attributor.h" 42349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CalledValuePropagation.h" 43349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ConstantMerge.h" 44349cc55cSDimitry Andric #include "llvm/Transforms/IPO/CrossDSOCFI.h" 45349cc55cSDimitry Andric #include "llvm/Transforms/IPO/DeadArgumentElimination.h" 46349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ElimAvailExtern.h" 47349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" 48349cc55cSDimitry Andric #include "llvm/Transforms/IPO/FunctionAttrs.h" 49349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalDCE.h" 50349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalOpt.h" 51349cc55cSDimitry Andric #include "llvm/Transforms/IPO/GlobalSplit.h" 52349cc55cSDimitry Andric #include "llvm/Transforms/IPO/HotColdSplitting.h" 53349cc55cSDimitry Andric #include "llvm/Transforms/IPO/IROutliner.h" 54349cc55cSDimitry Andric #include "llvm/Transforms/IPO/InferFunctionAttrs.h" 55349cc55cSDimitry Andric #include "llvm/Transforms/IPO/Inliner.h" 56349cc55cSDimitry Andric #include "llvm/Transforms/IPO/LowerTypeTests.h" 57349cc55cSDimitry Andric #include "llvm/Transforms/IPO/MergeFunctions.h" 58349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ModuleInliner.h" 59349cc55cSDimitry Andric #include "llvm/Transforms/IPO/OpenMPOpt.h" 60349cc55cSDimitry Andric #include "llvm/Transforms/IPO/PartialInlining.h" 61349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SCCP.h" 62349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfile.h" 63349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SampleProfileProbe.h" 64349cc55cSDimitry Andric #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" 65349cc55cSDimitry Andric #include "llvm/Transforms/IPO/WholeProgramDevirt.h" 66349cc55cSDimitry Andric #include "llvm/Transforms/InstCombine/InstCombine.h" 67349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/CGProfile.h" 68349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h" 69349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrOrderFile.h" 70349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/InstrProfiling.h" 71349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/MemProfiler.h" 72349cc55cSDimitry Andric #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" 73349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ADCE.h" 74349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" 75349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/AnnotationRemarks.h" 76349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/BDCE.h" 77349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CallSiteSplitting.h" 78349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/ConstraintElimination.h" 79349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" 80349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DFAJumpThreading.h" 81349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DeadStoreElimination.h" 82349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/DivRemPairs.h" 83349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/EarlyCSE.h" 84349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Float2Int.h" 85349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/GVN.h" 86349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/IndVarSimplify.h" 87349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/InstSimplifyPass.h" 88349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/JumpThreading.h" 89349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LICM.h" 90349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDeletion.h" 91349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopDistribute.h" 92349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopFlatten.h" 93349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" 94349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInstSimplify.h" 95349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopInterchange.h" 96349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopLoadElimination.h" 97349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopPassManager.h" 98349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopRotation.h" 99349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" 100349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopSink.h" 101349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" 102349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LoopUnrollPass.h" 103349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h" 104349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" 105349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h" 106349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" 107349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" 108349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/NewGVN.h" 109349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/Reassociate.h" 110349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SCCP.h" 111349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SROA.h" 112349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 113349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SimplifyCFG.h" 114349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/SpeculativeExecution.h" 115349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/TailRecursionElimination.h" 116349cc55cSDimitry Andric #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" 117349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AddDiscriminators.h" 118349cc55cSDimitry Andric #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 119349cc55cSDimitry Andric #include "llvm/Transforms/Utils/CanonicalizeAliases.h" 120349cc55cSDimitry Andric #include "llvm/Transforms/Utils/InjectTLIMappings.h" 121349cc55cSDimitry Andric #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" 122349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Mem2Reg.h" 123349cc55cSDimitry Andric #include "llvm/Transforms/Utils/NameAnonGlobals.h" 124349cc55cSDimitry Andric #include "llvm/Transforms/Utils/RelLookupTableConverter.h" 125349cc55cSDimitry Andric #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" 126349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/LoopVectorize.h" 127349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/SLPVectorizer.h" 128349cc55cSDimitry Andric #include "llvm/Transforms/Vectorize/VectorCombine.h" 129349cc55cSDimitry Andric 130349cc55cSDimitry Andric using namespace llvm; 131349cc55cSDimitry Andric 132349cc55cSDimitry Andric static cl::opt<InliningAdvisorMode> UseInlineAdvisor( 133349cc55cSDimitry Andric "enable-ml-inliner", cl::init(InliningAdvisorMode::Default), cl::Hidden, 134349cc55cSDimitry Andric cl::desc("Enable ML policy for inliner. Currently trained for -Oz only"), 135349cc55cSDimitry Andric cl::values(clEnumValN(InliningAdvisorMode::Default, "default", 136349cc55cSDimitry Andric "Heuristics-based inliner version."), 137349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Development, "development", 138349cc55cSDimitry Andric "Use development mode (runtime-loadable model)."), 139349cc55cSDimitry Andric clEnumValN(InliningAdvisorMode::Release, "release", 140349cc55cSDimitry Andric "Use release mode (AOT-compiled model)."))); 141349cc55cSDimitry Andric 142349cc55cSDimitry Andric static cl::opt<bool> EnableSyntheticCounts( 143349cc55cSDimitry Andric "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore, 144349cc55cSDimitry Andric cl::desc("Run synthetic function entry count generation " 145349cc55cSDimitry Andric "pass")); 146349cc55cSDimitry Andric 147349cc55cSDimitry Andric /// Flag to enable inline deferral during PGO. 148349cc55cSDimitry Andric static cl::opt<bool> 149349cc55cSDimitry Andric EnablePGOInlineDeferral("enable-npm-pgo-inline-deferral", cl::init(true), 150349cc55cSDimitry Andric cl::Hidden, 151349cc55cSDimitry Andric cl::desc("Enable inline deferral during PGO")); 152349cc55cSDimitry Andric 153349cc55cSDimitry Andric static cl::opt<bool> EnableMemProfiler("enable-mem-prof", cl::init(false), 154349cc55cSDimitry Andric cl::Hidden, cl::ZeroOrMore, 155349cc55cSDimitry Andric cl::desc("Enable memory profiler")); 156349cc55cSDimitry Andric 157349cc55cSDimitry Andric static cl::opt<bool> EnableModuleInliner("enable-module-inliner", 158349cc55cSDimitry Andric cl::init(false), cl::Hidden, 159349cc55cSDimitry Andric cl::desc("Enable module inliner")); 160349cc55cSDimitry Andric 161349cc55cSDimitry Andric static cl::opt<bool> PerformMandatoryInliningsFirst( 162349cc55cSDimitry Andric "mandatory-inlining-first", cl::init(true), cl::Hidden, cl::ZeroOrMore, 163349cc55cSDimitry Andric cl::desc("Perform mandatory inlinings module-wide, before performing " 164349cc55cSDimitry Andric "inlining.")); 165349cc55cSDimitry Andric 166349cc55cSDimitry Andric static cl::opt<bool> EnableO3NonTrivialUnswitching( 167349cc55cSDimitry Andric "enable-npm-O3-nontrivial-unswitch", cl::init(true), cl::Hidden, 168349cc55cSDimitry Andric cl::ZeroOrMore, cl::desc("Enable non-trivial loop unswitching for -O3")); 169349cc55cSDimitry Andric 170349cc55cSDimitry Andric static cl::opt<bool> EnableEagerlyInvalidateAnalyses( 171349cc55cSDimitry Andric "eagerly-invalidate-analyses", cl::init(true), cl::Hidden, 172349cc55cSDimitry Andric cl::desc("Eagerly invalidate more analyses in default pipelines")); 173349cc55cSDimitry Andric 174349cc55cSDimitry Andric static cl::opt<bool> EnableNoRerunSimplificationPipeline( 175349cc55cSDimitry Andric "enable-no-rerun-simplification-pipeline", cl::init(false), cl::Hidden, 176349cc55cSDimitry Andric cl::desc( 177349cc55cSDimitry Andric "Prevent running the simplification pipeline on a function more " 178349cc55cSDimitry Andric "than once in the case that SCC mutations cause a function to be " 179349cc55cSDimitry Andric "visited multiple times as long as the function has not been changed")); 180349cc55cSDimitry Andric 181349cc55cSDimitry Andric PipelineTuningOptions::PipelineTuningOptions() { 182349cc55cSDimitry Andric LoopInterleaving = true; 183349cc55cSDimitry Andric LoopVectorization = true; 184349cc55cSDimitry Andric SLPVectorization = false; 185349cc55cSDimitry Andric LoopUnrolling = true; 186349cc55cSDimitry Andric ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll; 187349cc55cSDimitry Andric LicmMssaOptCap = SetLicmMssaOptCap; 188349cc55cSDimitry Andric LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap; 189349cc55cSDimitry Andric CallGraphProfile = true; 190349cc55cSDimitry Andric MergeFunctions = false; 191349cc55cSDimitry Andric EagerlyInvalidateAnalyses = EnableEagerlyInvalidateAnalyses; 192349cc55cSDimitry Andric } 193349cc55cSDimitry Andric 194349cc55cSDimitry Andric namespace llvm { 195349cc55cSDimitry Andric 196349cc55cSDimitry Andric extern cl::opt<unsigned> MaxDevirtIterations; 197349cc55cSDimitry Andric extern cl::opt<bool> EnableConstraintElimination; 198349cc55cSDimitry Andric extern cl::opt<bool> EnableFunctionSpecialization; 199349cc55cSDimitry Andric extern cl::opt<bool> EnableGVNHoist; 200349cc55cSDimitry Andric extern cl::opt<bool> EnableGVNSink; 201349cc55cSDimitry Andric extern cl::opt<bool> EnableHotColdSplit; 202349cc55cSDimitry Andric extern cl::opt<bool> EnableIROutliner; 203349cc55cSDimitry Andric extern cl::opt<bool> EnableOrderFileInstrumentation; 204349cc55cSDimitry Andric extern cl::opt<bool> EnableCHR; 205349cc55cSDimitry Andric extern cl::opt<bool> EnableLoopInterchange; 206349cc55cSDimitry Andric extern cl::opt<bool> EnableUnrollAndJam; 207349cc55cSDimitry Andric extern cl::opt<bool> EnableLoopFlatten; 208349cc55cSDimitry Andric extern cl::opt<bool> EnableDFAJumpThreading; 209349cc55cSDimitry Andric extern cl::opt<bool> RunNewGVN; 210349cc55cSDimitry Andric extern cl::opt<bool> RunPartialInlining; 211349cc55cSDimitry Andric extern cl::opt<bool> ExtraVectorizerPasses; 212349cc55cSDimitry Andric 213349cc55cSDimitry Andric extern cl::opt<bool> FlattenedProfileUsed; 214349cc55cSDimitry Andric 215349cc55cSDimitry Andric extern cl::opt<AttributorRunOption> AttributorRun; 216349cc55cSDimitry Andric extern cl::opt<bool> EnableKnowledgeRetention; 217349cc55cSDimitry Andric 218349cc55cSDimitry Andric extern cl::opt<bool> EnableMatrix; 219349cc55cSDimitry Andric 220349cc55cSDimitry Andric extern cl::opt<bool> DisablePreInliner; 221349cc55cSDimitry Andric extern cl::opt<int> PreInlineThreshold; 222349cc55cSDimitry Andric } // namespace llvm 223349cc55cSDimitry Andric 224349cc55cSDimitry Andric void PassBuilder::invokePeepholeEPCallbacks(FunctionPassManager &FPM, 225349cc55cSDimitry Andric OptimizationLevel Level) { 226349cc55cSDimitry Andric for (auto &C : PeepholeEPCallbacks) 227349cc55cSDimitry Andric C(FPM, Level); 228349cc55cSDimitry Andric } 229349cc55cSDimitry Andric 230349cc55cSDimitry Andric // Helper to add AnnotationRemarksPass. 231349cc55cSDimitry Andric static void addAnnotationRemarksPass(ModulePassManager &MPM) { 232349cc55cSDimitry Andric FunctionPassManager FPM; 233349cc55cSDimitry Andric FPM.addPass(AnnotationRemarksPass()); 234349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 235349cc55cSDimitry Andric } 236349cc55cSDimitry Andric 237349cc55cSDimitry Andric // Helper to check if the current compilation phase is preparing for LTO 238349cc55cSDimitry Andric static bool isLTOPreLink(ThinOrFullLTOPhase Phase) { 239349cc55cSDimitry Andric return Phase == ThinOrFullLTOPhase::ThinLTOPreLink || 240349cc55cSDimitry Andric Phase == ThinOrFullLTOPhase::FullLTOPreLink; 241349cc55cSDimitry Andric } 242349cc55cSDimitry Andric 243349cc55cSDimitry Andric // TODO: Investigate the cost/benefit of tail call elimination on debugging. 244349cc55cSDimitry Andric FunctionPassManager 245349cc55cSDimitry Andric PassBuilder::buildO1FunctionSimplificationPipeline(OptimizationLevel Level, 246349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 247349cc55cSDimitry Andric 248349cc55cSDimitry Andric FunctionPassManager FPM; 249349cc55cSDimitry Andric 250349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 251349cc55cSDimitry Andric // scalars. 252349cc55cSDimitry Andric FPM.addPass(SROAPass()); 253349cc55cSDimitry Andric 254349cc55cSDimitry Andric // Catch trivial redundancies 255349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 256349cc55cSDimitry Andric 257349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 258349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 259349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 260349cc55cSDimitry Andric 261349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 262349cc55cSDimitry Andric 263349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 264349cc55cSDimitry Andric 265349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 266349cc55cSDimitry Andric 267349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 268349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 269349cc55cSDimitry Andric // minimal multiplication trees. 270349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 271349cc55cSDimitry Andric 272349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 273349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 274349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 275349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 276349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 277349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 278349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 279349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 280349cc55cSDimitry Andric // `LoopInstSimplify`. 281349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 282349cc55cSDimitry Andric 283349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 284349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 285349cc55cSDimitry Andric // implications on the outer loop. 286349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 287349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 288349cc55cSDimitry Andric 289349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 290349cc55cSDimitry Andric // to reduce amount of IR that will have to be duplicated. 291349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 292349cc55cSDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 293349cc55cSDimitry Andric 294349cc55cSDimitry Andric LPM1.addPass(LoopRotatePass(/* Disable header duplication */ true, 295349cc55cSDimitry Andric isLTOPreLink(Phase))); 296349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 297349cc55cSDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 298349cc55cSDimitry Andric LPM1.addPass(SimpleLoopUnswitchPass()); 299349cc55cSDimitry Andric 300349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 301349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 302349cc55cSDimitry Andric 303349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 304349cc55cSDimitry Andric C(LPM2, Level); 305349cc55cSDimitry Andric 306349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 307349cc55cSDimitry Andric 308349cc55cSDimitry Andric if (EnableLoopInterchange) 309349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 310349cc55cSDimitry Andric 311349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 312349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 313349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 314349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 315349cc55cSDimitry Andric // attention to it. 316349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 317349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 318349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 319349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 320349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 321349cc55cSDimitry Andric 322349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 323349cc55cSDimitry Andric C(LPM2, Level); 324349cc55cSDimitry Andric 325349cc55cSDimitry Andric // We provide the opt remark emitter pass for LICM to use. We only need to do 326349cc55cSDimitry Andric // this once as it is immutable. 327349cc55cSDimitry Andric FPM.addPass( 328349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 329349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 330349cc55cSDimitry Andric /*UseMemorySSA=*/true, 331349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 332349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 333349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 334349cc55cSDimitry Andric if (EnableLoopFlatten) 335349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 336349cc55cSDimitry Andric // The loop passes in LPM2 (LoopFullUnrollPass) do not preserve MemorySSA. 337349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 338349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 339349cc55cSDimitry Andric /*UseMemorySSA=*/false, 340349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 341349cc55cSDimitry Andric 342349cc55cSDimitry Andric // Delete small array after loop unroll. 343349cc55cSDimitry Andric FPM.addPass(SROAPass()); 344349cc55cSDimitry Andric 345349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 346349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 347349cc55cSDimitry Andric 348349cc55cSDimitry Andric // Sparse conditional constant propagation. 349349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 350349cc55cSDimitry Andric // before... 351349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 352349cc55cSDimitry Andric 353349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 354349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 355349cc55cSDimitry Andric // opportunities that creates). 356349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 357349cc55cSDimitry Andric 358349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 359349cc55cSDimitry Andric // opportunities opened up by them. 360349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 361349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 362349cc55cSDimitry Andric 363349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 364349cc55cSDimitry Andric 365349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 366349cc55cSDimitry Andric C(FPM, Level); 367349cc55cSDimitry Andric 368349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 369349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 370349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 371349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 372349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 373349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 374349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 375349cc55cSDimitry Andric 376349cc55cSDimitry Andric return FPM; 377349cc55cSDimitry Andric } 378349cc55cSDimitry Andric 379349cc55cSDimitry Andric FunctionPassManager 380349cc55cSDimitry Andric PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, 381349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 382349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Must request optimizations!"); 383349cc55cSDimitry Andric 384349cc55cSDimitry Andric // The O1 pipeline has a separate pipeline creation function to simplify 385349cc55cSDimitry Andric // construction readability. 386349cc55cSDimitry Andric if (Level.getSpeedupLevel() == 1) 387349cc55cSDimitry Andric return buildO1FunctionSimplificationPipeline(Level, Phase); 388349cc55cSDimitry Andric 389349cc55cSDimitry Andric FunctionPassManager FPM; 390349cc55cSDimitry Andric 391349cc55cSDimitry Andric // Form SSA out of local memory accesses after breaking apart aggregates into 392349cc55cSDimitry Andric // scalars. 393349cc55cSDimitry Andric FPM.addPass(SROAPass()); 394349cc55cSDimitry Andric 395349cc55cSDimitry Andric // Catch trivial redundancies 396349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); 397349cc55cSDimitry Andric if (EnableKnowledgeRetention) 398349cc55cSDimitry Andric FPM.addPass(AssumeSimplifyPass()); 399349cc55cSDimitry Andric 400349cc55cSDimitry Andric // Hoisting of scalars and load expressions. 401349cc55cSDimitry Andric if (EnableGVNHoist) 402349cc55cSDimitry Andric FPM.addPass(GVNHoistPass()); 403349cc55cSDimitry Andric 404349cc55cSDimitry Andric // Global value numbering based sinking. 405349cc55cSDimitry Andric if (EnableGVNSink) { 406349cc55cSDimitry Andric FPM.addPass(GVNSinkPass()); 407349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 408349cc55cSDimitry Andric } 409349cc55cSDimitry Andric 410349cc55cSDimitry Andric if (EnableConstraintElimination) 411349cc55cSDimitry Andric FPM.addPass(ConstraintEliminationPass()); 412349cc55cSDimitry Andric 413349cc55cSDimitry Andric // Speculative execution if the target has divergent branches; otherwise nop. 414349cc55cSDimitry Andric FPM.addPass(SpeculativeExecutionPass(/* OnlyIfDivergentTarget =*/true)); 415349cc55cSDimitry Andric 416349cc55cSDimitry Andric // Optimize based on known information about branches, and cleanup afterward. 417349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 418349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 419349cc55cSDimitry Andric 420349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 421349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 422349cc55cSDimitry Andric FPM.addPass(AggressiveInstCombinePass()); 423349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 424349cc55cSDimitry Andric 425349cc55cSDimitry Andric if (!Level.isOptimizingForSize()) 426349cc55cSDimitry Andric FPM.addPass(LibCallsShrinkWrapPass()); 427349cc55cSDimitry Andric 428349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 429349cc55cSDimitry Andric 430349cc55cSDimitry Andric // For PGO use pipeline, try to optimize memory intrinsics such as memcpy 431349cc55cSDimitry Andric // using the size value profile. Don't perform this when optimizing for size. 432349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse && 433349cc55cSDimitry Andric !Level.isOptimizingForSize()) 434349cc55cSDimitry Andric FPM.addPass(PGOMemOPSizeOpt()); 435349cc55cSDimitry Andric 436349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 437349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 438349cc55cSDimitry Andric 439349cc55cSDimitry Andric // Form canonically associated expression trees, and simplify the trees using 440349cc55cSDimitry Andric // basic mathematical properties. For example, this will form (nearly) 441349cc55cSDimitry Andric // minimal multiplication trees. 442349cc55cSDimitry Andric FPM.addPass(ReassociatePass()); 443349cc55cSDimitry Andric 444349cc55cSDimitry Andric // Add the primary loop simplification pipeline. 445349cc55cSDimitry Andric // FIXME: Currently this is split into two loop pass pipelines because we run 446349cc55cSDimitry Andric // some function passes in between them. These can and should be removed 447349cc55cSDimitry Andric // and/or replaced by scheduling the loop pass equivalents in the correct 448349cc55cSDimitry Andric // positions. But those equivalent passes aren't powerful enough yet. 449349cc55cSDimitry Andric // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still 450349cc55cSDimitry Andric // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to 451349cc55cSDimitry Andric // fully replace `SimplifyCFGPass`, and the closest to the other we have is 452349cc55cSDimitry Andric // `LoopInstSimplify`. 453349cc55cSDimitry Andric LoopPassManager LPM1, LPM2; 454349cc55cSDimitry Andric 455349cc55cSDimitry Andric // Simplify the loop body. We do this initially to clean up after other loop 456349cc55cSDimitry Andric // passes run, either when iterating on a loop or on inner loops with 457349cc55cSDimitry Andric // implications on the outer loop. 458349cc55cSDimitry Andric LPM1.addPass(LoopInstSimplifyPass()); 459349cc55cSDimitry Andric LPM1.addPass(LoopSimplifyCFGPass()); 460349cc55cSDimitry Andric 461349cc55cSDimitry Andric // Try to remove as much code from the loop header as possible, 462349cc55cSDimitry Andric // to reduce amount of IR that will have to be duplicated. 463349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 464349cc55cSDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 465349cc55cSDimitry Andric 466349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 467349cc55cSDimitry Andric LPM1.addPass( 468349cc55cSDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz, isLTOPreLink(Phase))); 469349cc55cSDimitry Andric // TODO: Investigate promotion cap for O1. 470349cc55cSDimitry Andric LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 471349cc55cSDimitry Andric LPM1.addPass( 472349cc55cSDimitry Andric SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 && 473349cc55cSDimitry Andric EnableO3NonTrivialUnswitching)); 474349cc55cSDimitry Andric LPM2.addPass(LoopIdiomRecognizePass()); 475349cc55cSDimitry Andric LPM2.addPass(IndVarSimplifyPass()); 476349cc55cSDimitry Andric 477349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 478349cc55cSDimitry Andric C(LPM2, Level); 479349cc55cSDimitry Andric 480349cc55cSDimitry Andric LPM2.addPass(LoopDeletionPass()); 481349cc55cSDimitry Andric 482349cc55cSDimitry Andric if (EnableLoopInterchange) 483349cc55cSDimitry Andric LPM2.addPass(LoopInterchangePass()); 484349cc55cSDimitry Andric 485349cc55cSDimitry Andric // Do not enable unrolling in PreLinkThinLTO phase during sample PGO 486349cc55cSDimitry Andric // because it changes IR to makes profile annotation in back compile 487349cc55cSDimitry Andric // inaccurate. The normal unroller doesn't pay attention to forced full unroll 488349cc55cSDimitry Andric // attributes so we need to make sure and allow the full unroll pass to pay 489349cc55cSDimitry Andric // attention to it. 490349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink || !PGOOpt || 491349cc55cSDimitry Andric PGOOpt->Action != PGOOptions::SampleUse) 492349cc55cSDimitry Andric LPM2.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 493349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 494349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 495349cc55cSDimitry Andric 496349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 497349cc55cSDimitry Andric C(LPM2, Level); 498349cc55cSDimitry Andric 499349cc55cSDimitry Andric // We provide the opt remark emitter pass for LICM to use. We only need to do 500349cc55cSDimitry Andric // this once as it is immutable. 501349cc55cSDimitry Andric FPM.addPass( 502349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 503349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), 504349cc55cSDimitry Andric /*UseMemorySSA=*/true, 505349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 506349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 507349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 508349cc55cSDimitry Andric if (EnableLoopFlatten) 509349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 510349cc55cSDimitry Andric // The loop passes in LPM2 (LoopIdiomRecognizePass, IndVarSimplifyPass, 511349cc55cSDimitry Andric // LoopDeletionPass and LoopFullUnrollPass) do not preserve MemorySSA. 512349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 513349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), 514349cc55cSDimitry Andric /*UseMemorySSA=*/false, 515349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 516349cc55cSDimitry Andric 517349cc55cSDimitry Andric // Delete small array after loop unroll. 518349cc55cSDimitry Andric FPM.addPass(SROAPass()); 519349cc55cSDimitry Andric 520349cc55cSDimitry Andric // The matrix extension can introduce large vector operations early, which can 521349cc55cSDimitry Andric // benefit from running vector-combine early on. 522349cc55cSDimitry Andric if (EnableMatrix) 523349cc55cSDimitry Andric FPM.addPass(VectorCombinePass(/*ScalarizationOnly=*/true)); 524349cc55cSDimitry Andric 525349cc55cSDimitry Andric // Eliminate redundancies. 526349cc55cSDimitry Andric FPM.addPass(MergedLoadStoreMotionPass()); 527349cc55cSDimitry Andric if (RunNewGVN) 528349cc55cSDimitry Andric FPM.addPass(NewGVNPass()); 529349cc55cSDimitry Andric else 530349cc55cSDimitry Andric FPM.addPass(GVNPass()); 531349cc55cSDimitry Andric 532349cc55cSDimitry Andric // Sparse conditional constant propagation. 533349cc55cSDimitry Andric // FIXME: It isn't clear why we do this *after* loop passes rather than 534349cc55cSDimitry Andric // before... 535349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 536349cc55cSDimitry Andric 537349cc55cSDimitry Andric // Delete dead bit computations (instcombine runs after to fold away the dead 538349cc55cSDimitry Andric // computations, and then ADCE will run later to exploit any new DCE 539349cc55cSDimitry Andric // opportunities that creates). 540349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 541349cc55cSDimitry Andric 542349cc55cSDimitry Andric // Run instcombine after redundancy and dead bit elimination to exploit 543349cc55cSDimitry Andric // opportunities opened up by them. 544349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 545349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 546349cc55cSDimitry Andric 547349cc55cSDimitry Andric // Re-consider control flow based optimizations after redundancy elimination, 548349cc55cSDimitry Andric // redo DCE, etc. 549349cc55cSDimitry Andric if (EnableDFAJumpThreading && Level.getSizeLevel() == 0) 550349cc55cSDimitry Andric FPM.addPass(DFAJumpThreadingPass()); 551349cc55cSDimitry Andric 552349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass()); 553349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 554349cc55cSDimitry Andric 555349cc55cSDimitry Andric // Finally, do an expensive DCE pass to catch all the dead code exposed by 556349cc55cSDimitry Andric // the simplifications and basic cleanup after all the simplifications. 557349cc55cSDimitry Andric // TODO: Investigate if this is too expensive. 558349cc55cSDimitry Andric FPM.addPass(ADCEPass()); 559349cc55cSDimitry Andric 560349cc55cSDimitry Andric // Specially optimize memory movement as it doesn't look like dataflow in SSA. 561349cc55cSDimitry Andric FPM.addPass(MemCpyOptPass()); 562349cc55cSDimitry Andric 563349cc55cSDimitry Andric FPM.addPass(DSEPass()); 564349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 565349cc55cSDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 566349cc55cSDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 567349cc55cSDimitry Andric 568349cc55cSDimitry Andric FPM.addPass(CoroElidePass()); 569349cc55cSDimitry Andric 570349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 571349cc55cSDimitry Andric C(FPM, Level); 572349cc55cSDimitry Andric 573349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass( 574349cc55cSDimitry Andric SimplifyCFGOptions().hoistCommonInsts(true).sinkCommonInsts(true))); 575349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 576349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 577349cc55cSDimitry Andric 578349cc55cSDimitry Andric if (EnableCHR && Level == OptimizationLevel::O3 && PGOOpt && 579349cc55cSDimitry Andric (PGOOpt->Action == PGOOptions::IRUse || 580349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse)) 581349cc55cSDimitry Andric FPM.addPass(ControlHeightReductionPass()); 582349cc55cSDimitry Andric 583349cc55cSDimitry Andric return FPM; 584349cc55cSDimitry Andric } 585349cc55cSDimitry Andric 586349cc55cSDimitry Andric void PassBuilder::addRequiredLTOPreLinkPasses(ModulePassManager &MPM) { 587349cc55cSDimitry Andric MPM.addPass(CanonicalizeAliasesPass()); 588349cc55cSDimitry Andric MPM.addPass(NameAnonGlobalPass()); 589349cc55cSDimitry Andric } 590349cc55cSDimitry Andric 591349cc55cSDimitry Andric void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, 592349cc55cSDimitry Andric OptimizationLevel Level, bool RunProfileGen, 593349cc55cSDimitry Andric bool IsCS, std::string ProfileFile, 594349cc55cSDimitry Andric std::string ProfileRemappingFile) { 595349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && "Not expecting O0 here!"); 596349cc55cSDimitry Andric if (!IsCS && !DisablePreInliner) { 597349cc55cSDimitry Andric InlineParams IP; 598349cc55cSDimitry Andric 599349cc55cSDimitry Andric IP.DefaultThreshold = PreInlineThreshold; 600349cc55cSDimitry Andric 601349cc55cSDimitry Andric // FIXME: The hint threshold has the same value used by the regular inliner 602349cc55cSDimitry Andric // when not optimzing for size. This should probably be lowered after 603349cc55cSDimitry Andric // performance testing. 604349cc55cSDimitry Andric // FIXME: this comment is cargo culted from the old pass manager, revisit). 605349cc55cSDimitry Andric IP.HintThreshold = Level.isOptimizingForSize() ? PreInlineThreshold : 325; 606349cc55cSDimitry Andric ModuleInlinerWrapperPass MIWP(IP); 607349cc55cSDimitry Andric CGSCCPassManager &CGPipeline = MIWP.getPM(); 608349cc55cSDimitry Andric 609349cc55cSDimitry Andric FunctionPassManager FPM; 610349cc55cSDimitry Andric FPM.addPass(SROAPass()); 611349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies. 612349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks. 613349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); // Combine silly sequences. 614349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 615349cc55cSDimitry Andric 616349cc55cSDimitry Andric CGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 617349cc55cSDimitry Andric std::move(FPM), PTO.EagerlyInvalidateAnalyses)); 618349cc55cSDimitry Andric 619349cc55cSDimitry Andric MPM.addPass(std::move(MIWP)); 620349cc55cSDimitry Andric 621349cc55cSDimitry Andric // Delete anything that is now dead to make sure that we don't instrument 622349cc55cSDimitry Andric // dead code. Instrumentation can end up keeping dead code around and 623349cc55cSDimitry Andric // dramatically increase code size. 624349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 625349cc55cSDimitry Andric } 626349cc55cSDimitry Andric 627349cc55cSDimitry Andric if (!RunProfileGen) { 628349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 629349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 630349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 631349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 632349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 633349cc55cSDimitry Andric return; 634349cc55cSDimitry Andric } 635349cc55cSDimitry Andric 636349cc55cSDimitry Andric // Perform PGO instrumentation. 637349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 638349cc55cSDimitry Andric 639349cc55cSDimitry Andric FunctionPassManager FPM; 640349cc55cSDimitry Andric // Disable header duplication in loop rotation at -Oz. 641349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 642349cc55cSDimitry Andric LoopRotatePass(Level != OptimizationLevel::Oz), /*UseMemorySSA=*/false, 643349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/false)); 644349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM), 645349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 646349cc55cSDimitry Andric 647349cc55cSDimitry Andric // Add the profile lowering pass. 648349cc55cSDimitry Andric InstrProfOptions Options; 649349cc55cSDimitry Andric if (!ProfileFile.empty()) 650349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 651349cc55cSDimitry Andric // Do counter promotion at Level greater than O0. 652349cc55cSDimitry Andric Options.DoCounterPromotion = true; 653349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 654349cc55cSDimitry Andric MPM.addPass(InstrProfiling(Options, IsCS)); 655349cc55cSDimitry Andric } 656349cc55cSDimitry Andric 657349cc55cSDimitry Andric void PassBuilder::addPGOInstrPassesForO0(ModulePassManager &MPM, 658349cc55cSDimitry Andric bool RunProfileGen, bool IsCS, 659349cc55cSDimitry Andric std::string ProfileFile, 660349cc55cSDimitry Andric std::string ProfileRemappingFile) { 661349cc55cSDimitry Andric if (!RunProfileGen) { 662349cc55cSDimitry Andric assert(!ProfileFile.empty() && "Profile use expecting a profile file!"); 663349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); 664349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 665349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 666349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 667349cc55cSDimitry Andric return; 668349cc55cSDimitry Andric } 669349cc55cSDimitry Andric 670349cc55cSDimitry Andric // Perform PGO instrumentation. 671349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGen(IsCS)); 672349cc55cSDimitry Andric // Add the profile lowering pass. 673349cc55cSDimitry Andric InstrProfOptions Options; 674349cc55cSDimitry Andric if (!ProfileFile.empty()) 675349cc55cSDimitry Andric Options.InstrProfileOutput = ProfileFile; 676349cc55cSDimitry Andric // Do not do counter promotion at O0. 677349cc55cSDimitry Andric Options.DoCounterPromotion = false; 678349cc55cSDimitry Andric Options.UseBFIInPromotion = IsCS; 679349cc55cSDimitry Andric MPM.addPass(InstrProfiling(Options, IsCS)); 680349cc55cSDimitry Andric } 681349cc55cSDimitry Andric 682349cc55cSDimitry Andric static InlineParams getInlineParamsFromOptLevel(OptimizationLevel Level) { 683349cc55cSDimitry Andric return getInlineParams(Level.getSpeedupLevel(), Level.getSizeLevel()); 684349cc55cSDimitry Andric } 685349cc55cSDimitry Andric 686349cc55cSDimitry Andric ModuleInlinerWrapperPass 687349cc55cSDimitry Andric PassBuilder::buildInlinerPipeline(OptimizationLevel Level, 688349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 689349cc55cSDimitry Andric InlineParams IP = getInlineParamsFromOptLevel(Level); 690349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 691349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 692349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 693349cc55cSDimitry Andric 694349cc55cSDimitry Andric if (PGOOpt) 695349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 696349cc55cSDimitry Andric 697349cc55cSDimitry Andric ModuleInlinerWrapperPass MIWP(IP, PerformMandatoryInliningsFirst, 698349cc55cSDimitry Andric UseInlineAdvisor, MaxDevirtIterations); 699349cc55cSDimitry Andric 700349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 701349cc55cSDimitry Andric // the CGSCC pipeline. 702349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>()); 703349cc55cSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly available 704349cc55cSDimitry Andric // GlobalsAA. 705349cc55cSDimitry Andric MIWP.addModulePass( 706349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 707349cc55cSDimitry Andric 708349cc55cSDimitry Andric // Require the ProfileSummaryAnalysis for the module so we can query it within 709349cc55cSDimitry Andric // the inliner pass. 710349cc55cSDimitry Andric MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 711349cc55cSDimitry Andric 712349cc55cSDimitry Andric // Now begin the main postorder CGSCC pipeline. 713349cc55cSDimitry Andric // FIXME: The current CGSCC pipeline has its origins in the legacy pass 714349cc55cSDimitry Andric // manager and trying to emulate its precise behavior. Much of this doesn't 715349cc55cSDimitry Andric // make a lot of sense and we should revisit the core CGSCC structure. 716349cc55cSDimitry Andric CGSCCPassManager &MainCGPipeline = MIWP.getPM(); 717349cc55cSDimitry Andric 718349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 719349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 720349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 721349cc55cSDimitry Andric // invoke or a call. 722349cc55cSDimitry Andric 723349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::CGSCC) 724349cc55cSDimitry Andric MainCGPipeline.addPass(AttributorCGSCCPass()); 725349cc55cSDimitry Andric 726349cc55cSDimitry Andric // Now deduce any function attributes based in the current code. 727349cc55cSDimitry Andric MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); 728349cc55cSDimitry Andric 729349cc55cSDimitry Andric // When at O3 add argument promotion to the pass pipeline. 730349cc55cSDimitry Andric // FIXME: It isn't at all clear why this should be limited to O3. 731349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 732349cc55cSDimitry Andric MainCGPipeline.addPass(ArgumentPromotionPass()); 733349cc55cSDimitry Andric 734349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations. This is a (quick!) no-op if 735349cc55cSDimitry Andric // there are no OpenMP runtime calls present in the module. 736349cc55cSDimitry Andric if (Level == OptimizationLevel::O2 || Level == OptimizationLevel::O3) 737349cc55cSDimitry Andric MainCGPipeline.addPass(OpenMPOptCGSCCPass()); 738349cc55cSDimitry Andric 739349cc55cSDimitry Andric for (auto &C : CGSCCOptimizerLateEPCallbacks) 740349cc55cSDimitry Andric C(MainCGPipeline, Level); 741349cc55cSDimitry Andric 742349cc55cSDimitry Andric // Lastly, add the core function simplification pipeline nested inside the 743349cc55cSDimitry Andric // CGSCC walk. 744349cc55cSDimitry Andric MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( 745349cc55cSDimitry Andric buildFunctionSimplificationPipeline(Level, Phase), 746349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses, EnableNoRerunSimplificationPipeline)); 747349cc55cSDimitry Andric 748349cc55cSDimitry Andric MainCGPipeline.addPass(CoroSplitPass(Level != OptimizationLevel::O0)); 749349cc55cSDimitry Andric 750349cc55cSDimitry Andric if (EnableNoRerunSimplificationPipeline) 751349cc55cSDimitry Andric MIWP.addLateModulePass(createModuleToFunctionPassAdaptor( 752349cc55cSDimitry Andric InvalidateAnalysisPass<ShouldNotRunFunctionPassesAnalysis>())); 753349cc55cSDimitry Andric 754349cc55cSDimitry Andric return MIWP; 755349cc55cSDimitry Andric } 756349cc55cSDimitry Andric 757349cc55cSDimitry Andric ModuleInlinerPass 758349cc55cSDimitry Andric PassBuilder::buildModuleInlinerPipeline(OptimizationLevel Level, 759349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 760349cc55cSDimitry Andric InlineParams IP = getInlineParamsFromOptLevel(Level); 761349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPreLink && PGOOpt && 762349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 763349cc55cSDimitry Andric IP.HotCallSiteThreshold = 0; 764349cc55cSDimitry Andric 765349cc55cSDimitry Andric if (PGOOpt) 766349cc55cSDimitry Andric IP.EnableDeferral = EnablePGOInlineDeferral; 767349cc55cSDimitry Andric 768349cc55cSDimitry Andric // The inline deferral logic is used to avoid losing some 769349cc55cSDimitry Andric // inlining chance in future. It is helpful in SCC inliner, in which 770349cc55cSDimitry Andric // inlining is processed in bottom-up order. 771349cc55cSDimitry Andric // While in module inliner, the inlining order is a priority-based order 772349cc55cSDimitry Andric // by default. The inline deferral is unnecessary there. So we disable the 773349cc55cSDimitry Andric // inline deferral logic in module inliner. 774349cc55cSDimitry Andric IP.EnableDeferral = false; 775349cc55cSDimitry Andric 776349cc55cSDimitry Andric return ModuleInlinerPass(IP, UseInlineAdvisor); 777349cc55cSDimitry Andric } 778349cc55cSDimitry Andric 779349cc55cSDimitry Andric ModulePassManager 780349cc55cSDimitry Andric PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, 781349cc55cSDimitry Andric ThinOrFullLTOPhase Phase) { 782349cc55cSDimitry Andric ModulePassManager MPM; 783349cc55cSDimitry Andric 784349cc55cSDimitry Andric // Place pseudo probe instrumentation as the first pass of the pipeline to 785349cc55cSDimitry Andric // minimize the impact of optimization changes. 786349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 787349cc55cSDimitry Andric Phase != ThinOrFullLTOPhase::ThinLTOPostLink) 788349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 789349cc55cSDimitry Andric 790349cc55cSDimitry Andric bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse); 791349cc55cSDimitry Andric 792349cc55cSDimitry Andric // In ThinLTO mode, when flattened profile is used, all the available 793349cc55cSDimitry Andric // profile information will be annotated in PreLink phase so there is 794349cc55cSDimitry Andric // no need to load the profile again in PostLink. 795349cc55cSDimitry Andric bool LoadSampleProfile = 796349cc55cSDimitry Andric HasSampleProfile && 797349cc55cSDimitry Andric !(FlattenedProfileUsed && Phase == ThinOrFullLTOPhase::ThinLTOPostLink); 798349cc55cSDimitry Andric 799349cc55cSDimitry Andric // During the ThinLTO backend phase we perform early indirect call promotion 800349cc55cSDimitry Andric // here, before globalopt. Otherwise imported available_externally functions 801349cc55cSDimitry Andric // look unreferenced and are removed. If we are going to load the sample 802349cc55cSDimitry Andric // profile then defer until later. 803349cc55cSDimitry Andric // TODO: See if we can move later and consolidate with the location where 804349cc55cSDimitry Andric // we perform ICP when we are loading a sample profile. 805349cc55cSDimitry Andric // TODO: We pass HasSampleProfile (whether there was a sample profile file 806349cc55cSDimitry Andric // passed to the compile) to the SamplePGO flag of ICP. This is used to 807349cc55cSDimitry Andric // determine whether the new direct calls are annotated with prof metadata. 808349cc55cSDimitry Andric // Ideally this should be determined from whether the IR is annotated with 809349cc55cSDimitry Andric // sample profile, and not whether the a sample profile was provided on the 810349cc55cSDimitry Andric // command line. E.g. for flattened profiles where we will not be reloading 811349cc55cSDimitry Andric // the sample profile in the ThinLTO backend, we ideally shouldn't have to 812349cc55cSDimitry Andric // provide the sample profile file. 813349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink && !LoadSampleProfile) 814349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile)); 815349cc55cSDimitry Andric 816349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 817349cc55cSDimitry Andric // libraries and other oracles. 818349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 819349cc55cSDimitry Andric 820349cc55cSDimitry Andric // Create an early function pass manager to cleanup the output of the 821349cc55cSDimitry Andric // frontend. 822349cc55cSDimitry Andric FunctionPassManager EarlyFPM; 823349cc55cSDimitry Andric // Lower llvm.expect to metadata before attempting transforms. 824349cc55cSDimitry Andric // Compare/branch metadata may alter the behavior of passes like SimplifyCFG. 825349cc55cSDimitry Andric EarlyFPM.addPass(LowerExpectIntrinsicPass()); 826349cc55cSDimitry Andric EarlyFPM.addPass(SimplifyCFGPass()); 827349cc55cSDimitry Andric EarlyFPM.addPass(SROAPass()); 828349cc55cSDimitry Andric EarlyFPM.addPass(EarlyCSEPass()); 829349cc55cSDimitry Andric EarlyFPM.addPass(CoroEarlyPass()); 830349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 831349cc55cSDimitry Andric EarlyFPM.addPass(CallSiteSplittingPass()); 832349cc55cSDimitry Andric 833349cc55cSDimitry Andric // In SamplePGO ThinLTO backend, we need instcombine before profile annotation 834349cc55cSDimitry Andric // to convert bitcast to direct calls so that they can be inlined during the 835349cc55cSDimitry Andric // profile annotation prepration step. 836349cc55cSDimitry Andric // More details about SamplePGO design can be found in: 837349cc55cSDimitry Andric // https://research.google.com/pubs/pub45290.html 838349cc55cSDimitry Andric // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. 839349cc55cSDimitry Andric if (LoadSampleProfile) 840349cc55cSDimitry Andric EarlyFPM.addPass(InstCombinePass()); 841349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM), 842349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 843349cc55cSDimitry Andric 844349cc55cSDimitry Andric if (LoadSampleProfile) { 845349cc55cSDimitry Andric // Annotate sample profile right after early FPM to ensure freshness of 846349cc55cSDimitry Andric // the debug info. 847349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 848349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, Phase)); 849349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 850349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 851349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 852349cc55cSDimitry Andric // Do not invoke ICP in the LTOPrelink phase as it makes it hard 853349cc55cSDimitry Andric // for the profile annotation to be accurate in the LTO backend. 854349cc55cSDimitry Andric if (Phase != ThinOrFullLTOPhase::ThinLTOPreLink && 855349cc55cSDimitry Andric Phase != ThinOrFullLTOPhase::FullLTOPreLink) 856349cc55cSDimitry Andric // We perform early indirect call promotion here, before globalopt. 857349cc55cSDimitry Andric // This is important for the ThinLTO backend phase because otherwise 858349cc55cSDimitry Andric // imported available_externally functions look unreferenced and are 859349cc55cSDimitry Andric // removed. 860349cc55cSDimitry Andric MPM.addPass( 861349cc55cSDimitry Andric PGOIndirectCallPromotion(true /* IsInLTO */, true /* SamplePGO */)); 862349cc55cSDimitry Andric } 863349cc55cSDimitry Andric 864349cc55cSDimitry Andric // Try to perform OpenMP specific optimizations on the module. This is a 865349cc55cSDimitry Andric // (quick!) no-op if there are no OpenMP runtime calls present in the module. 866349cc55cSDimitry Andric if (Level != OptimizationLevel::O0) 867349cc55cSDimitry Andric MPM.addPass(OpenMPOptPass()); 868349cc55cSDimitry Andric 869349cc55cSDimitry Andric if (AttributorRun & AttributorRunOption::MODULE) 870349cc55cSDimitry Andric MPM.addPass(AttributorPass()); 871349cc55cSDimitry Andric 872349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic in the ThinLTO 873349cc55cSDimitry Andric // post link pipeline after ICP. This is to enable usage of the type 874349cc55cSDimitry Andric // tests in ICP sequences. 875349cc55cSDimitry Andric if (Phase == ThinOrFullLTOPhase::ThinLTOPostLink) 876349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 877349cc55cSDimitry Andric 878349cc55cSDimitry Andric for (auto &C : PipelineEarlySimplificationEPCallbacks) 879349cc55cSDimitry Andric C(MPM, Level); 880349cc55cSDimitry Andric 881349cc55cSDimitry Andric // Specialize functions with IPSCCP. 882349cc55cSDimitry Andric if (EnableFunctionSpecialization && Level == OptimizationLevel::O3) 883349cc55cSDimitry Andric MPM.addPass(FunctionSpecializationPass()); 884349cc55cSDimitry Andric 885349cc55cSDimitry Andric // Interprocedural constant propagation now that basic cleanup has occurred 886349cc55cSDimitry Andric // and prior to optimizing globals. 887349cc55cSDimitry Andric // FIXME: This position in the pipeline hasn't been carefully considered in 888349cc55cSDimitry Andric // years, it should be re-analyzed. 889349cc55cSDimitry Andric MPM.addPass(IPSCCPPass()); 890349cc55cSDimitry Andric 891349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 892349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 893349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 894349cc55cSDimitry Andric 895349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 896349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 897349cc55cSDimitry Andric 898349cc55cSDimitry Andric // Promote any localized globals to SSA registers. 899349cc55cSDimitry Andric // FIXME: Should this instead by a run of SROA? 900349cc55cSDimitry Andric // FIXME: We should probably run instcombine and simplifycfg afterward to 901349cc55cSDimitry Andric // delete control flows that are dead once globals have been folded to 902349cc55cSDimitry Andric // constants. 903349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 904349cc55cSDimitry Andric 905349cc55cSDimitry Andric // Remove any dead arguments exposed by cleanups and constant folding 906349cc55cSDimitry Andric // globals. 907349cc55cSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 908349cc55cSDimitry Andric 909349cc55cSDimitry Andric // Create a small function pass pipeline to cleanup after all the global 910349cc55cSDimitry Andric // optimizations. 911349cc55cSDimitry Andric FunctionPassManager GlobalCleanupPM; 912349cc55cSDimitry Andric GlobalCleanupPM.addPass(InstCombinePass()); 913349cc55cSDimitry Andric invokePeepholeEPCallbacks(GlobalCleanupPM, Level); 914349cc55cSDimitry Andric 915349cc55cSDimitry Andric GlobalCleanupPM.addPass(SimplifyCFGPass()); 916349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM), 917349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 918349cc55cSDimitry Andric 919349cc55cSDimitry Andric // Add all the requested passes for instrumentation PGO, if requested. 920349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 921349cc55cSDimitry Andric (PGOOpt->Action == PGOOptions::IRInstr || 922349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) { 923349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, 924349cc55cSDimitry Andric /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr, 925349cc55cSDimitry Andric /* IsCS */ false, PGOOpt->ProfileFile, 926349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile); 927349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion(false, false)); 928349cc55cSDimitry Andric } 929349cc55cSDimitry Andric if (PGOOpt && Phase != ThinOrFullLTOPhase::ThinLTOPostLink && 930349cc55cSDimitry Andric PGOOpt->CSAction == PGOOptions::CSIRInstr) 931349cc55cSDimitry Andric MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); 932349cc55cSDimitry Andric 933349cc55cSDimitry Andric // Synthesize function entry counts for non-PGO compilation. 934349cc55cSDimitry Andric if (EnableSyntheticCounts && !PGOOpt) 935349cc55cSDimitry Andric MPM.addPass(SyntheticCountsPropagation()); 936349cc55cSDimitry Andric 937349cc55cSDimitry Andric if (EnableModuleInliner) 938349cc55cSDimitry Andric MPM.addPass(buildModuleInlinerPipeline(Level, Phase)); 939349cc55cSDimitry Andric else 940349cc55cSDimitry Andric MPM.addPass(buildInlinerPipeline(Level, Phase)); 941349cc55cSDimitry Andric 942349cc55cSDimitry Andric if (EnableMemProfiler && Phase != ThinOrFullLTOPhase::ThinLTOPreLink) { 943349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass())); 944349cc55cSDimitry Andric MPM.addPass(ModuleMemProfilerPass()); 945349cc55cSDimitry Andric } 946349cc55cSDimitry Andric 947349cc55cSDimitry Andric return MPM; 948349cc55cSDimitry Andric } 949349cc55cSDimitry Andric 950349cc55cSDimitry Andric /// TODO: Should LTO cause any differences to this set of passes? 951349cc55cSDimitry Andric void PassBuilder::addVectorPasses(OptimizationLevel Level, 952349cc55cSDimitry Andric FunctionPassManager &FPM, bool IsFullLTO) { 953349cc55cSDimitry Andric FPM.addPass(LoopVectorizePass( 954349cc55cSDimitry Andric LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization))); 955349cc55cSDimitry Andric 956349cc55cSDimitry Andric if (IsFullLTO) { 957349cc55cSDimitry Andric // The vectorizer may have significantly shortened a loop body; unroll 958349cc55cSDimitry Andric // again. Unroll small loops to hide loop backedge latency and saturate any 959349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 960349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 961349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 962349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 963349cc55cSDimitry Andric // across the loop nests. 964349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 965349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) 966349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 967349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 968349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 969349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 970349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 971349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 972349cc55cSDimitry Andric } 973349cc55cSDimitry Andric 974349cc55cSDimitry Andric if (!IsFullLTO) { 975349cc55cSDimitry Andric // Eliminate loads by forwarding stores from the previous iteration to loads 976349cc55cSDimitry Andric // of the current iteration. 977349cc55cSDimitry Andric FPM.addPass(LoopLoadEliminationPass()); 978349cc55cSDimitry Andric } 979349cc55cSDimitry Andric // Cleanup after the loop optimization passes. 980349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 981349cc55cSDimitry Andric 982349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 983349cc55cSDimitry Andric // At higher optimization levels, try to clean up any runtime overlap and 984349cc55cSDimitry Andric // alignment checks inserted by the vectorizer. We want to track correlated 985349cc55cSDimitry Andric // runtime checks for two inner loops in the same outer loop, fold any 986349cc55cSDimitry Andric // common computations, hoist loop-invariant aspects out of any outer loop, 987349cc55cSDimitry Andric // and unswitch the runtime checks if possible. Once hoisted, we may have 988349cc55cSDimitry Andric // dead (or speculatable) control flows or more combining opportunities. 989349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); 990349cc55cSDimitry Andric FPM.addPass(CorrelatedValuePropagationPass()); 991349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 992349cc55cSDimitry Andric LoopPassManager LPM; 993349cc55cSDimitry Andric LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap)); 994349cc55cSDimitry Andric LPM.addPass(SimpleLoopUnswitchPass(/* NonTrivial */ Level == 995349cc55cSDimitry Andric OptimizationLevel::O3)); 996349cc55cSDimitry Andric FPM.addPass( 997349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 998349cc55cSDimitry Andric FPM.addPass( 999349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM), /*UseMemorySSA=*/true, 1000349cc55cSDimitry Andric /*UseBlockFrequencyInfo=*/true)); 1001349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass()); 1002349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1003349cc55cSDimitry Andric } 1004349cc55cSDimitry Andric 1005349cc55cSDimitry Andric // Now that we've formed fast to execute loop structures, we do further 1006349cc55cSDimitry Andric // optimizations. These are run afterward as they might block doing complex 1007349cc55cSDimitry Andric // analyses and transforms such as what are needed for loop vectorization. 1008349cc55cSDimitry Andric 1009349cc55cSDimitry Andric // Cleanup after loop vectorization, etc. Simplification passes like CVP and 1010349cc55cSDimitry Andric // GVN, loop transforms, and others have already run, so it's now better to 1011349cc55cSDimitry Andric // convert to more optimized IR using more aggressive simplify CFG options. 1012349cc55cSDimitry Andric // The extra sinking transform can create larger basic blocks, so do this 1013349cc55cSDimitry Andric // before SLP vectorization. 1014349cc55cSDimitry Andric FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions() 1015349cc55cSDimitry Andric .forwardSwitchCondToPhi(true) 1016349cc55cSDimitry Andric .convertSwitchToLookupTable(true) 1017349cc55cSDimitry Andric .needCanonicalLoops(false) 1018349cc55cSDimitry Andric .hoistCommonInsts(true) 1019349cc55cSDimitry Andric .sinkCommonInsts(true))); 1020349cc55cSDimitry Andric 1021349cc55cSDimitry Andric if (IsFullLTO) { 1022349cc55cSDimitry Andric FPM.addPass(SCCPPass()); 1023349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1024349cc55cSDimitry Andric FPM.addPass(BDCEPass()); 1025349cc55cSDimitry Andric } 1026349cc55cSDimitry Andric 1027349cc55cSDimitry Andric // Optimize parallel scalar instruction chains into SIMD instructions. 1028349cc55cSDimitry Andric if (PTO.SLPVectorization) { 1029349cc55cSDimitry Andric FPM.addPass(SLPVectorizerPass()); 1030349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) { 1031349cc55cSDimitry Andric FPM.addPass(EarlyCSEPass()); 1032349cc55cSDimitry Andric } 1033349cc55cSDimitry Andric } 1034349cc55cSDimitry Andric // Enhance/cleanup vector code. 1035349cc55cSDimitry Andric FPM.addPass(VectorCombinePass()); 1036349cc55cSDimitry Andric 1037349cc55cSDimitry Andric if (!IsFullLTO) { 1038349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1039349cc55cSDimitry Andric // Unroll small loops to hide loop backedge latency and saturate any 1040349cc55cSDimitry Andric // parallel execution resources of an out-of-order processor. We also then 1041349cc55cSDimitry Andric // need to clean up redundancies and loop invariant code. 1042349cc55cSDimitry Andric // FIXME: It would be really good to use a loop-integrated instruction 1043349cc55cSDimitry Andric // combiner for cleanup here so that the unrolling and LICM can be pipelined 1044349cc55cSDimitry Andric // across the loop nests. 1045349cc55cSDimitry Andric // We do UnrollAndJam in a separate LPM to ensure it happens before unroll 1046349cc55cSDimitry Andric if (EnableUnrollAndJam && PTO.LoopUnrolling) { 1047349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1048349cc55cSDimitry Andric LoopUnrollAndJamPass(Level.getSpeedupLevel()))); 1049349cc55cSDimitry Andric } 1050349cc55cSDimitry Andric FPM.addPass(LoopUnrollPass(LoopUnrollOptions( 1051349cc55cSDimitry Andric Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling, 1052349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll))); 1053349cc55cSDimitry Andric FPM.addPass(WarnMissedTransformationsPass()); 1054349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1055349cc55cSDimitry Andric FPM.addPass( 1056349cc55cSDimitry Andric RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); 1057349cc55cSDimitry Andric FPM.addPass(createFunctionToLoopPassAdaptor( 1058349cc55cSDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 1059349cc55cSDimitry Andric /*UseMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 1060349cc55cSDimitry Andric } 1061349cc55cSDimitry Andric 1062349cc55cSDimitry Andric // Now that we've vectorized and unrolled loops, we may have more refined 1063349cc55cSDimitry Andric // alignment information, try to re-derive it here. 1064349cc55cSDimitry Andric FPM.addPass(AlignmentFromAssumptionsPass()); 1065349cc55cSDimitry Andric 1066349cc55cSDimitry Andric if (IsFullLTO) 1067349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1068349cc55cSDimitry Andric } 1069349cc55cSDimitry Andric 1070349cc55cSDimitry Andric ModulePassManager 1071349cc55cSDimitry Andric PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level, 1072349cc55cSDimitry Andric bool LTOPreLink) { 1073349cc55cSDimitry Andric ModulePassManager MPM; 1074349cc55cSDimitry Andric 1075349cc55cSDimitry Andric // Optimize globals now that the module is fully simplified. 1076349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1077349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1078349cc55cSDimitry Andric 1079349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1080349cc55cSDimitry Andric // large bodies. 1081349cc55cSDimitry Andric if (RunPartialInlining) 1082349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1083349cc55cSDimitry Andric 1084349cc55cSDimitry Andric // Remove avail extern fns and globals definitions since we aren't compiling 1085349cc55cSDimitry Andric // an object file for later LTO. For LTO we want to preserve these so they 1086349cc55cSDimitry Andric // are eligible for inlining at link-time. Note if they are unreferenced they 1087349cc55cSDimitry Andric // will be removed by GlobalDCE later, so this only impacts referenced 1088349cc55cSDimitry Andric // available externally globals. Eventually they will be suppressed during 1089349cc55cSDimitry Andric // codegen, but eliminating here enables more opportunity for GlobalDCE as it 1090349cc55cSDimitry Andric // may make globals referenced by available external functions dead and saves 1091349cc55cSDimitry Andric // running remaining passes on the eliminated functions. These should be 1092349cc55cSDimitry Andric // preserved during prelinking for link-time inlining decisions. 1093349cc55cSDimitry Andric if (!LTOPreLink) 1094349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1095349cc55cSDimitry Andric 1096349cc55cSDimitry Andric if (EnableOrderFileInstrumentation) 1097349cc55cSDimitry Andric MPM.addPass(InstrOrderFilePass()); 1098349cc55cSDimitry Andric 1099349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1100349cc55cSDimitry Andric // attributes where applicable. 1101349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1102349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1103349cc55cSDimitry Andric 1104349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1105349cc55cSDimitry Andric // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as 1106349cc55cSDimitry Andric // cross-module inline has not been done yet. The context sensitive 1107349cc55cSDimitry Andric // instrumentation is after all the inlines are done. 1108349cc55cSDimitry Andric if (!LTOPreLink && PGOOpt) { 1109349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1110349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1111349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->CSProfileGenFile, 1112349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile); 1113349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1114349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1115349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->ProfileFile, 1116349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile); 1117349cc55cSDimitry Andric } 1118349cc55cSDimitry Andric 1119349cc55cSDimitry Andric // Re-require GloblasAA here prior to function passes. This is particularly 1120349cc55cSDimitry Andric // useful as the above will have inlined, DCE'ed, and function-attr 1121349cc55cSDimitry Andric // propagated everything. We should at this point have a reasonably minimal 1122349cc55cSDimitry Andric // and richly annotated call graph. By computing aliasing and mod/ref 1123349cc55cSDimitry Andric // information for all local globals here, the late loop passes and notably 1124349cc55cSDimitry Andric // the vectorizer will be able to use them to help recognize vectorizable 1125349cc55cSDimitry Andric // memory operations. 1126349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1127349cc55cSDimitry Andric 1128349cc55cSDimitry Andric FunctionPassManager OptimizePM; 1129349cc55cSDimitry Andric OptimizePM.addPass(Float2IntPass()); 1130349cc55cSDimitry Andric OptimizePM.addPass(LowerConstantIntrinsicsPass()); 1131349cc55cSDimitry Andric 1132349cc55cSDimitry Andric if (EnableMatrix) { 1133349cc55cSDimitry Andric OptimizePM.addPass(LowerMatrixIntrinsicsPass()); 1134349cc55cSDimitry Andric OptimizePM.addPass(EarlyCSEPass()); 1135349cc55cSDimitry Andric } 1136349cc55cSDimitry Andric 1137349cc55cSDimitry Andric // FIXME: We need to run some loop optimizations to re-rotate loops after 1138349cc55cSDimitry Andric // simplifycfg and others undo their rotation. 1139349cc55cSDimitry Andric 1140349cc55cSDimitry Andric // Optimize the loop execution. These passes operate on entire loop nests 1141349cc55cSDimitry Andric // rather than on each loop in an inside-out manner, and so they are actually 1142349cc55cSDimitry Andric // function passes. 1143349cc55cSDimitry Andric 1144349cc55cSDimitry Andric for (auto &C : VectorizerStartEPCallbacks) 1145349cc55cSDimitry Andric C(OptimizePM, Level); 1146349cc55cSDimitry Andric 1147349cc55cSDimitry Andric LoopPassManager LPM; 1148349cc55cSDimitry Andric // First rotate loops that may have been un-rotated by prior passes. 1149349cc55cSDimitry Andric // Disable header duplication at -Oz. 1150349cc55cSDimitry Andric LPM.addPass(LoopRotatePass(Level != OptimizationLevel::Oz, LTOPreLink)); 1151349cc55cSDimitry Andric // Some loops may have become dead by now. Try to delete them. 1152349cc55cSDimitry Andric // FIXME: see disscussion in https://reviews.llvm.org/D112851 1153349cc55cSDimitry Andric // this may need to be revisited once GVN is more powerful. 1154349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1155349cc55cSDimitry Andric OptimizePM.addPass(createFunctionToLoopPassAdaptor( 1156349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/false)); 1157349cc55cSDimitry Andric 1158349cc55cSDimitry Andric // Distribute loops to allow partial vectorization. I.e. isolate dependences 1159349cc55cSDimitry Andric // into separate loop that would otherwise inhibit vectorization. This is 1160349cc55cSDimitry Andric // currently only performed for loops marked with the metadata 1161349cc55cSDimitry Andric // llvm.loop.distribute=true or when -enable-loop-distribute is specified. 1162349cc55cSDimitry Andric OptimizePM.addPass(LoopDistributePass()); 1163349cc55cSDimitry Andric 1164349cc55cSDimitry Andric // Populates the VFABI attribute with the scalar-to-vector mappings 1165349cc55cSDimitry Andric // from the TargetLibraryInfo. 1166349cc55cSDimitry Andric OptimizePM.addPass(InjectTLIMappings()); 1167349cc55cSDimitry Andric 1168349cc55cSDimitry Andric addVectorPasses(Level, OptimizePM, /* IsFullLTO */ false); 1169349cc55cSDimitry Andric 1170349cc55cSDimitry Andric // Split out cold code. Splitting is done late to avoid hiding context from 1171349cc55cSDimitry Andric // other optimizations and inadvertently regressing performance. The tradeoff 1172349cc55cSDimitry Andric // is that this has a higher code size cost than splitting early. 1173349cc55cSDimitry Andric if (EnableHotColdSplit && !LTOPreLink) 1174349cc55cSDimitry Andric MPM.addPass(HotColdSplittingPass()); 1175349cc55cSDimitry Andric 1176349cc55cSDimitry Andric // Search the code for similar regions of code. If enough similar regions can 1177349cc55cSDimitry Andric // be found where extracting the regions into their own function will decrease 1178349cc55cSDimitry Andric // the size of the program, we extract the regions, a deduplicate the 1179349cc55cSDimitry Andric // structurally similar regions. 1180349cc55cSDimitry Andric if (EnableIROutliner) 1181349cc55cSDimitry Andric MPM.addPass(IROutlinerPass()); 1182349cc55cSDimitry Andric 1183349cc55cSDimitry Andric // Merge functions if requested. 1184349cc55cSDimitry Andric if (PTO.MergeFunctions) 1185349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1186349cc55cSDimitry Andric 1187349cc55cSDimitry Andric // LoopSink pass sinks instructions hoisted by LICM, which serves as a 1188349cc55cSDimitry Andric // canonicalization pass that enables other optimizations. As a result, 1189349cc55cSDimitry Andric // LoopSink pass needs to be a very late IR pass to avoid undoing LICM 1190349cc55cSDimitry Andric // result too early. 1191349cc55cSDimitry Andric OptimizePM.addPass(LoopSinkPass()); 1192349cc55cSDimitry Andric 1193349cc55cSDimitry Andric // And finally clean up LCSSA form before generating code. 1194349cc55cSDimitry Andric OptimizePM.addPass(InstSimplifyPass()); 1195349cc55cSDimitry Andric 1196349cc55cSDimitry Andric // This hoists/decomposes div/rem ops. It should run after other sink/hoist 1197349cc55cSDimitry Andric // passes to avoid re-sinking, but before SimplifyCFG because it can allow 1198349cc55cSDimitry Andric // flattening of blocks. 1199349cc55cSDimitry Andric OptimizePM.addPass(DivRemPairsPass()); 1200349cc55cSDimitry Andric 1201349cc55cSDimitry Andric // LoopSink (and other loop passes since the last simplifyCFG) might have 1202349cc55cSDimitry Andric // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. 1203349cc55cSDimitry Andric OptimizePM.addPass(SimplifyCFGPass()); 1204349cc55cSDimitry Andric 1205349cc55cSDimitry Andric OptimizePM.addPass(CoroCleanupPass()); 1206349cc55cSDimitry Andric 1207349cc55cSDimitry Andric // Add the core optimizing pipeline. 1208349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM), 1209349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1210349cc55cSDimitry Andric 1211349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1212349cc55cSDimitry Andric C(MPM, Level); 1213349cc55cSDimitry Andric 1214349cc55cSDimitry Andric if (PTO.CallGraphProfile) 1215349cc55cSDimitry Andric MPM.addPass(CGProfilePass()); 1216349cc55cSDimitry Andric 1217349cc55cSDimitry Andric // Now we need to do some global optimization transforms. 1218349cc55cSDimitry Andric // FIXME: It would seem like these should come first in the optimization 1219349cc55cSDimitry Andric // pipeline and maybe be the bottom of the canonicalization pipeline? Weird 1220349cc55cSDimitry Andric // ordering here. 1221349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1222349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1223349cc55cSDimitry Andric 1224349cc55cSDimitry Andric // TODO: Relative look table converter pass caused an issue when full lto is 1225349cc55cSDimitry Andric // enabled. See https://reviews.llvm.org/D94355 for more details. 1226349cc55cSDimitry Andric // Until the issue fixed, disable this pass during pre-linking phase. 1227349cc55cSDimitry Andric if (!LTOPreLink) 1228349cc55cSDimitry Andric MPM.addPass(RelLookupTableConverterPass()); 1229349cc55cSDimitry Andric 1230349cc55cSDimitry Andric return MPM; 1231349cc55cSDimitry Andric } 1232349cc55cSDimitry Andric 1233349cc55cSDimitry Andric ModulePassManager 1234349cc55cSDimitry Andric PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, 1235349cc55cSDimitry Andric bool LTOPreLink) { 1236349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1237349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1238349cc55cSDimitry Andric 1239349cc55cSDimitry Andric ModulePassManager MPM; 1240349cc55cSDimitry Andric 1241349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1242349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1243349cc55cSDimitry Andric 1244349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1245349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1246349cc55cSDimitry Andric 1247349cc55cSDimitry Andric // Apply module pipeline start EP callback. 1248349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1249349cc55cSDimitry Andric C(MPM, Level); 1250349cc55cSDimitry Andric 1251349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1252349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1253349cc55cSDimitry Andric 1254349cc55cSDimitry Andric // Add the core simplification pipeline. 1255349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1256349cc55cSDimitry Andric Level, LTOPreLink ? ThinOrFullLTOPhase::FullLTOPreLink 1257349cc55cSDimitry Andric : ThinOrFullLTOPhase::None)); 1258349cc55cSDimitry Andric 1259349cc55cSDimitry Andric // Now add the optimization pipeline. 1260349cc55cSDimitry Andric MPM.addPass(buildModuleOptimizationPipeline(Level, LTOPreLink)); 1261349cc55cSDimitry Andric 1262349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1263349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1264349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1265349cc55cSDimitry Andric 1266349cc55cSDimitry Andric // Emit annotation remarks. 1267349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1268349cc55cSDimitry Andric 1269349cc55cSDimitry Andric if (LTOPreLink) 1270349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1271349cc55cSDimitry Andric 1272349cc55cSDimitry Andric return MPM; 1273349cc55cSDimitry Andric } 1274349cc55cSDimitry Andric 1275349cc55cSDimitry Andric ModulePassManager 1276349cc55cSDimitry Andric PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1277349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1278349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1279349cc55cSDimitry Andric 1280349cc55cSDimitry Andric ModulePassManager MPM; 1281349cc55cSDimitry Andric 1282349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1283349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1284349cc55cSDimitry Andric 1285349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1286349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1287349cc55cSDimitry Andric 1288349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1289349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1290349cc55cSDimitry Andric 1291349cc55cSDimitry Andric // Apply module pipeline start EP callback. 1292349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1293349cc55cSDimitry Andric C(MPM, Level); 1294349cc55cSDimitry Andric 1295349cc55cSDimitry Andric // If we are planning to perform ThinLTO later, we don't bloat the code with 1296349cc55cSDimitry Andric // unrolling/vectorization/... now. Just simplify the module as much as we 1297349cc55cSDimitry Andric // can. 1298349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1299349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPreLink)); 1300349cc55cSDimitry Andric 1301349cc55cSDimitry Andric // Run partial inlining pass to partially inline functions that have 1302349cc55cSDimitry Andric // large bodies. 1303349cc55cSDimitry Andric // FIXME: It isn't clear whether this is really the right place to run this 1304349cc55cSDimitry Andric // in ThinLTO. Because there is another canonicalization and simplification 1305349cc55cSDimitry Andric // phase that will run after the thin link, running this here ends up with 1306349cc55cSDimitry Andric // less information than will be available later and it may grow functions in 1307349cc55cSDimitry Andric // ways that aren't beneficial. 1308349cc55cSDimitry Andric if (RunPartialInlining) 1309349cc55cSDimitry Andric MPM.addPass(PartialInlinerPass()); 1310349cc55cSDimitry Andric 1311349cc55cSDimitry Andric // Reduce the size of the IR as much as possible. 1312349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1313349cc55cSDimitry Andric 1314349cc55cSDimitry Andric // Module simplification splits coroutines, but does not fully clean up 1315349cc55cSDimitry Andric // coroutine intrinsics. To ensure ThinLTO optimization passes don't trip up 1316349cc55cSDimitry Andric // on these, we schedule the cleanup here. 1317349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass())); 1318349cc55cSDimitry Andric 1319349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling && 1320349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::SampleUse) 1321349cc55cSDimitry Andric MPM.addPass(PseudoProbeUpdatePass()); 1322349cc55cSDimitry Andric 1323349cc55cSDimitry Andric // Handle OptimizerLastEPCallbacks added by clang on PreLink. Actual 1324349cc55cSDimitry Andric // optimization is going to be done in PostLink stage, but clang can't 1325349cc55cSDimitry Andric // add callbacks there in case of in-process ThinLTO called by linker. 1326349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1327349cc55cSDimitry Andric C(MPM, Level); 1328349cc55cSDimitry Andric 1329349cc55cSDimitry Andric // Emit annotation remarks. 1330349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1331349cc55cSDimitry Andric 1332349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1333349cc55cSDimitry Andric 1334349cc55cSDimitry Andric return MPM; 1335349cc55cSDimitry Andric } 1336349cc55cSDimitry Andric 1337349cc55cSDimitry Andric ModulePassManager PassBuilder::buildThinLTODefaultPipeline( 1338349cc55cSDimitry Andric OptimizationLevel Level, const ModuleSummaryIndex *ImportSummary) { 1339349cc55cSDimitry Andric ModulePassManager MPM; 1340349cc55cSDimitry Andric 1341349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1342349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1343349cc55cSDimitry Andric 1344349cc55cSDimitry Andric if (ImportSummary) { 1345349cc55cSDimitry Andric // These passes import type identifier resolutions for whole-program 1346349cc55cSDimitry Andric // devirtualization and CFI. They must run early because other passes may 1347349cc55cSDimitry Andric // disturb the specific instruction patterns that these passes look for, 1348349cc55cSDimitry Andric // creating dependencies on resolutions that may not appear in the summary. 1349349cc55cSDimitry Andric // 1350349cc55cSDimitry Andric // For example, GVN may transform the pattern assume(type.test) appearing in 1351349cc55cSDimitry Andric // two basic blocks into assume(phi(type.test, type.test)), which would 1352349cc55cSDimitry Andric // transform a dependency on a WPD resolution into a dependency on a type 1353349cc55cSDimitry Andric // identifier resolution for CFI. 1354349cc55cSDimitry Andric // 1355349cc55cSDimitry Andric // Also, WPD has access to more precise information than ICP and can 1356349cc55cSDimitry Andric // devirtualize more effectively, so it should operate on the IR first. 1357349cc55cSDimitry Andric // 1358349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1359349cc55cSDimitry Andric // metadata and intrinsics. 1360349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary)); 1361349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary)); 1362349cc55cSDimitry Andric } 1363349cc55cSDimitry Andric 1364349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1365349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1366349cc55cSDimitry Andric // in ICP. 1367349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1368349cc55cSDimitry Andric // Drop available_externally and unreferenced globals. This is necessary 1369349cc55cSDimitry Andric // with ThinLTO in order to avoid leaving undefined references to dead 1370349cc55cSDimitry Andric // globals in the object file. 1371349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1372349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1373349cc55cSDimitry Andric return MPM; 1374349cc55cSDimitry Andric } 1375349cc55cSDimitry Andric 1376349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1377349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1378349cc55cSDimitry Andric 1379349cc55cSDimitry Andric // Add the core simplification pipeline. 1380349cc55cSDimitry Andric MPM.addPass(buildModuleSimplificationPipeline( 1381349cc55cSDimitry Andric Level, ThinOrFullLTOPhase::ThinLTOPostLink)); 1382349cc55cSDimitry Andric 1383349cc55cSDimitry Andric // Now add the optimization pipeline. 1384349cc55cSDimitry Andric MPM.addPass(buildModuleOptimizationPipeline(Level)); 1385349cc55cSDimitry Andric 1386349cc55cSDimitry Andric // Emit annotation remarks. 1387349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1388349cc55cSDimitry Andric 1389349cc55cSDimitry Andric return MPM; 1390349cc55cSDimitry Andric } 1391349cc55cSDimitry Andric 1392349cc55cSDimitry Andric ModulePassManager 1393349cc55cSDimitry Andric PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level) { 1394349cc55cSDimitry Andric assert(Level != OptimizationLevel::O0 && 1395349cc55cSDimitry Andric "Must request optimizations for the default pipeline!"); 1396349cc55cSDimitry Andric // FIXME: We should use a customized pre-link pipeline! 1397349cc55cSDimitry Andric return buildPerModuleDefaultPipeline(Level, 1398349cc55cSDimitry Andric /* LTOPreLink */ true); 1399349cc55cSDimitry Andric } 1400349cc55cSDimitry Andric 1401349cc55cSDimitry Andric ModulePassManager 1402349cc55cSDimitry Andric PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, 1403349cc55cSDimitry Andric ModuleSummaryIndex *ExportSummary) { 1404349cc55cSDimitry Andric ModulePassManager MPM; 1405349cc55cSDimitry Andric 1406349cc55cSDimitry Andric // Convert @llvm.global.annotations to !annotation metadata. 1407349cc55cSDimitry Andric MPM.addPass(Annotation2MetadataPass()); 1408349cc55cSDimitry Andric 1409349cc55cSDimitry Andric // Create a function that performs CFI checks for cross-DSO calls with targets 1410349cc55cSDimitry Andric // in the current module. 1411349cc55cSDimitry Andric MPM.addPass(CrossDSOCFIPass()); 1412349cc55cSDimitry Andric 1413349cc55cSDimitry Andric if (Level == OptimizationLevel::O0) { 1414349cc55cSDimitry Andric // The WPD and LowerTypeTest passes need to run at -O0 to lower type 1415349cc55cSDimitry Andric // metadata and intrinsics. 1416349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1417349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1418349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1419349cc55cSDimitry Andric // in ICP. 1420349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1421349cc55cSDimitry Andric 1422349cc55cSDimitry Andric // Emit annotation remarks. 1423349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1424349cc55cSDimitry Andric 1425349cc55cSDimitry Andric return MPM; 1426349cc55cSDimitry Andric } 1427349cc55cSDimitry Andric 1428349cc55cSDimitry Andric if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) { 1429349cc55cSDimitry Andric // Load sample profile before running the LTO optimization pipeline. 1430349cc55cSDimitry Andric MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, 1431349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile, 1432349cc55cSDimitry Andric ThinOrFullLTOPhase::FullLTOPostLink)); 1433349cc55cSDimitry Andric // Cache ProfileSummaryAnalysis once to avoid the potential need to insert 1434349cc55cSDimitry Andric // RequireAnalysisPass for PSI before subsequent non-module passes. 1435349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); 1436349cc55cSDimitry Andric } 1437349cc55cSDimitry Andric 1438349cc55cSDimitry Andric // Remove unused virtual tables to improve the quality of code generated by 1439349cc55cSDimitry Andric // whole-program devirtualization and bitset lowering. 1440349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1441349cc55cSDimitry Andric 1442349cc55cSDimitry Andric // Force any function attributes we want the rest of the pipeline to observe. 1443349cc55cSDimitry Andric MPM.addPass(ForceFunctionAttrsPass()); 1444349cc55cSDimitry Andric 1445349cc55cSDimitry Andric // Do basic inference of function attributes from known properties of system 1446349cc55cSDimitry Andric // libraries and other oracles. 1447349cc55cSDimitry Andric MPM.addPass(InferFunctionAttrsPass()); 1448349cc55cSDimitry Andric 1449349cc55cSDimitry Andric if (Level.getSpeedupLevel() > 1) { 1450349cc55cSDimitry Andric FunctionPassManager EarlyFPM; 1451349cc55cSDimitry Andric EarlyFPM.addPass(CallSiteSplittingPass()); 1452349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1453349cc55cSDimitry Andric std::move(EarlyFPM), PTO.EagerlyInvalidateAnalyses)); 1454349cc55cSDimitry Andric 1455349cc55cSDimitry Andric // Indirect call promotion. This should promote all the targets that are 1456349cc55cSDimitry Andric // left by the earlier promotion pass that promotes intra-module targets. 1457349cc55cSDimitry Andric // This two-step promotion is to save the compile time. For LTO, it should 1458349cc55cSDimitry Andric // produce the same result as if we only do promotion here. 1459349cc55cSDimitry Andric MPM.addPass(PGOIndirectCallPromotion( 1460349cc55cSDimitry Andric true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse)); 1461349cc55cSDimitry Andric 1462349cc55cSDimitry Andric if (EnableFunctionSpecialization && Level == OptimizationLevel::O3) 1463349cc55cSDimitry Andric MPM.addPass(FunctionSpecializationPass()); 1464349cc55cSDimitry Andric // Propagate constants at call sites into the functions they call. This 1465349cc55cSDimitry Andric // opens opportunities for globalopt (and inlining) by substituting function 1466349cc55cSDimitry Andric // pointers passed as arguments to direct uses of functions. 1467349cc55cSDimitry Andric MPM.addPass(IPSCCPPass()); 1468349cc55cSDimitry Andric 1469349cc55cSDimitry Andric // Attach metadata to indirect call sites indicating the set of functions 1470349cc55cSDimitry Andric // they may target at run-time. This should follow IPSCCP. 1471349cc55cSDimitry Andric MPM.addPass(CalledValuePropagationPass()); 1472349cc55cSDimitry Andric } 1473349cc55cSDimitry Andric 1474349cc55cSDimitry Andric // Now deduce any function attributes based in the current code. 1475349cc55cSDimitry Andric MPM.addPass( 1476349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1477349cc55cSDimitry Andric 1478349cc55cSDimitry Andric // Do RPO function attribute inference across the module to forward-propagate 1479349cc55cSDimitry Andric // attributes where applicable. 1480349cc55cSDimitry Andric // FIXME: Is this really an optimization rather than a canonicalization? 1481349cc55cSDimitry Andric MPM.addPass(ReversePostOrderFunctionAttrsPass()); 1482349cc55cSDimitry Andric 1483349cc55cSDimitry Andric // Use in-range annotations on GEP indices to split globals where beneficial. 1484349cc55cSDimitry Andric MPM.addPass(GlobalSplitPass()); 1485349cc55cSDimitry Andric 1486349cc55cSDimitry Andric // Run whole program optimization of virtual call when the list of callees 1487349cc55cSDimitry Andric // is fixed. 1488349cc55cSDimitry Andric MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); 1489349cc55cSDimitry Andric 1490349cc55cSDimitry Andric // Stop here at -O1. 1491349cc55cSDimitry Andric if (Level == OptimizationLevel::O1) { 1492349cc55cSDimitry Andric // The LowerTypeTestsPass needs to run to lower type metadata and the 1493349cc55cSDimitry Andric // type.test intrinsics. The pass does nothing if CFI is disabled. 1494349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1495349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1496349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO 1497349cc55cSDimitry Andric // pipeline). 1498349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1499349cc55cSDimitry Andric 1500349cc55cSDimitry Andric // Emit annotation remarks. 1501349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1502349cc55cSDimitry Andric 1503349cc55cSDimitry Andric return MPM; 1504349cc55cSDimitry Andric } 1505349cc55cSDimitry Andric 1506349cc55cSDimitry Andric // Optimize globals to try and fold them into constants. 1507349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1508349cc55cSDimitry Andric 1509349cc55cSDimitry Andric // Promote any localized globals to SSA registers. 1510349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); 1511349cc55cSDimitry Andric 1512349cc55cSDimitry Andric // Linking modules together can lead to duplicate global constant, only 1513349cc55cSDimitry Andric // keep one copy of each constant. 1514349cc55cSDimitry Andric MPM.addPass(ConstantMergePass()); 1515349cc55cSDimitry Andric 1516349cc55cSDimitry Andric // Remove unused arguments from functions. 1517349cc55cSDimitry Andric MPM.addPass(DeadArgumentEliminationPass()); 1518349cc55cSDimitry Andric 1519349cc55cSDimitry Andric // Reduce the code after globalopt and ipsccp. Both can open up significant 1520349cc55cSDimitry Andric // simplification opportunities, and both can propagate functions through 1521349cc55cSDimitry Andric // function pointers. When this happens, we often have to resolve varargs 1522349cc55cSDimitry Andric // calls, etc, so let instcombine do this. 1523349cc55cSDimitry Andric FunctionPassManager PeepholeFPM; 1524349cc55cSDimitry Andric if (Level == OptimizationLevel::O3) 1525349cc55cSDimitry Andric PeepholeFPM.addPass(AggressiveInstCombinePass()); 1526349cc55cSDimitry Andric PeepholeFPM.addPass(InstCombinePass()); 1527349cc55cSDimitry Andric invokePeepholeEPCallbacks(PeepholeFPM, Level); 1528349cc55cSDimitry Andric 1529349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM), 1530349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1531349cc55cSDimitry Andric 1532349cc55cSDimitry Andric // Note: historically, the PruneEH pass was run first to deduce nounwind and 1533349cc55cSDimitry Andric // generally clean up exception handling overhead. It isn't clear this is 1534349cc55cSDimitry Andric // valuable as the inliner doesn't currently care whether it is inlining an 1535349cc55cSDimitry Andric // invoke or a call. 1536349cc55cSDimitry Andric // Run the inliner now. 1537349cc55cSDimitry Andric MPM.addPass(ModuleInlinerWrapperPass(getInlineParamsFromOptLevel(Level))); 1538349cc55cSDimitry Andric 1539349cc55cSDimitry Andric // Optimize globals again after we ran the inliner. 1540349cc55cSDimitry Andric MPM.addPass(GlobalOptPass()); 1541349cc55cSDimitry Andric 1542349cc55cSDimitry Andric // Garbage collect dead functions. 1543349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1544349cc55cSDimitry Andric 1545349cc55cSDimitry Andric // If we didn't decide to inline a function, check to see if we can 1546349cc55cSDimitry Andric // transform it to pass arguments by value instead of by reference. 1547349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(ArgumentPromotionPass())); 1548349cc55cSDimitry Andric 1549349cc55cSDimitry Andric FunctionPassManager FPM; 1550349cc55cSDimitry Andric // The IPO Passes may leave cruft around. Clean up after them. 1551349cc55cSDimitry Andric FPM.addPass(InstCombinePass()); 1552349cc55cSDimitry Andric invokePeepholeEPCallbacks(FPM, Level); 1553349cc55cSDimitry Andric 1554349cc55cSDimitry Andric FPM.addPass(JumpThreadingPass(/*InsertFreezeWhenUnfoldingSelect*/ true)); 1555349cc55cSDimitry Andric 1556349cc55cSDimitry Andric // Do a post inline PGO instrumentation and use pass. This is a context 1557349cc55cSDimitry Andric // sensitive PGO pass. 1558349cc55cSDimitry Andric if (PGOOpt) { 1559349cc55cSDimitry Andric if (PGOOpt->CSAction == PGOOptions::CSIRInstr) 1560349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ true, 1561349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->CSProfileGenFile, 1562349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile); 1563349cc55cSDimitry Andric else if (PGOOpt->CSAction == PGOOptions::CSIRUse) 1564349cc55cSDimitry Andric addPGOInstrPasses(MPM, Level, /* RunProfileGen */ false, 1565349cc55cSDimitry Andric /* IsCS */ true, PGOOpt->ProfileFile, 1566349cc55cSDimitry Andric PGOOpt->ProfileRemappingFile); 1567349cc55cSDimitry Andric } 1568349cc55cSDimitry Andric 1569349cc55cSDimitry Andric // Break up allocas 1570349cc55cSDimitry Andric FPM.addPass(SROAPass()); 1571349cc55cSDimitry Andric 1572349cc55cSDimitry Andric // LTO provides additional opportunities for tailcall elimination due to 1573349cc55cSDimitry Andric // link-time inlining, and visibility of nocapture attribute. 1574349cc55cSDimitry Andric FPM.addPass(TailCallElimPass()); 1575349cc55cSDimitry Andric 1576349cc55cSDimitry Andric // Run a few AA driver optimizations here and now to cleanup the code. 1577349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM), 1578349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1579349cc55cSDimitry Andric 1580349cc55cSDimitry Andric MPM.addPass( 1581349cc55cSDimitry Andric createModuleToPostOrderCGSCCPassAdaptor(PostOrderFunctionAttrsPass())); 1582349cc55cSDimitry Andric 1583349cc55cSDimitry Andric // Require the GlobalsAA analysis for the module so we can query it within 1584349cc55cSDimitry Andric // MainFPM. 1585349cc55cSDimitry Andric MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); 1586349cc55cSDimitry Andric // Invalidate AAManager so it can be recreated and pick up the newly available 1587349cc55cSDimitry Andric // GlobalsAA. 1588349cc55cSDimitry Andric MPM.addPass( 1589349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>())); 1590349cc55cSDimitry Andric 1591349cc55cSDimitry Andric FunctionPassManager MainFPM; 1592349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1593349cc55cSDimitry Andric LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap), 1594349cc55cSDimitry Andric /*USeMemorySSA=*/true, /*UseBlockFrequencyInfo=*/true)); 1595349cc55cSDimitry Andric 1596349cc55cSDimitry Andric if (RunNewGVN) 1597349cc55cSDimitry Andric MainFPM.addPass(NewGVNPass()); 1598349cc55cSDimitry Andric else 1599349cc55cSDimitry Andric MainFPM.addPass(GVNPass()); 1600349cc55cSDimitry Andric 1601349cc55cSDimitry Andric // Remove dead memcpy()'s. 1602349cc55cSDimitry Andric MainFPM.addPass(MemCpyOptPass()); 1603349cc55cSDimitry Andric 1604349cc55cSDimitry Andric // Nuke dead stores. 1605349cc55cSDimitry Andric MainFPM.addPass(DSEPass()); 1606349cc55cSDimitry Andric MainFPM.addPass(MergedLoadStoreMotionPass()); 1607349cc55cSDimitry Andric 1608349cc55cSDimitry Andric // More loops are countable; try to optimize them. 1609349cc55cSDimitry Andric if (EnableLoopFlatten && Level.getSpeedupLevel() > 1) 1610349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor(LoopFlattenPass())); 1611349cc55cSDimitry Andric 1612349cc55cSDimitry Andric if (EnableConstraintElimination) 1613349cc55cSDimitry Andric MainFPM.addPass(ConstraintEliminationPass()); 1614349cc55cSDimitry Andric 1615349cc55cSDimitry Andric LoopPassManager LPM; 1616349cc55cSDimitry Andric LPM.addPass(IndVarSimplifyPass()); 1617349cc55cSDimitry Andric LPM.addPass(LoopDeletionPass()); 1618349cc55cSDimitry Andric // FIXME: Add loop interchange. 1619349cc55cSDimitry Andric 1620349cc55cSDimitry Andric // Unroll small loops and perform peeling. 1621349cc55cSDimitry Andric LPM.addPass(LoopFullUnrollPass(Level.getSpeedupLevel(), 1622349cc55cSDimitry Andric /* OnlyWhenForced= */ !PTO.LoopUnrolling, 1623349cc55cSDimitry Andric PTO.ForgetAllSCEVInLoopUnroll)); 1624349cc55cSDimitry Andric // The loop passes in LPM (LoopFullUnrollPass) do not preserve MemorySSA. 1625349cc55cSDimitry Andric // *All* loop passes must preserve it, in order to be able to use it. 1626349cc55cSDimitry Andric MainFPM.addPass(createFunctionToLoopPassAdaptor( 1627349cc55cSDimitry Andric std::move(LPM), /*UseMemorySSA=*/false, /*UseBlockFrequencyInfo=*/true)); 1628349cc55cSDimitry Andric 1629349cc55cSDimitry Andric MainFPM.addPass(LoopDistributePass()); 1630349cc55cSDimitry Andric 1631349cc55cSDimitry Andric addVectorPasses(Level, MainFPM, /* IsFullLTO */ true); 1632349cc55cSDimitry Andric 1633349cc55cSDimitry Andric invokePeepholeEPCallbacks(MainFPM, Level); 1634349cc55cSDimitry Andric MainFPM.addPass(JumpThreadingPass(/*InsertFreezeWhenUnfoldingSelect*/ true)); 1635349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM), 1636349cc55cSDimitry Andric PTO.EagerlyInvalidateAnalyses)); 1637349cc55cSDimitry Andric 1638349cc55cSDimitry Andric // Lower type metadata and the type.test intrinsic. This pass supports 1639349cc55cSDimitry Andric // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs 1640349cc55cSDimitry Andric // to be run at link time if CFI is enabled. This pass does nothing if 1641349cc55cSDimitry Andric // CFI is disabled. 1642349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); 1643349cc55cSDimitry Andric // Run a second time to clean up any type tests left behind by WPD for use 1644349cc55cSDimitry Andric // in ICP (which is performed earlier than this in the regular LTO pipeline). 1645349cc55cSDimitry Andric MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true)); 1646349cc55cSDimitry Andric 1647349cc55cSDimitry Andric // Enable splitting late in the FullLTO post-link pipeline. This is done in 1648349cc55cSDimitry Andric // the same stage in the old pass manager (\ref addLateLTOOptimizationPasses). 1649349cc55cSDimitry Andric if (EnableHotColdSplit) 1650349cc55cSDimitry Andric MPM.addPass(HotColdSplittingPass()); 1651349cc55cSDimitry Andric 1652349cc55cSDimitry Andric // Add late LTO optimization passes. 1653349cc55cSDimitry Andric // Delete basic blocks, which optimization passes may have killed. 1654349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1655349cc55cSDimitry Andric SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)))); 1656349cc55cSDimitry Andric 1657349cc55cSDimitry Andric // Drop bodies of available eternally objects to improve GlobalDCE. 1658349cc55cSDimitry Andric MPM.addPass(EliminateAvailableExternallyPass()); 1659349cc55cSDimitry Andric 1660349cc55cSDimitry Andric // Now that we have optimized the program, discard unreachable functions. 1661349cc55cSDimitry Andric MPM.addPass(GlobalDCEPass()); 1662349cc55cSDimitry Andric 1663349cc55cSDimitry Andric if (PTO.MergeFunctions) 1664349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1665349cc55cSDimitry Andric 1666349cc55cSDimitry Andric // Emit annotation remarks. 1667349cc55cSDimitry Andric addAnnotationRemarksPass(MPM); 1668349cc55cSDimitry Andric 1669349cc55cSDimitry Andric return MPM; 1670349cc55cSDimitry Andric } 1671349cc55cSDimitry Andric 1672349cc55cSDimitry Andric ModulePassManager PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level, 1673349cc55cSDimitry Andric bool LTOPreLink) { 1674349cc55cSDimitry Andric assert(Level == OptimizationLevel::O0 && 1675349cc55cSDimitry Andric "buildO0DefaultPipeline should only be used with O0"); 1676349cc55cSDimitry Andric 1677349cc55cSDimitry Andric ModulePassManager MPM; 1678349cc55cSDimitry Andric 1679349cc55cSDimitry Andric // Perform pseudo probe instrumentation in O0 mode. This is for the 1680349cc55cSDimitry Andric // consistency between different build modes. For example, a LTO build can be 1681349cc55cSDimitry Andric // mixed with an O0 prelink and an O2 postlink. Loading a sample profile in 1682349cc55cSDimitry Andric // the postlink will require pseudo probe instrumentation in the prelink. 1683349cc55cSDimitry Andric if (PGOOpt && PGOOpt->PseudoProbeForProfiling) 1684349cc55cSDimitry Andric MPM.addPass(SampleProfileProbePass(TM)); 1685349cc55cSDimitry Andric 1686349cc55cSDimitry Andric if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr || 1687349cc55cSDimitry Andric PGOOpt->Action == PGOOptions::IRUse)) 1688349cc55cSDimitry Andric addPGOInstrPassesForO0( 1689349cc55cSDimitry Andric MPM, 1690349cc55cSDimitry Andric /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr), 1691349cc55cSDimitry Andric /* IsCS */ false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile); 1692349cc55cSDimitry Andric 1693349cc55cSDimitry Andric for (auto &C : PipelineStartEPCallbacks) 1694349cc55cSDimitry Andric C(MPM, Level); 1695349cc55cSDimitry Andric 1696349cc55cSDimitry Andric if (PGOOpt && PGOOpt->DebugInfoForProfiling) 1697349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); 1698349cc55cSDimitry Andric 1699349cc55cSDimitry Andric for (auto &C : PipelineEarlySimplificationEPCallbacks) 1700349cc55cSDimitry Andric C(MPM, Level); 1701349cc55cSDimitry Andric 1702349cc55cSDimitry Andric // Build a minimal pipeline based on the semantics required by LLVM, 1703349cc55cSDimitry Andric // which is just that always inlining occurs. Further, disable generating 1704349cc55cSDimitry Andric // lifetime intrinsics to avoid enabling further optimizations during 1705349cc55cSDimitry Andric // code generation. 1706349cc55cSDimitry Andric MPM.addPass(AlwaysInlinerPass( 1707349cc55cSDimitry Andric /*InsertLifetimeIntrinsics=*/false)); 1708349cc55cSDimitry Andric 1709349cc55cSDimitry Andric if (PTO.MergeFunctions) 1710349cc55cSDimitry Andric MPM.addPass(MergeFunctionsPass()); 1711349cc55cSDimitry Andric 1712349cc55cSDimitry Andric if (EnableMatrix) 1713349cc55cSDimitry Andric MPM.addPass( 1714349cc55cSDimitry Andric createModuleToFunctionPassAdaptor(LowerMatrixIntrinsicsPass(true))); 1715349cc55cSDimitry Andric 1716349cc55cSDimitry Andric if (!CGSCCOptimizerLateEPCallbacks.empty()) { 1717349cc55cSDimitry Andric CGSCCPassManager CGPM; 1718349cc55cSDimitry Andric for (auto &C : CGSCCOptimizerLateEPCallbacks) 1719349cc55cSDimitry Andric C(CGPM, Level); 1720349cc55cSDimitry Andric if (!CGPM.isEmpty()) 1721349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 1722349cc55cSDimitry Andric } 1723349cc55cSDimitry Andric if (!LateLoopOptimizationsEPCallbacks.empty()) { 1724349cc55cSDimitry Andric LoopPassManager LPM; 1725349cc55cSDimitry Andric for (auto &C : LateLoopOptimizationsEPCallbacks) 1726349cc55cSDimitry Andric C(LPM, Level); 1727349cc55cSDimitry Andric if (!LPM.isEmpty()) { 1728349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1729349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 1730349cc55cSDimitry Andric } 1731349cc55cSDimitry Andric } 1732349cc55cSDimitry Andric if (!LoopOptimizerEndEPCallbacks.empty()) { 1733349cc55cSDimitry Andric LoopPassManager LPM; 1734349cc55cSDimitry Andric for (auto &C : LoopOptimizerEndEPCallbacks) 1735349cc55cSDimitry Andric C(LPM, Level); 1736349cc55cSDimitry Andric if (!LPM.isEmpty()) { 1737349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor( 1738349cc55cSDimitry Andric createFunctionToLoopPassAdaptor(std::move(LPM)))); 1739349cc55cSDimitry Andric } 1740349cc55cSDimitry Andric } 1741349cc55cSDimitry Andric if (!ScalarOptimizerLateEPCallbacks.empty()) { 1742349cc55cSDimitry Andric FunctionPassManager FPM; 1743349cc55cSDimitry Andric for (auto &C : ScalarOptimizerLateEPCallbacks) 1744349cc55cSDimitry Andric C(FPM, Level); 1745349cc55cSDimitry Andric if (!FPM.isEmpty()) 1746349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1747349cc55cSDimitry Andric } 1748349cc55cSDimitry Andric if (!VectorizerStartEPCallbacks.empty()) { 1749349cc55cSDimitry Andric FunctionPassManager FPM; 1750349cc55cSDimitry Andric for (auto &C : VectorizerStartEPCallbacks) 1751349cc55cSDimitry Andric C(FPM, Level); 1752349cc55cSDimitry Andric if (!FPM.isEmpty()) 1753349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); 1754349cc55cSDimitry Andric } 1755349cc55cSDimitry Andric 1756349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(CoroEarlyPass())); 1757349cc55cSDimitry Andric CGSCCPassManager CGPM; 1758349cc55cSDimitry Andric CGPM.addPass(CoroSplitPass()); 1759349cc55cSDimitry Andric MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); 1760349cc55cSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass())); 1761349cc55cSDimitry Andric 1762349cc55cSDimitry Andric for (auto &C : OptimizerLastEPCallbacks) 1763349cc55cSDimitry Andric C(MPM, Level); 1764349cc55cSDimitry Andric 1765349cc55cSDimitry Andric if (LTOPreLink) 1766349cc55cSDimitry Andric addRequiredLTOPreLinkPasses(MPM); 1767349cc55cSDimitry Andric 1768*4824e7fdSDimitry Andric MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass())); 1769*4824e7fdSDimitry Andric 1770349cc55cSDimitry Andric return MPM; 1771349cc55cSDimitry Andric } 1772349cc55cSDimitry Andric 1773349cc55cSDimitry Andric AAManager PassBuilder::buildDefaultAAPipeline() { 1774349cc55cSDimitry Andric AAManager AA; 1775349cc55cSDimitry Andric 1776349cc55cSDimitry Andric // The order in which these are registered determines their priority when 1777349cc55cSDimitry Andric // being queried. 1778349cc55cSDimitry Andric 1779349cc55cSDimitry Andric // First we register the basic alias analysis that provides the majority of 1780349cc55cSDimitry Andric // per-function local AA logic. This is a stateless, on-demand local set of 1781349cc55cSDimitry Andric // AA techniques. 1782349cc55cSDimitry Andric AA.registerFunctionAnalysis<BasicAA>(); 1783349cc55cSDimitry Andric 1784349cc55cSDimitry Andric // Next we query fast, specialized alias analyses that wrap IR-embedded 1785349cc55cSDimitry Andric // information about aliasing. 1786349cc55cSDimitry Andric AA.registerFunctionAnalysis<ScopedNoAliasAA>(); 1787349cc55cSDimitry Andric AA.registerFunctionAnalysis<TypeBasedAA>(); 1788349cc55cSDimitry Andric 1789349cc55cSDimitry Andric // Add support for querying global aliasing information when available. 1790349cc55cSDimitry Andric // Because the `AAManager` is a function analysis and `GlobalsAA` is a module 1791349cc55cSDimitry Andric // analysis, all that the `AAManager` can do is query for any *cached* 1792349cc55cSDimitry Andric // results from `GlobalsAA` through a readonly proxy. 1793349cc55cSDimitry Andric AA.registerModuleAnalysis<GlobalsAA>(); 1794349cc55cSDimitry Andric 1795349cc55cSDimitry Andric // Add target-specific alias analyses. 1796349cc55cSDimitry Andric if (TM) 1797349cc55cSDimitry Andric TM->registerDefaultAliasAnalyses(AA); 1798349cc55cSDimitry Andric 1799349cc55cSDimitry Andric return AA; 1800349cc55cSDimitry Andric } 1801