IntervalIterator.h 11 KB

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