1 //===- IntervalIterator.h - Interval Iterator Declaration --------*- C++ -*--=//
3 // This file defines an iterator that enumerates the intervals in a control flow
4 // graph of some sort. This iterator is parametric, allowing iterator over the
5 // following types of graphs:
7 // 1. A Function* object, composed of BasicBlock nodes.
8 // 2. An IntervalPartition& object, composed of Interval nodes.
10 // This iterator is defined to walk the control flow graph, returning intervals
11 // in depth first order. These intervals are completely filled in except for
12 // the predecessor fields (the successor information is filled in however).
14 // By default, the intervals created by this iterator are deleted after they
15 // are no longer any use to the iterator. This behavior can be changed by
16 // passing a false value into the intervals_begin() function. This causes the
17 // IOwnMem member to be set, and the intervals to not be deleted.
19 // It is only safe to use this if all of the intervals are deleted by the caller
20 // and all of the intervals are processed. However, the user of the iterator is
21 // not allowed to modify or delete the intervals until after the iterator has
22 // been used completely. The IntervalPartition class uses this functionality.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_INTERVAL_ITERATOR_H
27 #define LLVM_INTERVAL_ITERATOR_H
29 #include "llvm/Analysis/IntervalPartition.h"
30 #include "llvm/Function.h"
31 #include "llvm/BasicBlock.h"
32 #include "llvm/Support/CFG.h"
39 // getNodeHeader - Given a source graph node and the source graph, return the
40 // BasicBlock that is the header node. This is the opposite of
41 // getSourceGraphNode.
43 inline BasicBlock *getNodeHeader(BasicBlock *BB) { return BB; }
44 inline BasicBlock *getNodeHeader(Interval *I) { return I->getHeaderNode(); }
46 // getSourceGraphNode - Given a BasicBlock and the source graph, return the
47 // source graph node that corresponds to the BasicBlock. This is the opposite
50 inline BasicBlock *getSourceGraphNode(Function *, BasicBlock *BB) {
53 inline Interval *getSourceGraphNode(IntervalPartition *IP, BasicBlock *BB) {
54 return IP->getBlockInterval(BB);
57 // addNodeToInterval - This method exists to assist the generic ProcessNode
58 // with the task of adding a node to the new interval, depending on the
59 // type of the source node. In the case of a CFG source graph (BasicBlock
60 // case), the BasicBlock itself is added to the interval.
62 inline void addNodeToInterval(Interval *Int, BasicBlock *BB) {
63 Int->Nodes.push_back(BB);
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. In the case of
70 // an IntervalPartition source graph (Interval case), all of the member
71 // BasicBlocks are added to the interval.
73 inline void addNodeToInterval(Interval *Int, Interval *I) {
74 // Add all of the nodes in I as new nodes in Int.
75 copy(I->Nodes.begin(), I->Nodes.end(), back_inserter(Int->Nodes));
82 template<class NodeTy, class OrigContainer_t>
83 class IntervalIterator {
84 std::stack<std::pair<Interval*, typename Interval::succ_iterator> > IntStack;
85 std::set<BasicBlock*> Visited;
86 OrigContainer_t *OrigContainer;
87 bool IOwnMem; // If True, delete intervals when done with them
88 // See file header for conditions of use
90 typedef BasicBlock* _BB;
92 typedef IntervalIterator<NodeTy, OrigContainer_t> _Self;
93 typedef std::forward_iterator_tag iterator_category;
95 IntervalIterator() {} // End iterator, empty stack
96 IntervalIterator(Function *M, bool OwnMemory) : IOwnMem(OwnMemory) {
98 if (!ProcessInterval(M->front())) {
99 assert(0 && "ProcessInterval should never fail for first interval!");
103 IntervalIterator(IntervalPartition &IP, bool OwnMemory) : IOwnMem(OwnMemory) {
105 if (!ProcessInterval(IP.getRootInterval())) {
106 assert(0 && "ProcessInterval should never fail for first interval!");
110 inline ~IntervalIterator() {
112 while (!IntStack.empty()) {
118 inline bool operator==(const _Self& x) const { return IntStack == x.IntStack;}
119 inline bool operator!=(const _Self& x) const { return !operator==(x); }
121 inline const Interval *operator*() const { return IntStack.top().first; }
122 inline Interval *operator*() { return IntStack.top().first; }
123 inline const Interval *operator->() const { return operator*(); }
124 inline Interval *operator->() { return operator*(); }
126 _Self& operator++() { // Preincrement
127 assert(!IntStack.empty() && "Attempting to use interval iterator at end!");
129 // All of the intervals on the stack have been visited. Try visiting
130 // their successors now.
131 Interval::succ_iterator &SuccIt = IntStack.top().second,
132 EndIt = succ_end(IntStack.top().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!
139 // Free interval memory... if neccesary
140 if (IOwnMem) delete IntStack.top().first;
142 // We ran out of successors for this interval... pop off the stack
144 } while (!IntStack.empty());
148 inline _Self operator++(int) { // Postincrement
149 _Self tmp = *this; ++*this; return tmp;
153 // ProcessInterval - This method is used during the construction of the
154 // interval graph. It walks through the source graph, recursively creating
155 // an interval per invokation until the entire graph is covered. This uses
156 // the ProcessNode method to add all of the nodes to the interval.
158 // This method is templated because it may operate on two different source
159 // graphs: a basic block graph, or a preexisting interval graph.
161 bool ProcessInterval(NodeTy *Node) {
162 BasicBlock *Header = getNodeHeader(Node);
163 if (Visited.count(Header)) return false;
165 Interval *Int = new Interval(Header);
166 Visited.insert(Header); // The header has now been visited!
168 // Check all of our successors to see if they are in the interval...
169 for (typename NodeTy::succ_iterator I = succ_begin(Node),
170 E = succ_end(Node); I != E; ++I)
171 ProcessNode(Int, getSourceGraphNode(OrigContainer, *I));
173 IntStack.push(make_pair(Int, succ_begin(Int)));
177 // ProcessNode - This method is called by ProcessInterval to add nodes to the
178 // interval being constructed, and it is also called recursively as it walks
179 // the source graph. A node is added to the current interval only if all of
180 // its predecessors are already in the graph. This also takes care of keeping
181 // the successor set of an interval up to date.
183 // This method is templated because it may operate on two different source
184 // graphs: a basic block graph, or a preexisting interval graph.
186 void ProcessNode(Interval *Int, NodeTy *Node) {
187 assert(Int && "Null interval == bad!");
188 assert(Node && "Null Node == bad!");
190 BasicBlock *NodeHeader = getNodeHeader(Node);
192 if (Visited.count(NodeHeader)) { // Node already been visited?
193 if (Int->contains(NodeHeader)) { // Already in this interval...
195 } else { // In other interval, add as successor
196 if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
197 Int->Successors.push_back(NodeHeader);
199 } else { // Otherwise, not in interval yet
200 for (typename NodeTy::pred_iterator I = pred_begin(Node),
201 E = pred_end(Node); I != E; ++I) {
202 if (!Int->contains(*I)) { // If pred not in interval, we can't be
203 if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
204 Int->Successors.push_back(NodeHeader);
205 return; // See you later
209 // If we get here, then all of the predecessors of BB are in the interval
210 // already. In this case, we must add BB to the interval!
211 addNodeToInterval(Int, Node);
212 Visited.insert(NodeHeader); // The node has now been visited!
214 if (Int->isSuccessor(NodeHeader)) {
215 // If we were in the successor list from before... remove from succ list
216 Int->Successors.erase(remove(Int->Successors.begin(),
217 Int->Successors.end(), NodeHeader),
218 Int->Successors.end());
221 // Now that we have discovered that Node is in the interval, perhaps some
222 // of its successors are as well?
223 for (typename NodeTy::succ_iterator It = succ_begin(Node),
224 End = succ_end(Node); It != End; ++It)
225 ProcessNode(Int, getSourceGraphNode(OrigContainer, *It));
230 typedef IntervalIterator<BasicBlock, Function> function_interval_iterator;
231 typedef IntervalIterator<Interval, IntervalPartition> interval_part_interval_iterator;
234 inline function_interval_iterator intervals_begin(Function *F,
235 bool DeleteInts = true) {
236 return function_interval_iterator(F, DeleteInts);
238 inline function_interval_iterator intervals_end(Function *) {
239 return function_interval_iterator();
242 inline interval_part_interval_iterator
243 intervals_begin(IntervalPartition &IP, bool DeleteIntervals = true) {
244 return interval_part_interval_iterator(IP, DeleteIntervals);
247 inline interval_part_interval_iterator intervals_end(IntervalPartition &IP) {
248 return interval_part_interval_iterator();
251 } // End namespace cfg