IntervalIterator.h 10 KB

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