Interval.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===- llvm/Analysis/Interval.h - Interval Class 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 contains the declaration of the Interval class, which
  11. // represents a set of CFG nodes and is a portion of an interval partition.
  12. //
  13. // Intervals have some interesting and useful properties, including the
  14. // following:
  15. // 1. The header node of an interval dominates all of the elements of the
  16. // interval
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ANALYSIS_INTERVAL_H
  20. #define LLVM_ANALYSIS_INTERVAL_H
  21. #include "llvm/ADT/GraphTraits.h"
  22. #include <vector>
  23. namespace llvm {
  24. class BasicBlock;
  25. class raw_ostream;
  26. // //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. /// Interval Class - An Interval is a set of nodes defined such that every node
  30. /// in the interval has all of its predecessors in the interval (except for the
  31. /// header)
  32. ///
  33. class Interval {
  34. /// HeaderNode - The header BasicBlock, which dominates all BasicBlocks in this
  35. /// interval. Also, any loops in this interval must go through the HeaderNode.
  36. ///
  37. BasicBlock *HeaderNode;
  38. public:
  39. typedef std::vector<BasicBlock*>::iterator succ_iterator;
  40. typedef std::vector<BasicBlock*>::iterator pred_iterator;
  41. typedef std::vector<BasicBlock*>::iterator node_iterator;
  42. inline Interval(BasicBlock *Header) : HeaderNode(Header) {
  43. Nodes.push_back(Header);
  44. }
  45. inline BasicBlock *getHeaderNode() const { return HeaderNode; }
  46. /// Nodes - The basic blocks in this interval.
  47. ///
  48. std::vector<BasicBlock*> Nodes;
  49. /// Successors - List of BasicBlocks that are reachable directly from nodes in
  50. /// this interval, but are not in the interval themselves.
  51. /// These nodes necessarily must be header nodes for other intervals.
  52. ///
  53. std::vector<BasicBlock*> Successors;
  54. /// Predecessors - List of BasicBlocks that have this Interval's header block
  55. /// as one of their successors.
  56. ///
  57. std::vector<BasicBlock*> Predecessors;
  58. /// contains - Find out if a basic block is in this interval
  59. inline bool contains(BasicBlock *BB) const {
  60. for (unsigned i = 0; i < Nodes.size(); ++i)
  61. if (Nodes[i] == BB) return true;
  62. return false;
  63. // I don't want the dependency on <algorithm>
  64. //return find(Nodes.begin(), Nodes.end(), BB) != Nodes.end();
  65. }
  66. /// isSuccessor - find out if a basic block is a successor of this Interval
  67. inline bool isSuccessor(BasicBlock *BB) const {
  68. for (unsigned i = 0; i < Successors.size(); ++i)
  69. if (Successors[i] == BB) return true;
  70. return false;
  71. // I don't want the dependency on <algorithm>
  72. //return find(Successors.begin(), Successors.end(), BB) != Successors.end();
  73. }
  74. /// Equality operator. It is only valid to compare two intervals from the
  75. /// same partition, because of this, all we have to check is the header node
  76. /// for equality.
  77. ///
  78. inline bool operator==(const Interval &I) const {
  79. return HeaderNode == I.HeaderNode;
  80. }
  81. /// isLoop - Find out if there is a back edge in this interval...
  82. bool isLoop() const;
  83. /// print - Show contents in human readable format...
  84. void print(raw_ostream &O) const;
  85. };
  86. /// succ_begin/succ_end - define methods so that Intervals may be used
  87. /// just like BasicBlocks can with the succ_* functions, and *::succ_iterator.
  88. ///
  89. inline Interval::succ_iterator succ_begin(Interval *I) {
  90. return I->Successors.begin();
  91. }
  92. inline Interval::succ_iterator succ_end(Interval *I) {
  93. return I->Successors.end();
  94. }
  95. /// pred_begin/pred_end - define methods so that Intervals may be used
  96. /// just like BasicBlocks can with the pred_* functions, and *::pred_iterator.
  97. ///
  98. inline Interval::pred_iterator pred_begin(Interval *I) {
  99. return I->Predecessors.begin();
  100. }
  101. inline Interval::pred_iterator pred_end(Interval *I) {
  102. return I->Predecessors.end();
  103. }
  104. template <> struct GraphTraits<Interval*> {
  105. typedef Interval NodeType;
  106. typedef Interval::succ_iterator ChildIteratorType;
  107. static NodeType *getEntryNode(Interval *I) { return I; }
  108. /// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  109. static inline ChildIteratorType child_begin(NodeType *N) {
  110. return succ_begin(N);
  111. }
  112. static inline ChildIteratorType child_end(NodeType *N) {
  113. return succ_end(N);
  114. }
  115. };
  116. template <> struct GraphTraits<Inverse<Interval*> > {
  117. typedef Interval NodeType;
  118. typedef Interval::pred_iterator ChildIteratorType;
  119. static NodeType *getEntryNode(Inverse<Interval *> G) { return G.Graph; }
  120. static inline ChildIteratorType child_begin(NodeType *N) {
  121. return pred_begin(N);
  122. }
  123. static inline ChildIteratorType child_end(NodeType *N) {
  124. return pred_end(N);
  125. }
  126. };
  127. } // End llvm namespace
  128. #endif