Added LLVM copyright header (for lack of a better term).
[oota-llvm.git] / include / llvm / Analysis / Interval.h
1 //===- llvm/Analysis/Interval.h - Interval Class Declaration ----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
20 #ifndef LLVM_INTERVAL_H
21 #define LLVM_INTERVAL_H
22
23 #include "Support/GraphTraits.h"
24 #include <vector>
25 #include <iosfwd>
26
27 class BasicBlock;
28
29 //===----------------------------------------------------------------------===//
30 //
31 // Interval Class - An Interval is a set of nodes defined such that every node
32 // in the interval has all of its predecessors in the interval (except for the
33 // header)
34 //
35 class Interval {
36   // HeaderNode - The header BasicBlock, which dominates all BasicBlocks in this
37   // interval.  Also, any loops in this interval must go through the HeaderNode.
38   //
39   BasicBlock *HeaderNode;
40 public:
41   typedef std::vector<BasicBlock*>::iterator succ_iterator;
42   typedef std::vector<BasicBlock*>::iterator pred_iterator;
43   typedef std::vector<BasicBlock*>::iterator node_iterator;
44
45   inline Interval(BasicBlock *Header) : HeaderNode(Header) {
46     Nodes.push_back(Header);
47   }
48
49   inline Interval(const Interval &I) // copy ctor
50     : HeaderNode(I.HeaderNode), Nodes(I.Nodes), Successors(I.Successors) {}
51
52   inline BasicBlock *getHeaderNode() const { return HeaderNode; }
53
54   // Nodes - The basic blocks in this interval.
55   //
56   std::vector<BasicBlock*> Nodes;
57
58   // Successors - List of BasicBlocks that are reachable directly from nodes in
59   // this interval, but are not in the interval themselves.
60   // These nodes necessarily must be header nodes for other intervals.
61   //
62   std::vector<BasicBlock*> Successors;
63
64   // Predecessors - List of BasicBlocks that have this Interval's header block
65   // as one of their successors.
66   //
67   std::vector<BasicBlock*> Predecessors;
68
69   // contains - Find out if a basic block is in this interval
70   inline bool contains(BasicBlock *BB) const {
71     for (unsigned i = 0; i < Nodes.size(); ++i)
72       if (Nodes[i] == BB) return true;
73     return false;
74     // I don't want the dependency on <algorithm>
75     //return find(Nodes.begin(), Nodes.end(), BB) != Nodes.end();
76   }
77
78   // isSuccessor - find out if a basic block is a successor of this Interval
79   inline bool isSuccessor(BasicBlock *BB) const {
80     for (unsigned i = 0; i < Successors.size(); ++i)
81       if (Successors[i] == BB) return true;
82     return false;
83     // I don't want the dependency on <algorithm>
84     //return find(Successors.begin(), Successors.end(), BB) != Successors.end();
85   }
86
87   // Equality operator.  It is only valid to compare two intervals from the same
88   // partition, because of this, all we have to check is the header node for 
89   // equality.
90   //
91   inline bool operator==(const Interval &I) const {
92     return HeaderNode == I.HeaderNode;
93   }
94
95   // isLoop - Find out if there is a back edge in this interval...
96   bool isLoop() const;
97
98   // print - Show contents in human readable format...
99   void print(std::ostream &O) const;
100 };
101
102 // succ_begin/succ_end - define methods so that Intervals may be used
103 // just like BasicBlocks can with the succ_* functions, and *::succ_iterator.
104 //
105 inline Interval::succ_iterator succ_begin(Interval *I) {
106   return I->Successors.begin();
107 }
108 inline Interval::succ_iterator succ_end(Interval *I)   {
109   return I->Successors.end();
110 }
111   
112 // pred_begin/pred_end - define methods so that Intervals may be used
113 // just like BasicBlocks can with the pred_* functions, and *::pred_iterator.
114 //
115 inline Interval::pred_iterator pred_begin(Interval *I) {
116   return I->Predecessors.begin();
117 }
118 inline Interval::pred_iterator pred_end(Interval *I)   {
119   return I->Predecessors.end();
120 }
121
122 template <> struct GraphTraits<Interval*> {
123   typedef Interval NodeType;
124   typedef Interval::succ_iterator ChildIteratorType;
125
126   static NodeType *getEntryNode(Interval *I) { return I; }
127
128   // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
129   static inline ChildIteratorType child_begin(NodeType *N) { 
130     return succ_begin(N);
131   }
132   static inline ChildIteratorType child_end(NodeType *N) { 
133     return succ_end(N);
134   }
135 };
136
137 template <> struct GraphTraits<Inverse<Interval*> > {
138   typedef Interval NodeType;
139   typedef Interval::pred_iterator ChildIteratorType;
140   static NodeType *getEntryNode(Inverse<Interval *> G) { return G.Graph; }
141   static inline ChildIteratorType child_begin(NodeType *N) { 
142     return pred_begin(N);
143   }
144   static inline ChildIteratorType child_end(NodeType *N) { 
145     return pred_end(N);
146   }
147 };
148
149 #endif