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.

304 lines
9.9 KiB

  1. //===- PathNumbering.h ----------------------------------------*- 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. // Ball-Larus path numbers uniquely identify paths through a directed acyclic
  11. // graph (DAG) [Ball96]. For a CFG backedges are removed and replaced by phony
  12. // edges to obtain a DAG, and thus the unique path numbers [Ball96].
  13. //
  14. // The purpose of this analysis is to enumerate the edges in a CFG in order
  15. // to obtain paths from path numbers in a convenient manner. As described in
  16. // [Ball96] edges can be enumerated such that given a path number by following
  17. // the CFG and updating the path number, the path is obtained.
  18. //
  19. // [Ball96]
  20. // T. Ball and J. R. Larus. "Efficient Path Profiling."
  21. // International Symposium on Microarchitecture, pages 46-57, 1996.
  22. // http://portal.acm.org/citation.cfm?id=243857
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_ANALYSIS_PATHNUMBERING_H
  26. #define LLVM_ANALYSIS_PATHNUMBERING_H
  27. #include "llvm/Analysis/ProfileInfoTypes.h"
  28. #include "llvm/IR/BasicBlock.h"
  29. #include "llvm/IR/Instructions.h"
  30. #include "llvm/Pass.h"
  31. #include "llvm/Support/CFG.h"
  32. #include <map>
  33. #include <stack>
  34. #include <vector>
  35. namespace llvm {
  36. class BallLarusNode;
  37. class BallLarusEdge;
  38. class BallLarusDag;
  39. // typedefs for storage/ interators of various DAG components
  40. typedef std::vector<BallLarusNode*> BLNodeVector;
  41. typedef std::vector<BallLarusNode*>::iterator BLNodeIterator;
  42. typedef std::vector<BallLarusEdge*> BLEdgeVector;
  43. typedef std::vector<BallLarusEdge*>::iterator BLEdgeIterator;
  44. typedef std::map<BasicBlock*, BallLarusNode*> BLBlockNodeMap;
  45. typedef std::stack<BallLarusNode*> BLNodeStack;
  46. // Represents a basic block with information necessary for the BallLarus
  47. // algorithms.
  48. class BallLarusNode {
  49. public:
  50. enum NodeColor { WHITE, GRAY, BLACK };
  51. // Constructor: Initializes a new Node for the given BasicBlock
  52. BallLarusNode(BasicBlock* BB) :
  53. _basicBlock(BB), _numberPaths(0), _color(WHITE) {
  54. static unsigned nextUID = 0;
  55. _uid = nextUID++;
  56. }
  57. // Returns the basic block for the BallLarusNode
  58. BasicBlock* getBlock();
  59. // Get/set the number of paths to the exit starting at the node.
  60. unsigned getNumberPaths();
  61. void setNumberPaths(unsigned numberPaths);
  62. // Get/set the NodeColor used in graph algorithms.
  63. NodeColor getColor();
  64. void setColor(NodeColor color);
  65. // Iterator information for predecessor edges. Includes phony and
  66. // backedges.
  67. BLEdgeIterator predBegin();
  68. BLEdgeIterator predEnd();
  69. unsigned getNumberPredEdges();
  70. // Iterator information for successor edges. Includes phony and
  71. // backedges.
  72. BLEdgeIterator succBegin();
  73. BLEdgeIterator succEnd();
  74. unsigned getNumberSuccEdges();
  75. // Add an edge to the predecessor list.
  76. void addPredEdge(BallLarusEdge* edge);
  77. // Remove an edge from the predecessor list.
  78. void removePredEdge(BallLarusEdge* edge);
  79. // Add an edge to the successor list.
  80. void addSuccEdge(BallLarusEdge* edge);
  81. // Remove an edge from the successor list.
  82. void removeSuccEdge(BallLarusEdge* edge);
  83. // Returns the name of the BasicBlock being represented. If BasicBlock
  84. // is null then returns "<null>". If BasicBlock has no name, then
  85. // "<unnamed>" is returned. Intended for use with debug output.
  86. std::string getName();
  87. private:
  88. // The corresponding underlying BB.
  89. BasicBlock* _basicBlock;
  90. // Holds the predecessor edges of this node.
  91. BLEdgeVector _predEdges;
  92. // Holds the successor edges of this node.
  93. BLEdgeVector _succEdges;
  94. // The number of paths from the node to the exit.
  95. unsigned _numberPaths;
  96. // 'Color' used by graph algorithms to mark the node.
  97. NodeColor _color;
  98. // Unique ID to ensure naming difference with dotgraphs
  99. unsigned _uid;
  100. // Removes an edge from an edgeVector. Used by removePredEdge and
  101. // removeSuccEdge.
  102. void removeEdge(BLEdgeVector& v, BallLarusEdge* e);
  103. };
  104. // Represents an edge in the Dag. For an edge, v -> w, v is the source, and
  105. // w is the target.
  106. class BallLarusEdge {
  107. public:
  108. enum EdgeType { NORMAL, BACKEDGE, SPLITEDGE,
  109. BACKEDGE_PHONY, SPLITEDGE_PHONY, CALLEDGE_PHONY };
  110. // Constructor: Initializes an BallLarusEdge with a source and target.
  111. BallLarusEdge(BallLarusNode* source, BallLarusNode* target,
  112. unsigned duplicateNumber)
  113. : _source(source), _target(target), _weight(0), _edgeType(NORMAL),
  114. _realEdge(NULL), _duplicateNumber(duplicateNumber) {}
  115. // Returns the source/ target node of this edge.
  116. BallLarusNode* getSource() const;
  117. BallLarusNode* getTarget() const;
  118. // Sets the type of the edge.
  119. EdgeType getType() const;
  120. // Gets the type of the edge.
  121. void setType(EdgeType type);
  122. // Returns the weight of this edge. Used to decode path numbers to
  123. // sequences of basic blocks.
  124. unsigned getWeight();
  125. // Sets the weight of the edge. Used during path numbering.
  126. void setWeight(unsigned weight);
  127. // Gets/sets the phony edge originating at the root.
  128. BallLarusEdge* getPhonyRoot();
  129. void setPhonyRoot(BallLarusEdge* phonyRoot);
  130. // Gets/sets the phony edge terminating at the exit.
  131. BallLarusEdge* getPhonyExit();
  132. void setPhonyExit(BallLarusEdge* phonyExit);
  133. // Gets/sets the associated real edge if this is a phony edge.
  134. BallLarusEdge* getRealEdge();
  135. void setRealEdge(BallLarusEdge* realEdge);
  136. // Returns the duplicate number of the edge.
  137. unsigned getDuplicateNumber();
  138. protected:
  139. // Source node for this edge.
  140. BallLarusNode* _source;
  141. // Target node for this edge.
  142. BallLarusNode* _target;
  143. private:
  144. // Edge weight cooresponding to path number increments before removing
  145. // increments along a spanning tree. The sum over the edge weights gives
  146. // the path number.
  147. unsigned _weight;
  148. // Type to represent for what this edge is intended
  149. EdgeType _edgeType;
  150. // For backedges and split-edges, the phony edge which is linked to the
  151. // root node of the DAG. This contains a path number initialization.
  152. BallLarusEdge* _phonyRoot;
  153. // For backedges and split-edges, the phony edge which is linked to the
  154. // exit node of the DAG. This contains a path counter increment, and
  155. // potentially a path number increment.
  156. BallLarusEdge* _phonyExit;
  157. // If this is a phony edge, _realEdge is a link to the back or split
  158. // edge. Otherwise, this is null.
  159. BallLarusEdge* _realEdge;
  160. // An ID to differentiate between those edges which have the same source
  161. // and destination blocks.
  162. unsigned _duplicateNumber;
  163. };
  164. // Represents the Ball Larus DAG for a given Function. Can calculate
  165. // various properties required for instrumentation or analysis. E.g. the
  166. // edge weights that determine the path number.
  167. class BallLarusDag {
  168. public:
  169. // Initializes a BallLarusDag from the CFG of a given function. Must
  170. // call init() after creation, since some initialization requires
  171. // virtual functions.
  172. BallLarusDag(Function &F)
  173. : _root(NULL), _exit(NULL), _function(F) {}
  174. // Initialization that requires virtual functions which are not fully
  175. // functional in the constructor.
  176. void init();
  177. // Frees all memory associated with the DAG.
  178. virtual ~BallLarusDag();
  179. // Calculate the path numbers by assigning edge increments as prescribed
  180. // in Ball-Larus path profiling.
  181. void calculatePathNumbers();
  182. // Returns the number of paths for the DAG.
  183. unsigned getNumberOfPaths();
  184. // Returns the root (i.e. entry) node for the DAG.
  185. BallLarusNode* getRoot();
  186. // Returns the exit node for the DAG.
  187. BallLarusNode* getExit();
  188. // Returns the function for the DAG.
  189. Function& getFunction();
  190. // Clears the node colors.
  191. void clearColors(BallLarusNode::NodeColor color);
  192. protected:
  193. // All nodes in the DAG.
  194. BLNodeVector _nodes;
  195. // All edges in the DAG.
  196. BLEdgeVector _edges;
  197. // All backedges in the DAG.
  198. BLEdgeVector _backEdges;
  199. // Allows subclasses to determine which type of Node is created.
  200. // Override this method to produce subclasses of BallLarusNode if
  201. // necessary. The destructor of BallLarusDag will call free on each pointer
  202. // created.
  203. virtual BallLarusNode* createNode(BasicBlock* BB);
  204. // Allows subclasses to determine which type of Edge is created.
  205. // Override this method to produce subclasses of BallLarusEdge if
  206. // necessary. Parameters source and target will have been created by
  207. // createNode and can be cast to the subclass of BallLarusNode*
  208. // returned by createNode. The destructor of BallLarusDag will call free
  209. // on each pointer created.
  210. virtual BallLarusEdge* createEdge(BallLarusNode* source, BallLarusNode*
  211. target, unsigned duplicateNumber);
  212. // Proxy to node's constructor. Updates the DAG state.
  213. BallLarusNode* addNode(BasicBlock* BB);
  214. // Proxy to edge's constructor. Updates the DAG state.
  215. BallLarusEdge* addEdge(BallLarusNode* source, BallLarusNode* target,
  216. unsigned duplicateNumber);
  217. private:
  218. // The root (i.e. entry) node for this DAG.
  219. BallLarusNode* _root;
  220. // The exit node for this DAG.
  221. BallLarusNode* _exit;
  222. // The function represented by this DAG.
  223. Function& _function;
  224. // Processes one node and its imediate edges for building the DAG.
  225. void buildNode(BLBlockNodeMap& inDag, std::stack<BallLarusNode*>& dfsStack);
  226. // Process an edge in the CFG for DAG building.
  227. void buildEdge(BLBlockNodeMap& inDag, std::stack<BallLarusNode*>& dfsStack,
  228. BallLarusNode* currentNode, BasicBlock* succBB,
  229. unsigned duplicateNumber);
  230. // The weight on each edge is the increment required along any path that
  231. // contains that edge.
  232. void calculatePathNumbersFrom(BallLarusNode* node);
  233. // Adds a backedge with its phony edges. Updates the DAG state.
  234. void addBackedge(BallLarusNode* source, BallLarusNode* target,
  235. unsigned duplicateCount);
  236. };
  237. } // end namespace llvm
  238. #endif