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