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 #include "llvm/Support/Compiler.h" 34 35 namespace llvm { 36 37 class AAResults; 38 class AssumptionCache; 39 class BlockFrequencyInfo; 40 class BranchProbabilityInfo; 41 class DominatorTree; 42 class Function; 43 class Loop; 44 class LoopInfo; 45 class MemorySSA; 46 class ScalarEvolution; 47 class TargetLibraryInfo; 48 class TargetTransformInfo; 49 50 /// The adaptor from a function pass to a loop pass computes these analyses and 51 /// makes them available to the loop passes "for free". Each loop pass is 52 /// expected to update these analyses if necessary to ensure they're 53 /// valid after it runs. 54 struct LoopStandardAnalysisResults { 55 AAResults &AA; 56 AssumptionCache &AC; 57 DominatorTree &DT; 58 LoopInfo &LI; 59 ScalarEvolution &SE; 60 TargetLibraryInfo &TLI; 61 TargetTransformInfo &TTI; 62 BlockFrequencyInfo *BFI; 63 BranchProbabilityInfo *BPI; 64 MemorySSA *MSSA; 65 }; 66 67 /// Extern template declaration for the analysis set for this IR unit. 68 extern template class LLVM_TEMPLATE_ABI AllAnalysesOn<Loop>; 69 70 extern template class LLVM_TEMPLATE_ABI 71 AnalysisManager<Loop, LoopStandardAnalysisResults &>; 72 /// The loop analysis manager. 73 /// 74 /// See the documentation for the AnalysisManager template for detail 75 /// documentation. This typedef serves as a convenient way to refer to this 76 /// construct in the adaptors and proxies used to integrate this into the larger 77 /// pass manager infrastructure. 78 typedef AnalysisManager<Loop, LoopStandardAnalysisResults &> 79 LoopAnalysisManager; 80 81 /// A proxy from a \c LoopAnalysisManager to a \c Function. 82 typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function> 83 LoopAnalysisManagerFunctionProxy; 84 85 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which 86 /// retains a \c LoopInfo reference. 87 /// 88 /// This allows it to collect loop objects for which analysis results may be 89 /// cached in the \c LoopAnalysisManager. 90 template <> class LoopAnalysisManagerFunctionProxy::Result { 91 public: Result(LoopAnalysisManager & InnerAM,LoopInfo & LI)92 explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI) 93 : InnerAM(&InnerAM), LI(&LI) {} Result(Result && Arg)94 Result(Result &&Arg) 95 : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI), MSSAUsed(Arg.MSSAUsed) { 96 // We have to null out the analysis manager in the moved-from state 97 // because we are taking ownership of the responsibilty to clear the 98 // analysis state. 99 Arg.InnerAM = nullptr; 100 } 101 Result &operator=(Result &&RHS) { 102 InnerAM = RHS.InnerAM; 103 LI = RHS.LI; 104 MSSAUsed = RHS.MSSAUsed; 105 // We have to null out the analysis manager in the moved-from state 106 // because we are taking ownership of the responsibilty to clear the 107 // analysis state. 108 RHS.InnerAM = nullptr; 109 return *this; 110 } ~Result()111 ~Result() { 112 // InnerAM is cleared in a moved from state where there is nothing to do. 113 if (!InnerAM) 114 return; 115 116 // Clear out the analysis manager if we're being destroyed -- it means we 117 // didn't even see an invalidate call when we got invalidated. 118 InnerAM->clear(); 119 } 120 121 /// Mark MemorySSA as used so we can invalidate self if MSSA is invalidated. markMSSAUsed()122 void markMSSAUsed() { MSSAUsed = true; } 123 124 /// Accessor for the analysis manager. getManager()125 LoopAnalysisManager &getManager() { return *InnerAM; } 126 127 /// Handler for invalidation of the proxy for a particular function. 128 /// 129 /// If the proxy, \c LoopInfo, and associated analyses are preserved, this 130 /// will merely forward the invalidation event to any cached loop analysis 131 /// results for loops within this function. 132 /// 133 /// If the necessary loop infrastructure is not preserved, this will forcibly 134 /// clear all of the cached analysis results that are keyed on the \c 135 /// LoopInfo for this function. 136 LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, 137 FunctionAnalysisManager::Invalidator &Inv); 138 139 private: 140 LoopAnalysisManager *InnerAM; 141 LoopInfo *LI; 142 bool MSSAUsed = false; 143 }; 144 145 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy 146 /// so it can pass the \c LoopInfo to the result. 147 template <> 148 LLVM_ABI LoopAnalysisManagerFunctionProxy::Result 149 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM); 150 151 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern 152 // template. 153 extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>; 154 155 extern template class LLVM_TEMPLATE_ABI OuterAnalysisManagerProxy< 156 FunctionAnalysisManager, Loop, LoopStandardAnalysisResults &>; 157 /// A proxy from a \c FunctionAnalysisManager to a \c Loop. 158 typedef OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop, 159 LoopStandardAnalysisResults &> 160 FunctionAnalysisManagerLoopProxy; 161 162 /// Returns the minimum set of Analyses that all loop passes must preserve. 163 LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses(); 164 } 165 166 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H 167