1 //===- llvm/Analysis/Interval.h - Interval Class Declaration ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 // Intervals have some interesting and useful properties, including the
15 // 1. The header node of an interval dominates all of the elements of the
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_INTERVAL_H
21 #define LLVM_INTERVAL_H
23 #include "Support/GraphTraits.h"
29 //===----------------------------------------------------------------------===//
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
36 // HeaderNode - The header BasicBlock, which dominates all BasicBlocks in this
37 // interval. Also, any loops in this interval must go through the HeaderNode.
39 BasicBlock *HeaderNode;
41 typedef std::vector<BasicBlock*>::iterator succ_iterator;
42 typedef std::vector<BasicBlock*>::iterator pred_iterator;
43 typedef std::vector<BasicBlock*>::iterator node_iterator;
45 inline Interval(BasicBlock *Header) : HeaderNode(Header) {
46 Nodes.push_back(Header);
49 inline Interval(const Interval &I) // copy ctor
50 : HeaderNode(I.HeaderNode), Nodes(I.Nodes), Successors(I.Successors) {}
52 inline BasicBlock *getHeaderNode() const { return HeaderNode; }
54 // Nodes - The basic blocks in this interval.
56 std::vector<BasicBlock*> Nodes;
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.
62 std::vector<BasicBlock*> Successors;
64 // Predecessors - List of BasicBlocks that have this Interval's header block
65 // as one of their successors.
67 std::vector<BasicBlock*> Predecessors;
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;
74 // I don't want the dependency on <algorithm>
75 //return find(Nodes.begin(), Nodes.end(), BB) != Nodes.end();
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;
83 // I don't want the dependency on <algorithm>
84 //return find(Successors.begin(), Successors.end(), BB) != Successors.end();
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
91 inline bool operator==(const Interval &I) const {
92 return HeaderNode == I.HeaderNode;
95 // isLoop - Find out if there is a back edge in this interval...
98 // print - Show contents in human readable format...
99 void print(std::ostream &O) const;
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.
105 inline Interval::succ_iterator succ_begin(Interval *I) {
106 return I->Successors.begin();
108 inline Interval::succ_iterator succ_end(Interval *I) {
109 return I->Successors.end();
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.
115 inline Interval::pred_iterator pred_begin(Interval *I) {
116 return I->Predecessors.begin();
118 inline Interval::pred_iterator pred_end(Interval *I) {
119 return I->Predecessors.end();
122 template <> struct GraphTraits<Interval*> {
123 typedef Interval NodeType;
124 typedef Interval::succ_iterator ChildIteratorType;
126 static NodeType *getEntryNode(Interval *I) { return I; }
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);
132 static inline ChildIteratorType child_end(NodeType *N) {
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);
144 static inline ChildIteratorType child_end(NodeType *N) {