PassAnalysisSupport.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines stuff that is used to define and "use" Analysis Passes.
  11. // This file is automatically #included by Pass.h, so:
  12. //
  13. // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
  14. //
  15. // Instead, #include Pass.h
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_PASSANALYSISSUPPORT_H
  19. #define LLVM_PASSANALYSISSUPPORT_H
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Pass.h"
  23. #include <vector>
  24. namespace llvm {
  25. //===----------------------------------------------------------------------===//
  26. /// Represent the analysis usage information of a pass. This tracks analyses
  27. /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
  28. /// TRANSITIVE (must be available throughout the lifetime of the pass), and
  29. /// analyses that the pass PRESERVES (the pass does not invalidate the results
  30. /// of these analyses). This information is provided by a pass to the Pass
  31. /// infrastructure through the getAnalysisUsage virtual function.
  32. ///
  33. class AnalysisUsage {
  34. public:
  35. typedef SmallVector<AnalysisID, 32> VectorType;
  36. private:
  37. /// Sets of analyses required and preserved by a pass
  38. VectorType Required, RequiredTransitive, Preserved;
  39. bool PreservesAll;
  40. public:
  41. AnalysisUsage() : PreservesAll(false) {}
  42. ///@{
  43. /// Add the specified ID to the required set of the usage info for a pass.
  44. AnalysisUsage &addRequiredID(const void *ID);
  45. AnalysisUsage &addRequiredID(char &ID);
  46. template<class PassClass>
  47. AnalysisUsage &addRequired() {
  48. return addRequiredID(PassClass::ID);
  49. }
  50. AnalysisUsage &addRequiredTransitiveID(char &ID);
  51. template<class PassClass>
  52. AnalysisUsage &addRequiredTransitive() {
  53. return addRequiredTransitiveID(PassClass::ID);
  54. }
  55. ///@}
  56. ///@{
  57. /// Add the specified ID to the set of analyses preserved by this pass.
  58. AnalysisUsage &addPreservedID(const void *ID) {
  59. Preserved.push_back(ID);
  60. return *this;
  61. }
  62. AnalysisUsage &addPreservedID(char &ID) {
  63. Preserved.push_back(&ID);
  64. return *this;
  65. }
  66. ///@}
  67. /// Add the specified Pass class to the set of analyses preserved by this pass.
  68. template<class PassClass>
  69. AnalysisUsage &addPreserved() {
  70. Preserved.push_back(&PassClass::ID);
  71. return *this;
  72. }
  73. /// Add the Pass with the specified argument string to the set of analyses
  74. /// preserved by this pass. If no such Pass exists, do nothing. This can be
  75. /// useful when a pass is trivially preserved, but may not be linked in. Be
  76. /// careful about spelling!
  77. AnalysisUsage &addPreserved(StringRef Arg);
  78. /// Set by analyses that do not transform their input at all
  79. void setPreservesAll() { PreservesAll = true; }
  80. /// Determine whether a pass said it does not transform its input at all
  81. bool getPreservesAll() const { return PreservesAll; }
  82. /// This function should be called by the pass, iff they do not:
  83. ///
  84. /// 1. Add or remove basic blocks from the function
  85. /// 2. Modify terminator instructions in any way.
  86. ///
  87. /// This function annotates the AnalysisUsage info object to say that analyses
  88. /// that only depend on the CFG are preserved by this pass.
  89. ///
  90. void setPreservesCFG();
  91. const VectorType &getRequiredSet() const { return Required; }
  92. const VectorType &getRequiredTransitiveSet() const {
  93. return RequiredTransitive;
  94. }
  95. const VectorType &getPreservedSet() const { return Preserved; }
  96. };
  97. // //
  98. ///////////////////////////////////////////////////////////////////////////////
  99. /// AnalysisResolver - Simple interface used by Pass objects to pull all
  100. /// analysis information out of pass manager that is responsible to manage
  101. /// the pass.
  102. ///
  103. class PMDataManager;
  104. class AnalysisResolver {
  105. private:
  106. AnalysisResolver() = delete;
  107. public:
  108. explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
  109. inline PMDataManager &getPMDataManager() { return PM; }
  110. /// Find pass that is implementing PI.
  111. Pass *findImplPass(AnalysisID PI) {
  112. Pass *ResultPass = nullptr;
  113. for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
  114. if (AnalysisImpls[i].first == PI) {
  115. ResultPass = AnalysisImpls[i].second;
  116. break;
  117. }
  118. }
  119. return ResultPass;
  120. }
  121. /// Find pass that is implementing PI. Initialize pass for Function F.
  122. Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
  123. void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
  124. if (findImplPass(PI) == P)
  125. return;
  126. std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
  127. AnalysisImpls.push_back(pir);
  128. }
  129. /// Clear cache that is used to connect a pass to the the analysis (PassInfo).
  130. void clearAnalysisImpls() {
  131. AnalysisImpls.clear();
  132. }
  133. /// Return analysis result or null if it doesn't exist.
  134. Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
  135. private:
  136. /// This keeps track of which passes implements the interfaces that are
  137. /// required by the current pass (to implement getAnalysis()).
  138. std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
  139. /// PassManager that is used to resolve analysis info
  140. PMDataManager &PM;
  141. };
  142. /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
  143. /// get analysis information that might be around, for example to update it.
  144. /// This is different than getAnalysis in that it can fail (if the analysis
  145. /// results haven't been computed), so should only be used if you can handle
  146. /// the case when the analysis is not available. This method is often used by
  147. /// transformation APIs to update analysis results for a pass automatically as
  148. /// the transform is performed.
  149. ///
  150. template<typename AnalysisType>
  151. AnalysisType *Pass::getAnalysisIfAvailable() const {
  152. assert(Resolver && "Pass not resident in a PassManager object!");
  153. const void *PI = &AnalysisType::ID;
  154. Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
  155. if (!ResultPass) return nullptr;
  156. // Because the AnalysisType may not be a subclass of pass (for
  157. // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
  158. // adjust the return pointer (because the class may multiply inherit, once
  159. // from pass, once from AnalysisType).
  160. return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
  161. }
  162. /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
  163. /// to the analysis information that they claim to use by overriding the
  164. /// getAnalysisUsage function.
  165. ///
  166. template<typename AnalysisType>
  167. AnalysisType &Pass::getAnalysis() const {
  168. assert(Resolver && "Pass has not been inserted into a PassManager object!");
  169. return getAnalysisID<AnalysisType>(&AnalysisType::ID);
  170. }
  171. template<typename AnalysisType>
  172. AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
  173. assert(PI && "getAnalysis for unregistered pass!");
  174. assert(Resolver&&"Pass has not been inserted into a PassManager object!");
  175. // PI *must* appear in AnalysisImpls. Because the number of passes used
  176. // should be a small number, we just do a linear search over a (dense)
  177. // vector.
  178. Pass *ResultPass = Resolver->findImplPass(PI);
  179. assert (ResultPass &&
  180. "getAnalysis*() called on an analysis that was not "
  181. "'required' by pass!");
  182. // Because the AnalysisType may not be a subclass of pass (for
  183. // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
  184. // adjust the return pointer (because the class may multiply inherit, once
  185. // from pass, once from AnalysisType).
  186. return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
  187. }
  188. /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
  189. /// to the analysis information that they claim to use by overriding the
  190. /// getAnalysisUsage function.
  191. ///
  192. template<typename AnalysisType>
  193. AnalysisType &Pass::getAnalysis(Function &F) {
  194. assert(Resolver &&"Pass has not been inserted into a PassManager object!");
  195. return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
  196. }
  197. template<typename AnalysisType>
  198. AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
  199. assert(PI && "getAnalysis for unregistered pass!");
  200. assert(Resolver && "Pass has not been inserted into a PassManager object!");
  201. // PI *must* appear in AnalysisImpls. Because the number of passes used
  202. // should be a small number, we just do a linear search over a (dense)
  203. // vector.
  204. Pass *ResultPass = Resolver->findImplPass(this, PI, F);
  205. assert(ResultPass && "Unable to find requested analysis info");
  206. // Because the AnalysisType may not be a subclass of pass (for
  207. // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
  208. // adjust the return pointer (because the class may multiply inherit, once
  209. // from pass, once from AnalysisType).
  210. return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
  211. }
  212. } // End llvm namespace
  213. #endif