Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines stuff that is used to define and "use" Analysis Passes. |
| 10 | // This file is automatically #included by Pass.h, so: |
| 11 | // |
| 12 | // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY |
| 13 | // |
| 14 | // Instead, #include Pass.h |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #if !defined(LLVM_PASS_H) || defined(LLVM_PASSANALYSISSUPPORT_H) |
| 19 | #error "Do not include <PassAnalysisSupport.h>; include <Pass.h> instead" |
| 20 | #endif |
| 21 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | #ifndef LLVM_PASSANALYSISSUPPORT_H |
| 23 | #define LLVM_PASSANALYSISSUPPORT_H |
| 24 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 25 | #include "llvm/ADT/SmallVector.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 26 | #include <cassert> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 27 | #include <tuple> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 28 | #include <utility> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | class Function; |
| 34 | class Pass; |
| 35 | class PMDataManager; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 36 | class StringRef; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | /// Represent the analysis usage information of a pass. This tracks analyses |
| 40 | /// that the pass REQUIRES (must be available when the pass runs), REQUIRES |
| 41 | /// TRANSITIVE (must be available throughout the lifetime of the pass), and |
| 42 | /// analyses that the pass PRESERVES (the pass does not invalidate the results |
| 43 | /// of these analyses). This information is provided by a pass to the Pass |
| 44 | /// infrastructure through the getAnalysisUsage virtual function. |
| 45 | /// |
| 46 | class AnalysisUsage { |
| 47 | public: |
| 48 | using VectorType = SmallVectorImpl<AnalysisID>; |
| 49 | |
| 50 | private: |
| 51 | /// Sets of analyses required and preserved by a pass |
| 52 | // TODO: It's not clear that SmallVector is an appropriate data structure for |
| 53 | // this usecase. The sizes were picked to minimize wasted space, but are |
| 54 | // otherwise fairly meaningless. |
| 55 | SmallVector<AnalysisID, 8> Required; |
| 56 | SmallVector<AnalysisID, 2> RequiredTransitive; |
| 57 | SmallVector<AnalysisID, 2> Preserved; |
| 58 | SmallVector<AnalysisID, 0> Used; |
| 59 | bool PreservesAll = false; |
| 60 | |
| 61 | public: |
| 62 | AnalysisUsage() = default; |
| 63 | |
| 64 | ///@{ |
| 65 | /// Add the specified ID to the required set of the usage info for a pass. |
| 66 | AnalysisUsage &addRequiredID(const void *ID); |
| 67 | AnalysisUsage &addRequiredID(char &ID); |
| 68 | template<class PassClass> |
| 69 | AnalysisUsage &addRequired() { |
| 70 | return addRequiredID(PassClass::ID); |
| 71 | } |
| 72 | |
| 73 | AnalysisUsage &addRequiredTransitiveID(char &ID); |
| 74 | template<class PassClass> |
| 75 | AnalysisUsage &addRequiredTransitive() { |
| 76 | return addRequiredTransitiveID(PassClass::ID); |
| 77 | } |
| 78 | ///@} |
| 79 | |
| 80 | ///@{ |
| 81 | /// Add the specified ID to the set of analyses preserved by this pass. |
| 82 | AnalysisUsage &addPreservedID(const void *ID) { |
| 83 | Preserved.push_back(ID); |
| 84 | return *this; |
| 85 | } |
| 86 | AnalysisUsage &addPreservedID(char &ID) { |
| 87 | Preserved.push_back(&ID); |
| 88 | return *this; |
| 89 | } |
| 90 | /// Add the specified Pass class to the set of analyses preserved by this pass. |
| 91 | template<class PassClass> |
| 92 | AnalysisUsage &addPreserved() { |
| 93 | Preserved.push_back(&PassClass::ID); |
| 94 | return *this; |
| 95 | } |
| 96 | ///@} |
| 97 | |
| 98 | ///@{ |
| 99 | /// Add the specified ID to the set of analyses used by this pass if they are |
| 100 | /// available.. |
| 101 | AnalysisUsage &addUsedIfAvailableID(const void *ID) { |
| 102 | Used.push_back(ID); |
| 103 | return *this; |
| 104 | } |
| 105 | AnalysisUsage &addUsedIfAvailableID(char &ID) { |
| 106 | Used.push_back(&ID); |
| 107 | return *this; |
| 108 | } |
| 109 | /// Add the specified Pass class to the set of analyses used by this pass. |
| 110 | template<class PassClass> |
| 111 | AnalysisUsage &addUsedIfAvailable() { |
| 112 | Used.push_back(&PassClass::ID); |
| 113 | return *this; |
| 114 | } |
| 115 | ///@} |
| 116 | |
| 117 | /// Add the Pass with the specified argument string to the set of analyses |
| 118 | /// preserved by this pass. If no such Pass exists, do nothing. This can be |
| 119 | /// useful when a pass is trivially preserved, but may not be linked in. Be |
| 120 | /// careful about spelling! |
| 121 | AnalysisUsage &addPreserved(StringRef Arg); |
| 122 | |
| 123 | /// Set by analyses that do not transform their input at all |
| 124 | void setPreservesAll() { PreservesAll = true; } |
| 125 | |
| 126 | /// Determine whether a pass said it does not transform its input at all |
| 127 | bool getPreservesAll() const { return PreservesAll; } |
| 128 | |
| 129 | /// This function should be called by the pass, iff they do not: |
| 130 | /// |
| 131 | /// 1. Add or remove basic blocks from the function |
| 132 | /// 2. Modify terminator instructions in any way. |
| 133 | /// |
| 134 | /// This function annotates the AnalysisUsage info object to say that analyses |
| 135 | /// that only depend on the CFG are preserved by this pass. |
| 136 | void setPreservesCFG(); |
| 137 | |
| 138 | const VectorType &getRequiredSet() const { return Required; } |
| 139 | const VectorType &getRequiredTransitiveSet() const { |
| 140 | return RequiredTransitive; |
| 141 | } |
| 142 | const VectorType &getPreservedSet() const { return Preserved; } |
| 143 | const VectorType &getUsedSet() const { return Used; } |
| 144 | }; |
| 145 | |
| 146 | //===----------------------------------------------------------------------===// |
| 147 | /// AnalysisResolver - Simple interface used by Pass objects to pull all |
| 148 | /// analysis information out of pass manager that is responsible to manage |
| 149 | /// the pass. |
| 150 | /// |
| 151 | class AnalysisResolver { |
| 152 | public: |
| 153 | AnalysisResolver() = delete; |
| 154 | explicit AnalysisResolver(PMDataManager &P) : PM(P) {} |
| 155 | |
| 156 | PMDataManager &getPMDataManager() { return PM; } |
| 157 | |
| 158 | /// Find pass that is implementing PI. |
| 159 | Pass *findImplPass(AnalysisID PI) { |
| 160 | Pass *ResultPass = nullptr; |
| 161 | for (const auto &AnalysisImpl : AnalysisImpls) { |
| 162 | if (AnalysisImpl.first == PI) { |
| 163 | ResultPass = AnalysisImpl.second; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | return ResultPass; |
| 168 | } |
| 169 | |
| 170 | /// Find pass that is implementing PI. Initialize pass for Function F. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 171 | std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI, Function &F); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 172 | |
| 173 | void addAnalysisImplsPair(AnalysisID PI, Pass *P) { |
| 174 | if (findImplPass(PI) == P) |
| 175 | return; |
| 176 | std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P); |
| 177 | AnalysisImpls.push_back(pir); |
| 178 | } |
| 179 | |
| 180 | /// Clear cache that is used to connect a pass to the analysis (PassInfo). |
| 181 | void clearAnalysisImpls() { |
| 182 | AnalysisImpls.clear(); |
| 183 | } |
| 184 | |
| 185 | /// Return analysis result or null if it doesn't exist. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 186 | Pass *getAnalysisIfAvailable(AnalysisID ID) const; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 187 | |
| 188 | private: |
| 189 | /// This keeps track of which passes implements the interfaces that are |
| 190 | /// required by the current pass (to implement getAnalysis()). |
| 191 | std::vector<std::pair<AnalysisID, Pass *>> AnalysisImpls; |
| 192 | |
| 193 | /// PassManager that is used to resolve analysis info |
| 194 | PMDataManager &PM; |
| 195 | }; |
| 196 | |
| 197 | /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to |
| 198 | /// get analysis information that might be around, for example to update it. |
| 199 | /// This is different than getAnalysis in that it can fail (if the analysis |
| 200 | /// results haven't been computed), so should only be used if you can handle |
| 201 | /// the case when the analysis is not available. This method is often used by |
| 202 | /// transformation APIs to update analysis results for a pass automatically as |
| 203 | /// the transform is performed. |
| 204 | template<typename AnalysisType> |
| 205 | AnalysisType *Pass::getAnalysisIfAvailable() const { |
| 206 | assert(Resolver && "Pass not resident in a PassManager object!"); |
| 207 | |
| 208 | const void *PI = &AnalysisType::ID; |
| 209 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 210 | Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 211 | if (!ResultPass) return nullptr; |
| 212 | |
| 213 | // Because the AnalysisType may not be a subclass of pass (for |
| 214 | // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially |
| 215 | // adjust the return pointer (because the class may multiply inherit, once |
| 216 | // from pass, once from AnalysisType). |
| 217 | return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); |
| 218 | } |
| 219 | |
| 220 | /// getAnalysis<AnalysisType>() - This function is used by subclasses to get |
| 221 | /// to the analysis information that they claim to use by overriding the |
| 222 | /// getAnalysisUsage function. |
| 223 | template<typename AnalysisType> |
| 224 | AnalysisType &Pass::getAnalysis() const { |
| 225 | assert(Resolver && "Pass has not been inserted into a PassManager object!"); |
| 226 | return getAnalysisID<AnalysisType>(&AnalysisType::ID); |
| 227 | } |
| 228 | |
| 229 | template<typename AnalysisType> |
| 230 | AnalysisType &Pass::getAnalysisID(AnalysisID PI) const { |
| 231 | assert(PI && "getAnalysis for unregistered pass!"); |
| 232 | assert(Resolver&&"Pass has not been inserted into a PassManager object!"); |
| 233 | // PI *must* appear in AnalysisImpls. Because the number of passes used |
| 234 | // should be a small number, we just do a linear search over a (dense) |
| 235 | // vector. |
| 236 | Pass *ResultPass = Resolver->findImplPass(PI); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 237 | assert(ResultPass && |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 238 | "getAnalysis*() called on an analysis that was not " |
| 239 | "'required' by pass!"); |
| 240 | |
| 241 | // Because the AnalysisType may not be a subclass of pass (for |
| 242 | // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially |
| 243 | // adjust the return pointer (because the class may multiply inherit, once |
| 244 | // from pass, once from AnalysisType). |
| 245 | return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); |
| 246 | } |
| 247 | |
| 248 | /// getAnalysis<AnalysisType>() - This function is used by subclasses to get |
| 249 | /// to the analysis information that they claim to use by overriding the |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 250 | /// getAnalysisUsage function. If as part of the dependencies, an IR |
| 251 | /// transformation is triggered (e.g. because the analysis requires |
| 252 | /// BreakCriticalEdges), and Changed is non null, *Changed is updated. |
| 253 | template <typename AnalysisType> |
| 254 | AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 255 | assert(Resolver &&"Pass has not been inserted into a PassManager object!"); |
| 256 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 257 | return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 260 | template <typename AnalysisType> |
| 261 | AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F, bool *Changed) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 262 | assert(PI && "getAnalysis for unregistered pass!"); |
| 263 | assert(Resolver && "Pass has not been inserted into a PassManager object!"); |
| 264 | // PI *must* appear in AnalysisImpls. Because the number of passes used |
| 265 | // should be a small number, we just do a linear search over a (dense) |
| 266 | // vector. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 267 | Pass *ResultPass; |
| 268 | bool LocalChanged; |
| 269 | std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F); |
| 270 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 271 | assert(ResultPass && "Unable to find requested analysis info"); |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 272 | if (Changed) |
| 273 | *Changed |= LocalChanged; |
| 274 | else |
| 275 | assert(!LocalChanged && |
| 276 | "A pass trigged a code update but the update status is lost"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 277 | |
| 278 | // Because the AnalysisType may not be a subclass of pass (for |
| 279 | // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially |
| 280 | // adjust the return pointer (because the class may multiply inherit, once |
| 281 | // from pass, once from AnalysisType). |
| 282 | return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI); |
| 283 | } |
| 284 | |
| 285 | } // end namespace llvm |
| 286 | |
| 287 | #endif // LLVM_PASSANALYSISSUPPORT_H |