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.

259 lines
10 KiB

  1. //===- IntervalIterator.h - Interval Iterator Declaration -------*- 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 an iterator that enumerates the intervals in a control flow
  11. // graph of some sort. This iterator is parametric, allowing iterator over the
  12. // following types of graphs:
  13. //
  14. // 1. A Function* object, composed of BasicBlock nodes.
  15. // 2. An IntervalPartition& object, composed of Interval nodes.
  16. //
  17. // This iterator is defined to walk the control flow graph, returning intervals
  18. // in depth first order. These intervals are completely filled in except for
  19. // the predecessor fields (the successor information is filled in however).
  20. //
  21. // By default, the intervals created by this iterator are deleted after they
  22. // are no longer any use to the iterator. This behavior can be changed by
  23. // passing a false value into the intervals_begin() function. This causes the
  24. // IOwnMem member to be set, and the intervals to not be deleted.
  25. //
  26. // It is only safe to use this if all of the intervals are deleted by the caller
  27. // and all of the intervals are processed. However, the user of the iterator is
  28. // not allowed to modify or delete the intervals until after the iterator has
  29. // been used completely. The IntervalPartition class uses this functionality.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_ANALYSIS_INTERVALITERATOR_H
  33. #define LLVM_ANALYSIS_INTERVALITERATOR_H
  34. #include "llvm/Analysis/IntervalPartition.h"
  35. #include "llvm/IR/Function.h"
  36. #include "llvm/Support/CFG.h"
  37. #include <algorithm>
  38. #include <set>
  39. #include <vector>
  40. namespace llvm {
  41. // getNodeHeader - Given a source graph node and the source graph, return the
  42. // BasicBlock that is the header node. This is the opposite of
  43. // getSourceGraphNode.
  44. //
  45. inline BasicBlock *getNodeHeader(BasicBlock *BB) { return BB; }
  46. inline BasicBlock *getNodeHeader(Interval *I) { return I->getHeaderNode(); }
  47. // getSourceGraphNode - Given a BasicBlock and the source graph, return the
  48. // source graph node that corresponds to the BasicBlock. This is the opposite
  49. // of getNodeHeader.
  50. //
  51. inline BasicBlock *getSourceGraphNode(Function *, BasicBlock *BB) {
  52. return BB;
  53. }
  54. inline Interval *getSourceGraphNode(IntervalPartition *IP, BasicBlock *BB) {
  55. return IP->getBlockInterval(BB);
  56. }
  57. // addNodeToInterval - This method exists to assist the generic ProcessNode
  58. // with the task of adding a node to the new interval, depending on the
  59. // type of the source node. In the case of a CFG source graph (BasicBlock
  60. // case), the BasicBlock itself is added to the interval.
  61. //
  62. inline void addNodeToInterval(Interval *Int, BasicBlock *BB) {
  63. Int->Nodes.push_back(BB);
  64. }
  65. // addNodeToInterval - This method exists to assist the generic ProcessNode
  66. // with the task of adding a node to the new interval, depending on the
  67. // type of the source node. In the case of a CFG source graph (BasicBlock
  68. // case), the BasicBlock itself is added to the interval. In the case of
  69. // an IntervalPartition source graph (Interval case), all of the member
  70. // BasicBlocks are added to the interval.
  71. //
  72. inline void addNodeToInterval(Interval *Int, Interval *I) {
  73. // Add all of the nodes in I as new nodes in Int.
  74. copy(I->Nodes.begin(), I->Nodes.end(), back_inserter(Int->Nodes));
  75. }
  76. template<class NodeTy, class OrigContainer_t, class GT = GraphTraits<NodeTy*>,
  77. class IGT = GraphTraits<Inverse<NodeTy*> > >
  78. class IntervalIterator {
  79. std::vector<std::pair<Interval*, typename Interval::succ_iterator> > IntStack;
  80. std::set<BasicBlock*> Visited;
  81. OrigContainer_t *OrigContainer;
  82. bool IOwnMem; // If True, delete intervals when done with them
  83. // See file header for conditions of use
  84. public:
  85. typedef IntervalIterator<NodeTy, OrigContainer_t> _Self;
  86. typedef std::forward_iterator_tag iterator_category;
  87. IntervalIterator() {} // End iterator, empty stack
  88. IntervalIterator(Function *M, bool OwnMemory) : IOwnMem(OwnMemory) {
  89. OrigContainer = M;
  90. if (!ProcessInterval(&M->front())) {
  91. llvm_unreachable("ProcessInterval should never fail for first interval!");
  92. }
  93. }
  94. IntervalIterator(IntervalPartition &IP, bool OwnMemory) : IOwnMem(OwnMemory) {
  95. OrigContainer = &IP;
  96. if (!ProcessInterval(IP.getRootInterval())) {
  97. llvm_unreachable("ProcessInterval should never fail for first interval!");
  98. }
  99. }
  100. inline ~IntervalIterator() {
  101. if (IOwnMem)
  102. while (!IntStack.empty()) {
  103. delete operator*();
  104. IntStack.pop_back();
  105. }
  106. }
  107. inline bool operator==(const _Self& x) const { return IntStack == x.IntStack;}
  108. inline bool operator!=(const _Self& x) const { return !operator==(x); }
  109. inline const Interval *operator*() const { return IntStack.back().first; }
  110. inline Interval *operator*() { return IntStack.back().first; }
  111. inline const Interval *operator->() const { return operator*(); }
  112. inline Interval *operator->() { return operator*(); }
  113. _Self& operator++() { // Preincrement
  114. assert(!IntStack.empty() && "Attempting to use interval iterator at end!");
  115. do {
  116. // All of the intervals on the stack have been visited. Try visiting
  117. // their successors now.
  118. Interval::succ_iterator &SuccIt = IntStack.back().second,
  119. EndIt = succ_end(IntStack.back().first);
  120. while (SuccIt != EndIt) { // Loop over all interval succs
  121. bool Done = ProcessInterval(getSourceGraphNode(OrigContainer, *SuccIt));
  122. ++SuccIt; // Increment iterator
  123. if (Done) return *this; // Found a new interval! Use it!
  124. }
  125. // Free interval memory... if necessary
  126. if (IOwnMem) delete IntStack.back().first;
  127. // We ran out of successors for this interval... pop off the stack
  128. IntStack.pop_back();
  129. } while (!IntStack.empty());
  130. return *this;
  131. }
  132. inline _Self operator++(int) { // Postincrement
  133. _Self tmp = *this; ++*this; return tmp;
  134. }
  135. private:
  136. // ProcessInterval - This method is used during the construction of the
  137. // interval graph. It walks through the source graph, recursively creating
  138. // an interval per invocation until the entire graph is covered. This uses
  139. // the ProcessNode method to add all of the nodes to the interval.
  140. //
  141. // This method is templated because it may operate on two different source
  142. // graphs: a basic block graph, or a preexisting interval graph.
  143. //
  144. bool ProcessInterval(NodeTy *Node) {
  145. BasicBlock *Header = getNodeHeader(Node);
  146. if (Visited.count(Header)) return false;
  147. Interval *Int = new Interval(Header);
  148. Visited.insert(Header); // The header has now been visited!
  149. // Check all of our successors to see if they are in the interval...
  150. for (typename GT::ChildIteratorType I = GT::child_begin(Node),
  151. E = GT::child_end(Node); I != E; ++I)
  152. ProcessNode(Int, getSourceGraphNode(OrigContainer, *I));
  153. IntStack.push_back(std::make_pair(Int, succ_begin(Int)));
  154. return true;
  155. }
  156. // ProcessNode - This method is called by ProcessInterval to add nodes to the
  157. // interval being constructed, and it is also called recursively as it walks
  158. // the source graph. A node is added to the current interval only if all of
  159. // its predecessors are already in the graph. This also takes care of keeping
  160. // the successor set of an interval up to date.
  161. //
  162. // This method is templated because it may operate on two different source
  163. // graphs: a basic block graph, or a preexisting interval graph.
  164. //
  165. void ProcessNode(Interval *Int, NodeTy *Node) {
  166. assert(Int && "Null interval == bad!");
  167. assert(Node && "Null Node == bad!");
  168. BasicBlock *NodeHeader = getNodeHeader(Node);
  169. if (Visited.count(NodeHeader)) { // Node already been visited?
  170. if (Int->contains(NodeHeader)) { // Already in this interval...
  171. return;
  172. } else { // In other interval, add as successor
  173. if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
  174. Int->Successors.push_back(NodeHeader);
  175. }
  176. } else { // Otherwise, not in interval yet
  177. for (typename IGT::ChildIteratorType I = IGT::child_begin(Node),
  178. E = IGT::child_end(Node); I != E; ++I) {
  179. if (!Int->contains(*I)) { // If pred not in interval, we can't be
  180. if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
  181. Int->Successors.push_back(NodeHeader);
  182. return; // See you later
  183. }
  184. }
  185. // If we get here, then all of the predecessors of BB are in the interval
  186. // already. In this case, we must add BB to the interval!
  187. addNodeToInterval(Int, Node);
  188. Visited.insert(NodeHeader); // The node has now been visited!
  189. if (Int->isSuccessor(NodeHeader)) {
  190. // If we were in the successor list from before... remove from succ list
  191. Int->Successors.erase(std::remove(Int->Successors.begin(),
  192. Int->Successors.end(), NodeHeader),
  193. Int->Successors.end());
  194. }
  195. // Now that we have discovered that Node is in the interval, perhaps some
  196. // of its successors are as well?
  197. for (typename GT::ChildIteratorType It = GT::child_begin(Node),
  198. End = GT::child_end(Node); It != End; ++It)
  199. ProcessNode(Int, getSourceGraphNode(OrigContainer, *It));
  200. }
  201. }
  202. };
  203. typedef IntervalIterator<BasicBlock, Function> function_interval_iterator;
  204. typedef IntervalIterator<Interval, IntervalPartition>
  205. interval_part_interval_iterator;
  206. inline function_interval_iterator intervals_begin(Function *F,
  207. bool DeleteInts = true) {
  208. return function_interval_iterator(F, DeleteInts);
  209. }
  210. inline function_interval_iterator intervals_end(Function *) {
  211. return function_interval_iterator();
  212. }
  213. inline interval_part_interval_iterator
  214. intervals_begin(IntervalPartition &IP, bool DeleteIntervals = true) {
  215. return interval_part_interval_iterator(IP, DeleteIntervals);
  216. }
  217. inline interval_part_interval_iterator intervals_end(IntervalPartition &IP) {
  218. return interval_part_interval_iterator();
  219. }
  220. } // End llvm namespace
  221. #endif