1 //===- LoopAnalysisManager.h - Loop analysis management ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// \file 9 /// 10 /// This header provides classes for managing per-loop analyses. These are 11 /// typically used as part of a loop pass pipeline over the loop nests of 12 /// a function. 13 /// 14 /// Loop analyses are allowed to make some simplifying assumptions: 15 /// 1) Loops are, where possible, in simplified form. 16 /// 2) Loops are *always* in LCSSA form. 17 /// 3) A collection of analysis results are available: 18 /// - LoopInfo 19 /// - DominatorTree 20 /// - ScalarEvolution 21 /// - AAManager 22 /// 23 /// The primary mechanism to provide these invariants is the loop pass manager, 24 /// but they can also be manually provided in order to reason about a loop from 25 /// outside of a dedicated pass manager. 26 /// 27 //===----------------------------------------------------------------------===// 28 29 #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H 30 #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H 31 32 #include "llvm/IR/PassManager.h" 33 34 namespace llvm { 35 36 class AAResults; 37 class AssumptionCache; 38 class BlockFrequencyInfo; 39 class BranchProbabilityInfo; 40 class DominatorTree; 41 class Function; 42 class Loop; 43 class LoopInfo; 44 class MemorySSA; 45 class ScalarEvolution; 46 class TargetLibraryInfo; 47 class TargetTransformInfo; 48 49 /// The adaptor from a function pass to a loop pass computes these analyses and 50 /// makes them available to the loop passes "for free". Each loop pass is 51 /// expected to update these analyses if necessary to ensure they're 52 /// valid after it runs. 53 struct LoopStandardAnalysisResults { 54 AAResults &AA; 55 AssumptionCache &AC; 56 DominatorTree &DT; 57 LoopInfo &LI; 58 ScalarEvolution &SE; 59 TargetLibraryInfo &TLI; 60 TargetTransformInfo &TTI; 61 BlockFrequencyInfo *BFI; 62 BranchProbabilityInfo *BPI; 63 MemorySSA *MSSA; 64 }; 65 66 /// Extern template declaration for the analysis set for this IR unit. 67 extern template class AllAnalysesOn<Loop>; 68 69 extern template class AnalysisManager<Loop, LoopStandardAnalysisResults &>; 70 /// The loop analysis manager. 71 /// 72 /// See the documentation for the AnalysisManager template for detail 73 /// documentation. This typedef serves as a convenient way to refer to this 74 /// construct in the adaptors and proxies used to integrate this into the larger 75 /// pass manager infrastructure. 76 typedef AnalysisManager<Loop, LoopStandardAnalysisResults &> 77 LoopAnalysisManager; 78 79 /// A proxy from a \c LoopAnalysisManager to a \c Function. 80 typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function> 81 LoopAnalysisManagerFunctionProxy; 82 83 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which 84 /// retains a \c LoopInfo reference. 85 /// 86 /// This allows it to collect loop objects for which analysis results may be 87 /// cached in the \c LoopAnalysisManager. 88 template <> class LoopAnalysisManagerFunctionProxy::Result { 89 public: Result(LoopAnalysisManager & InnerAM,LoopInfo & LI)90 explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI) 91 : InnerAM(&InnerAM), LI(&LI) {} Result(Result && Arg)92 Result(Result &&Arg) 93 : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI), MSSAUsed(Arg.MSSAUsed) { 94 // We have to null out the analysis manager in the moved-from state 95 // because we are taking ownership of the responsibilty to clear the 96 // analysis state. 97 Arg.InnerAM = nullptr; 98 } 99 Result &operator=(Result &&RHS) { 100 InnerAM = RHS.InnerAM; 101 LI = RHS.LI; 102 MSSAUsed = RHS.MSSAUsed; 103 // We have to null out the analysis manager in the moved-from state 104 // because we are taking ownership of the responsibilty to clear the 105 // analysis state. 106 RHS.InnerAM = nullptr; 107 return *this; 108 } ~Result()109 ~Result() { 110 // InnerAM is cleared in a moved from state where there is nothing to do. 111 if (!InnerAM) 112 return; 113 114 // Clear out the analysis manager if we're being destroyed -- it means we 115 // didn't even see an invalidate call when we got invalidated. 116 InnerAM->clear(); 117 } 118 119 /// Mark MemorySSA as used so we can invalidate self if MSSA is invalidated. markMSSAUsed()120 void markMSSAUsed() { MSSAUsed = true; } 121 122 /// Accessor for the analysis manager. getManager()123 LoopAnalysisManager &getManager() { return *InnerAM; } 124 125 /// Handler for invalidation of the proxy for a particular function. 126 /// 127 /// If the proxy, \c LoopInfo, and associated analyses are preserved, this 128 /// will merely forward the invalidation event to any cached loop analysis 129 /// results for loops within this function. 130 /// 131 /// If the necessary loop infrastructure is not preserved, this will forcibly 132 /// clear all of the cached analysis results that are keyed on the \c 133 /// LoopInfo for this function. 134 bool invalidate(Function &F, const PreservedAnalyses &PA, 135 FunctionAnalysisManager::Invalidator &Inv); 136 137 private: 138 LoopAnalysisManager *InnerAM; 139 LoopInfo *LI; 140 bool MSSAUsed = false; 141 }; 142 143 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy 144 /// so it can pass the \c LoopInfo to the result. 145 template <> 146 LoopAnalysisManagerFunctionProxy::Result 147 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM); 148 149 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern 150 // template. 151 extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>; 152 153 extern template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop, 154 LoopStandardAnalysisResults &>; 155 /// A proxy from a \c FunctionAnalysisManager to a \c Loop. 156 typedef OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop, 157 LoopStandardAnalysisResults &> 158 FunctionAnalysisManagerLoopProxy; 159 160 /// Returns the minimum set of Analyses that all loop passes must preserve. 161 PreservedAnalyses getLoopPassPreservedAnalyses(); 162 } 163 164 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H 165