IntervalIterator.h 11 KB

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