//===- llvm/Analysis/Intervals.h - Interval partition Calculation-*- C++ -*--=//
//
// This file contains the declaration of the cfg::IntervalPartition class, which
-// calculates and represent the interval partition of a method.
+// calculates and represents the interval partition of a method, or a
+// preexisting interval partition.
+//
+// In this way, the interval partition may be used to reduce a flow graph down
+// to its degenerate single node interval partition (unless it is irreducible).
//
//===----------------------------------------------------------------------===//
};
+// succ_begin/succ_end - define global functions so that Intervals may be used
+// just like BasicBlocks can with the succ_* functions, and *::succ_iterator.
+//
+inline Interval::succ_iterator succ_begin(Interval *I) {
+ return I->Successors.begin();
+}
+inline Interval::succ_iterator succ_end(Interval *I) {
+ return I->Successors.end();
+}
+
+// pred_begin/pred_end - define global functions so that Intervals may be used
+// just like BasicBlocks can with the pred_* functions, and *::pred_iterator.
+//
+inline Interval::pred_iterator pred_begin(Interval *I) {
+ return I->Predecessors.begin();
+}
+inline Interval::pred_iterator pred_end(Interval *I) {
+ return I->Predecessors.end();
+}
+
+
+
// IntervalPartition - This class builds and holds an "interval partition" for
// a method. This partition divides the control flow graph into a set of
// maximal intervals, as defined with the properties above. Intuitively, a
// IntervalPartition ctor - Build the partition for the specified method
IntervalPartition(Method *M);
+ // IntervalPartition ctor - Build a reduced interval partition from an
+ // existing interval graph. This takes an additional boolean parameter to
+ // distinguish it from a copy constructor. Always pass in false for now.
+ //
+ IntervalPartition(IntervalPartition &I, bool);
+
// getRootInterval() - Return the root interval that contains the starting
// block of the method
inline Interval *getRootInterval() { return RootInterval; }
inline iterator end() { return IntervalList.end(); }
private:
- void ProcessInterval(BasicBlock *Header);
- void ProcessBasicBlock(Interval *I, BasicBlock *BB);
- void UpdateSuccessors(Interval *Int);
+ // ProcessInterval - This method is used during the construction of the
+ // interval graph. It walks through the source graph, recursively creating
+ // an interval per invokation until the entire graph is covered. This uses
+ // the ProcessNode method to add all of the nodes to the interval.
+ //
+ // This method is templated because it may operate on two different source
+ // graphs: a basic block graph, or a preexisting interval graph.
+ //
+ template<class NodeTy, class OrigContainer>
+ void ProcessInterval(NodeTy *Node, OrigContainer *OC);
+
+ // ProcessNode - This method is called by ProcessInterval to add nodes to the
+ // interval being constructed, and it is also called recursively as it walks
+ // the source graph. A node is added to the current interval only if all of
+ // its predecessors are already in the graph. This also takes care of keeping
+ // the successor set of an interval up to date.
+ //
+ // This method is templated because it may operate on two different source
+ // graphs: a basic block graph, or a preexisting interval graph.
+ //
+ template<class NodeTy, class OrigContainer>
+ void ProcessNode(Interval *Int, NodeTy *Node, OrigContainer *OC);
+
+ // addNodeToInterval - This method exists to assist the generic ProcessNode
+ // with the task of adding a node to the new interval, depending on the
+ // type of the source node. In the case of a CFG source graph (BasicBlock
+ // case), the BasicBlock itself is added to the interval. In the case of
+ // an IntervalPartition source graph (Interval case), all of the member
+ // BasicBlocks are added to the interval.
+ //
+ inline void addNodeToInterval(Interval *Int, Interval *I);
+ inline void addNodeToInterval(Interval *Int, BasicBlock *BB);
+
+ // updatePredecessors - Interval generation only sets the successor fields of
+ // the interval data structures. After interval generation is complete,
+ // run through all of the intervals and propogate successor info as
+ // predecessor info.
+ //
+ void updatePredecessors(Interval *Int);
};
} // End namespace cfg
#include "llvm/BasicBlock.h"
#include "llvm/CFG.h"
-void cfg::IntervalPartition::UpdateSuccessors(cfg::Interval *Int) {
- BasicBlock *Header = Int->HeaderNode;
- for (cfg::Interval::succ_iterator I = Int->Successors.begin(),
- E = Int->Successors.end(); I != E; ++I)
- getBlockInterval(*I)->Predecessors.push_back(Header);
-}
+using namespace cfg;
-// IntervalPartition ctor - Build the partition for the specified method
-cfg::IntervalPartition::IntervalPartition(Method *M) {
- BasicBlock *MethodStart = M->getBasicBlocks().front();
- assert(MethodStart && "Cannot operate on prototypes!");
+// getNodeHeader - Given a source graph node and the source graph, return the
+// BasicBlock that is the header node. This is the opposite of
+// getSourceGraphNode.
+//
+inline static BasicBlock *getNodeHeader(BasicBlock *BB) { return BB; }
+inline static BasicBlock *getNodeHeader(Interval *I) { return I->HeaderNode; }
- ProcessInterval(MethodStart);
- RootInterval = getBlockInterval(MethodStart);
- // Now that we know all of the successor information, propogate this to the
- // predecessors for each block...
- for(iterator I = begin(), E = end(); I != E; ++I)
- UpdateSuccessors(*I);
+// getSourceGraphNode - Given a BasicBlock and the source graph, return the
+// source graph node that corresponds to the BasicBlock. This is the opposite
+// of getNodeHeader.
+//
+inline static BasicBlock *getSourceGraphNode(Method *, BasicBlock *BB) {
+ return BB;
+}
+inline static Interval *getSourceGraphNode(IntervalPartition *IP,
+ BasicBlock *BB) {
+ return IP->getBlockInterval(BB);
}
-void cfg::IntervalPartition::ProcessInterval(BasicBlock *Header) {
- if (getBlockInterval(Header)) return; // Interval already constructed
- Interval *Int = new Interval(Header);
- IntervalList.push_back(Int); // Add the interval to our current set
- IntervalMap.insert(make_pair(Header, Int));
+// addNodeToInterval - This method exists to assist the generic ProcessNode
+// with the task of adding a node to the new interval, depending on the
+// type of the source node. In the case of a CFG source graph (BasicBlock
+// case), the BasicBlock itself is added to the interval.
+//
+inline void IntervalPartition::addNodeToInterval(Interval *Int, BasicBlock *BB){
+ Int->Nodes.push_back(BB);
+ IntervalMap.insert(make_pair(BB, Int));
+}
- // Check all of our successors to see if they are in the interval...
- for (succ_iterator I = succ_begin(Header), E = succ_end(Header); I != E; ++I)
- ProcessBasicBlock(Int, *I);
+// addNodeToInterval - This method exists to assist the generic ProcessNode
+// with the task of adding a node to the new interval, depending on the
+// type of the source node. In the case of a CFG source graph (BasicBlock
+// case), the BasicBlock itself is added to the interval. In the case of
+// an IntervalPartition source graph (Interval case), all of the member
+// BasicBlocks are added to the interval.
+//
+inline void IntervalPartition::addNodeToInterval(Interval *Int, Interval *I) {
+ // Add all of the nodes in I as new nodes in Int.
+ copy(I->Nodes.begin(), I->Nodes.end(), back_inserter(Int->Nodes));
- // Build all of the successor intervals of this interval now...
- for(Interval::succ_iterator I = Int->Successors.begin(),
- E = Int->Successors.end(); I != E; ++I)
- ProcessInterval(*I);
+ // Add mappings for all of the basic blocks in I to the IntervalPartition
+ for (Interval::node_iterator It = I->Nodes.begin(), End = I->Nodes.end();
+ It != End; ++It)
+ IntervalMap.insert(make_pair(*It, Int));
}
-void cfg::IntervalPartition::ProcessBasicBlock(Interval *Int, BasicBlock *BB) {
- assert(Int && "Null interval == bad!");
- assert(BB && "Null interval == bad!");
- Interval *CurInt = getBlockInterval(BB);
+// ProcessNode - This method is called by ProcessInterval to add nodes to the
+// interval being constructed, and it is also called recursively as it walks
+// the source graph. A node is added to the current interval only if all of
+// its predecessors are already in the graph. This also takes care of keeping
+// the successor set of an interval up to date.
+//
+// This method is templated because it may operate on two different source
+// graphs: a basic block graph, or a preexisting interval graph.
+//
+template<class NodeTy, class OrigContainer>
+void IntervalPartition::ProcessNode(Interval *Int,
+ NodeTy *Node, OrigContainer *OC) {
+ assert(Int && "Null interval == bad!");
+ assert(Node && "Null Node == bad!");
+
+ BasicBlock *NodeHeader = getNodeHeader(Node);
+ Interval *CurInt = getBlockInterval(NodeHeader);
if (CurInt == Int) { // Already in this interval...
return;
} else if (CurInt != 0) { // In another interval, add as successor
- if (!Int->isSuccessor(BB)) // Add only if not already in set
- Int->Successors.push_back(BB);
+ if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
+ Int->Successors.push_back(NodeHeader);
} else { // Otherwise, not in interval yet
- for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
+ for (typename NodeTy::pred_iterator I = pred_begin(Node),
+ E = pred_end(Node); I != E; ++I) {
if (!Int->contains(*I)) { // If pred not in interval, we can't be
- if (!Int->isSuccessor(BB)) // Add only if not already in set
- Int->Successors.push_back(BB);
+ if (!Int->isSuccessor(NodeHeader)) // Add only if not already in set
+ Int->Successors.push_back(NodeHeader);
return; // See you later
}
}
// If we get here, then all of the predecessors of BB are in the interval
// already. In this case, we must add BB to the interval!
- Int->Nodes.push_back(BB);
- IntervalMap.insert(make_pair(BB, Int));
+ addNodeToInterval(Int, Node);
- if (Int->isSuccessor(BB)) {
+ if (Int->isSuccessor(NodeHeader)) {
// If we were in the successor list from before... remove from succ list
- remove(Int->Successors.begin(), Int->Successors.end(), BB);
+ Int->Successors.erase(remove(Int->Successors.begin(),
+ Int->Successors.end(), NodeHeader),
+ Int->Successors.end());
}
- // Now that we have discovered that BB is in the interval, perhaps some of
+ // Now that we have discovered that Node is in the interval, perhaps some of
// its successors are as well?
- for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
- ProcessBasicBlock(Int, *I);
+ for (typename NodeTy::succ_iterator It = succ_begin(Node),
+ End = succ_end(Node); It != End; ++It)
+ ProcessNode(Int, getSourceGraphNode(OC, *It), OC);
}
}
+
+
+// ProcessInterval - This method is used during the construction of the
+// interval graph. It walks through the source graph, recursively creating
+// an interval per invokation until the entire graph is covered. This uses
+// the ProcessNode method to add all of the nodes to the interval.
+//
+// This method is templated because it may operate on two different source
+// graphs: a basic block graph, or a preexisting interval graph.
+//
+template<class NodeTy, class OrigContainer>
+void IntervalPartition::ProcessInterval(NodeTy *Node, OrigContainer *OC) {
+ BasicBlock *Header = getNodeHeader(Node);
+ if (getBlockInterval(Header)) return; // Interval already constructed?
+
+ // Create a new interval and add the interval to our current set
+ Interval *Int = new Interval(Header);
+ IntervalList.push_back(Int);
+ IntervalMap.insert(make_pair(Header, Int));
+
+ // Check all of our successors to see if they are in the interval...
+ for (typename NodeTy::succ_iterator I = succ_begin(Node), E = succ_end(Node);
+ I != E; ++I)
+ ProcessNode(Int, getSourceGraphNode(OC, *I), OC);
+
+ // Build all of the successor intervals of this interval now...
+ for(Interval::succ_iterator I = Int->Successors.begin(),
+ E = Int->Successors.end(); I != E; ++I) {
+ ProcessInterval(getSourceGraphNode(OC, *I), OC);
+ }
+}
+
+
+
+// updatePredecessors - Interval generation only sets the successor fields of
+// the interval data structures. After interval generation is complete,
+// run through all of the intervals and propogate successor info as
+// predecessor info.
+//
+void IntervalPartition::updatePredecessors(cfg::Interval *Int) {
+ BasicBlock *Header = Int->HeaderNode;
+ for (Interval::succ_iterator I = Int->Successors.begin(),
+ E = Int->Successors.end(); I != E; ++I)
+ getBlockInterval(*I)->Predecessors.push_back(Header);
+}
+
+
+
+// IntervalPartition ctor - Build the first level interval partition for the
+// specified method...
+//
+IntervalPartition::IntervalPartition(Method *M) {
+ BasicBlock *MethodStart = M->getBasicBlocks().front();
+ assert(MethodStart && "Cannot operate on prototypes!");
+
+ ProcessInterval(MethodStart, M);
+ RootInterval = getBlockInterval(MethodStart);
+
+ // Now that we know all of the successor information, propogate this to the
+ // predecessors for each block...
+ for(iterator I = begin(), E = end(); I != E; ++I)
+ updatePredecessors(*I);
+}
+
+
+// IntervalPartition ctor - Build a reduced interval partition from an
+// existing interval graph. This takes an additional boolean parameter to
+// distinguish it from a copy constructor. Always pass in false for now.
+//
+IntervalPartition::IntervalPartition(IntervalPartition &I, bool) {
+ Interval *MethodStart = I.getRootInterval();
+ assert(MethodStart && "Cannot operate on empty IntervalPartitions!");
+
+ ProcessInterval(MethodStart, &I);
+ RootInterval = getBlockInterval(*MethodStart->Nodes.begin());
+
+ // Now that we know all of the successor information, propogate this to the
+ // predecessors for each block...
+ for(iterator I = begin(), E = end(); I != E; ++I)
+ updatePredecessors(*I);
+}