PassAnalysisSupport.h 11 KB

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