1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
3 // This file implements the post-dominator construction algorithms.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/Dominators.h"
8 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
9 #include "llvm/Support/CFG.h"
10 #include "Support/DepthFirstIterator.h"
11 #include "Support/SetOperations.h"
14 //===----------------------------------------------------------------------===//
15 // PostDominatorSet Implementation
16 //===----------------------------------------------------------------------===//
18 static RegisterAnalysis<PostDominatorSet>
19 B("postdomset", "Post-Dominator Set Construction", true);
20 AnalysisID PostDominatorSet::ID = B;
22 // Postdominator set construction. This converts the specified function to only
23 // have a single exit node (return stmt), then calculates the post dominance
24 // sets for the function.
26 bool PostDominatorSet::runOnFunction(Function &F) {
27 Doms.clear(); // Reset from the last time we were run...
28 // Since we require that the unify all exit nodes pass has been run, we know
29 // that there can be at most one return instruction in the function left.
32 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
34 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
35 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
36 Doms[FI] = DomSetType();
44 set<const BasicBlock*> Visited;
45 DomSetType WorkingSet;
46 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
47 for ( ; It != End; ++It) {
49 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
50 if (PI != PEnd) { // Is there SOME predecessor?
51 // Loop until we get to a successor that has had it's dom set filled
52 // in at least once. We are guaranteed to have this because we are
53 // traversing the graph in DFO and have handled start nodes specially.
55 while (Doms[*PI].size() == 0) ++PI;
56 WorkingSet = Doms[*PI];
58 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
59 DomSetType &PredSet = Doms[*PI];
61 set_intersect(WorkingSet, PredSet);
65 WorkingSet.insert(BB); // A block always dominates itself
66 DomSetType &BBSet = Doms[BB];
67 if (BBSet != WorkingSet) {
68 BBSet.swap(WorkingSet); // Constant time operation!
69 Changed = true; // The sets changed.
71 WorkingSet.clear(); // Clear out the set for next iteration
77 // getAnalysisUsage - This obviously provides a post-dominator set, but it also
78 // requires the UnifyFunctionExitNodes pass.
80 void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.addRequired(UnifyFunctionExitNodes::ID);
85 //===----------------------------------------------------------------------===//
86 // ImmediatePostDominators Implementation
87 //===----------------------------------------------------------------------===//
89 static RegisterAnalysis<ImmediatePostDominators>
90 D("postidom", "Immediate Post-Dominators Construction", true);
91 AnalysisID ImmediatePostDominators::ID = D;
93 //===----------------------------------------------------------------------===//
94 // PostDominatorTree Implementation
95 //===----------------------------------------------------------------------===//
97 static RegisterAnalysis<PostDominatorTree>
98 F("postdomtree", "Post-Dominator Tree Construction", true);
99 AnalysisID PostDominatorTree::ID = F;
101 void PostDominatorTree::calculate(const PostDominatorSet &DS) {
102 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
105 // Iterate over all nodes in depth first order...
106 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
109 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
110 unsigned DomSetSize = Dominators.size();
111 if (DomSetSize == 1) continue; // Root node... IDom = null
113 // Loop over all dominators of this node. This corresponds to looping
114 // over nodes in the dominator chain, looking for a node whose dominator
115 // set is equal to the current nodes, except that the current node does
116 // not exist in it. This means that it is one level higher in the dom
117 // chain than the current node, and it is our idom! We know that we have
118 // already added a DominatorTree node for our idom, because the idom must
119 // be a predecessor in the depth first order that we are iterating through
122 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
123 DominatorSet::DomSetType::const_iterator End = Dominators.end();
124 for (; I != End; ++I) { // Iterate over dominators...
125 // All of our dominators should form a chain, where the number
126 // of elements in the dominator set indicates what level the
127 // node is at in the chain. We want the node immediately
128 // above us, so it will have an identical dominator set,
129 // except that BB will not dominate it... therefore it's
130 // dominator set size will be one less than BB's...
132 if (DS.getDominators(*I).size() == DomSetSize - 1) {
133 // We know that the immediate dominator should already have a node,
134 // because we are traversing the CFG in depth first order!
136 Node *IDomNode = Nodes[*I];
137 assert(IDomNode && "No node for IDOM?");
139 // Add a new tree node for this BasicBlock, and link it as a child of
141 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
149 //===----------------------------------------------------------------------===//
150 // PostDominanceFrontier Implementation
151 //===----------------------------------------------------------------------===//
153 static RegisterAnalysis<PostDominanceFrontier>
154 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
155 AnalysisID PostDominanceFrontier::ID = H;
157 const DominanceFrontier::DomSetType &
158 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
159 const DominatorTree::Node *Node) {
160 // Loop over CFG successors to calculate DFlocal[Node]
161 BasicBlock *BB = Node->getNode();
162 DomSetType &S = Frontiers[BB]; // The new set to fill in...
165 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
167 // Does Node immediately dominate this predeccessor?
168 if (DT[*SI]->getIDom() != Node)
172 // At this point, S is DFlocal. Now we union in DFup's of our children...
173 // Loop through and visit the nodes that Node immediately dominates (Node's
174 // children in the IDomTree)
176 for (PostDominatorTree::Node::const_iterator
177 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
178 DominatorTree::Node *IDominee = *NI;
179 const DomSetType &ChildDF = calculate(DT, IDominee);
181 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
182 for (; CDFI != CDFE; ++CDFI) {
183 if (!Node->dominates(DT[*CDFI]))