PassAnalysisSupport.h 11 KB

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