Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
9.0 KiB

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