Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:14

0001 //===- LoopAnalysisManager.h - Loop analysis management ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This header provides classes for managing per-loop analyses. These are
0011 /// typically used as part of a loop pass pipeline over the loop nests of
0012 /// a function.
0013 ///
0014 /// Loop analyses are allowed to make some simplifying assumptions:
0015 /// 1) Loops are, where possible, in simplified form.
0016 /// 2) Loops are *always* in LCSSA form.
0017 /// 3) A collection of analysis results are available:
0018 ///    - LoopInfo
0019 ///    - DominatorTree
0020 ///    - ScalarEvolution
0021 ///    - AAManager
0022 ///
0023 /// The primary mechanism to provide these invariants is the loop pass manager,
0024 /// but they can also be manually provided in order to reason about a loop from
0025 /// outside of a dedicated pass manager.
0026 ///
0027 //===----------------------------------------------------------------------===//
0028 
0029 #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
0030 #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
0031 
0032 #include "llvm/IR/PassManager.h"
0033 
0034 namespace llvm {
0035 
0036 class AAResults;
0037 class AssumptionCache;
0038 class BlockFrequencyInfo;
0039 class BranchProbabilityInfo;
0040 class DominatorTree;
0041 class Function;
0042 class Loop;
0043 class LoopInfo;
0044 class MemorySSA;
0045 class ScalarEvolution;
0046 class TargetLibraryInfo;
0047 class TargetTransformInfo;
0048 
0049 /// The adaptor from a function pass to a loop pass computes these analyses and
0050 /// makes them available to the loop passes "for free". Each loop pass is
0051 /// expected to update these analyses if necessary to ensure they're
0052 /// valid after it runs.
0053 struct LoopStandardAnalysisResults {
0054   AAResults &AA;
0055   AssumptionCache ∾
0056   DominatorTree &DT;
0057   LoopInfo &LI;
0058   ScalarEvolution &SE;
0059   TargetLibraryInfo &TLI;
0060   TargetTransformInfo &TTI;
0061   BlockFrequencyInfo *BFI;
0062   BranchProbabilityInfo *BPI;
0063   MemorySSA *MSSA;
0064 };
0065 
0066 /// Extern template declaration for the analysis set for this IR unit.
0067 extern template class AllAnalysesOn<Loop>;
0068 
0069 extern template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
0070 /// The loop analysis manager.
0071 ///
0072 /// See the documentation for the AnalysisManager template for detail
0073 /// documentation. This typedef serves as a convenient way to refer to this
0074 /// construct in the adaptors and proxies used to integrate this into the larger
0075 /// pass manager infrastructure.
0076 typedef AnalysisManager<Loop, LoopStandardAnalysisResults &>
0077     LoopAnalysisManager;
0078 
0079 /// A proxy from a \c LoopAnalysisManager to a \c Function.
0080 typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function>
0081     LoopAnalysisManagerFunctionProxy;
0082 
0083 /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
0084 /// retains a \c LoopInfo reference.
0085 ///
0086 /// This allows it to collect loop objects for which analysis results may be
0087 /// cached in the \c LoopAnalysisManager.
0088 template <> class LoopAnalysisManagerFunctionProxy::Result {
0089 public:
0090   explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
0091       : InnerAM(&InnerAM), LI(&LI) {}
0092   Result(Result &&Arg)
0093       : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI), MSSAUsed(Arg.MSSAUsed) {
0094     // We have to null out the analysis manager in the moved-from state
0095     // because we are taking ownership of the responsibilty to clear the
0096     // analysis state.
0097     Arg.InnerAM = nullptr;
0098   }
0099   Result &operator=(Result &&RHS) {
0100     InnerAM = RHS.InnerAM;
0101     LI = RHS.LI;
0102     MSSAUsed = RHS.MSSAUsed;
0103     // We have to null out the analysis manager in the moved-from state
0104     // because we are taking ownership of the responsibilty to clear the
0105     // analysis state.
0106     RHS.InnerAM = nullptr;
0107     return *this;
0108   }
0109   ~Result() {
0110     // InnerAM is cleared in a moved from state where there is nothing to do.
0111     if (!InnerAM)
0112       return;
0113 
0114     // Clear out the analysis manager if we're being destroyed -- it means we
0115     // didn't even see an invalidate call when we got invalidated.
0116     InnerAM->clear();
0117   }
0118 
0119   /// Mark MemorySSA as used so we can invalidate self if MSSA is invalidated.
0120   void markMSSAUsed() { MSSAUsed = true; }
0121 
0122   /// Accessor for the analysis manager.
0123   LoopAnalysisManager &getManager() { return *InnerAM; }
0124 
0125   /// Handler for invalidation of the proxy for a particular function.
0126   ///
0127   /// If the proxy, \c LoopInfo, and associated analyses are preserved, this
0128   /// will merely forward the invalidation event to any cached loop analysis
0129   /// results for loops within this function.
0130   ///
0131   /// If the necessary loop infrastructure is not preserved, this will forcibly
0132   /// clear all of the cached analysis results that are keyed on the \c
0133   /// LoopInfo for this function.
0134   bool invalidate(Function &F, const PreservedAnalyses &PA,
0135                   FunctionAnalysisManager::Invalidator &Inv);
0136 
0137 private:
0138   LoopAnalysisManager *InnerAM;
0139   LoopInfo *LI;
0140   bool MSSAUsed = false;
0141 };
0142 
0143 /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
0144 /// so it can pass the \c LoopInfo to the result.
0145 template <>
0146 LoopAnalysisManagerFunctionProxy::Result
0147 LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
0148 
0149 // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
0150 // template.
0151 extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
0152 
0153 extern template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
0154                                                 LoopStandardAnalysisResults &>;
0155 /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
0156 typedef OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
0157                                   LoopStandardAnalysisResults &>
0158     FunctionAnalysisManagerLoopProxy;
0159 
0160 /// Returns the minimum set of Analyses that all loop passes must preserve.
0161 PreservedAnalyses getLoopPassPreservedAnalyses();
0162 }
0163 
0164 #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H