1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
3 // This file implements the post-dominator construction algorithms.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/PostDominators.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);
21 // Postdominator set construction. This converts the specified function to only
22 // have a single exit node (return stmt), then calculates the post dominance
23 // sets for the function.
25 bool PostDominatorSet::runOnFunction(Function &F) {
26 Doms.clear(); // Reset from the last time we were run...
27 // Since we require that the unify all exit nodes pass has been run, we know
28 // that there can be at most one return instruction in the function left.
31 Root = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
33 if (Root == 0) { // No exit node for the function? Postdomsets are all empty
34 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
35 Doms[FI] = DomSetType();
43 set<const BasicBlock*> Visited;
44 DomSetType WorkingSet;
45 idf_iterator<BasicBlock*> It = idf_begin(Root), End = idf_end(Root);
46 for ( ; It != End; ++It) {
48 succ_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
49 if (PI != PEnd) { // Is there SOME predecessor?
50 // Loop until we get to a successor that has had it's dom set filled
51 // in at least once. We are guaranteed to have this because we are
52 // traversing the graph in DFO and have handled start nodes specially.
54 while (Doms[*PI].size() == 0) ++PI;
55 WorkingSet = Doms[*PI];
57 for (++PI; PI != PEnd; ++PI) { // Intersect all of the successor sets
58 DomSetType &PredSet = Doms[*PI];
60 set_intersect(WorkingSet, PredSet);
64 WorkingSet.insert(BB); // A block always dominates itself
65 DomSetType &BBSet = Doms[BB];
66 if (BBSet != WorkingSet) {
67 BBSet.swap(WorkingSet); // Constant time operation!
68 Changed = true; // The sets changed.
70 WorkingSet.clear(); // Clear out the set for next iteration
76 // getAnalysisUsage - This obviously provides a post-dominator set, but it also
77 // requires the UnifyFunctionExitNodes pass.
79 void PostDominatorSet::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.addRequired<UnifyFunctionExitNodes>();
84 //===----------------------------------------------------------------------===//
85 // ImmediatePostDominators Implementation
86 //===----------------------------------------------------------------------===//
88 static RegisterAnalysis<ImmediatePostDominators>
89 D("postidom", "Immediate Post-Dominators Construction", true);
91 //===----------------------------------------------------------------------===//
92 // PostDominatorTree Implementation
93 //===----------------------------------------------------------------------===//
95 static RegisterAnalysis<PostDominatorTree>
96 F("postdomtree", "Post-Dominator Tree Construction", true);
98 void PostDominatorTree::calculate(const PostDominatorSet &DS) {
99 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
102 // Iterate over all nodes in depth first order...
103 for (idf_iterator<BasicBlock*> I = idf_begin(Root), E = idf_end(Root);
106 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
107 unsigned DomSetSize = Dominators.size();
108 if (DomSetSize == 1) continue; // Root node... IDom = null
110 // Loop over all dominators of this node. This corresponds to looping
111 // over nodes in the dominator chain, looking for a node whose dominator
112 // set is equal to the current nodes, except that the current node does
113 // not exist in it. This means that it is one level higher in the dom
114 // chain than the current node, and it is our idom! We know that we have
115 // already added a DominatorTree node for our idom, because the idom must
116 // be a predecessor in the depth first order that we are iterating through
119 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
120 DominatorSet::DomSetType::const_iterator End = Dominators.end();
121 for (; I != End; ++I) { // Iterate over dominators...
122 // All of our dominators should form a chain, where the number
123 // of elements in the dominator set indicates what level the
124 // node is at in the chain. We want the node immediately
125 // above us, so it will have an identical dominator set,
126 // except that BB will not dominate it... therefore it's
127 // dominator set size will be one less than BB's...
129 if (DS.getDominators(*I).size() == DomSetSize - 1) {
130 // We know that the immediate dominator should already have a node,
131 // because we are traversing the CFG in depth first order!
133 Node *IDomNode = Nodes[*I];
134 assert(IDomNode && "No node for IDOM?");
136 // Add a new tree node for this BasicBlock, and link it as a child of
138 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
146 //===----------------------------------------------------------------------===//
147 // PostDominanceFrontier Implementation
148 //===----------------------------------------------------------------------===//
150 static RegisterAnalysis<PostDominanceFrontier>
151 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
153 const DominanceFrontier::DomSetType &
154 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
155 const DominatorTree::Node *Node) {
156 // Loop over CFG successors to calculate DFlocal[Node]
157 BasicBlock *BB = Node->getNode();
158 DomSetType &S = Frontiers[BB]; // The new set to fill in...
161 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
163 // Does Node immediately dominate this predeccessor?
164 if (DT[*SI]->getIDom() != Node)
168 // At this point, S is DFlocal. Now we union in DFup's of our children...
169 // Loop through and visit the nodes that Node immediately dominates (Node's
170 // children in the IDomTree)
172 for (PostDominatorTree::Node::const_iterator
173 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
174 DominatorTree::Node *IDominee = *NI;
175 const DomSetType &ChildDF = calculate(DT, IDominee);
177 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
178 for (; CDFI != CDFE; ++CDFI) {
179 if (!Node->dominates(DT[*CDFI]))
187 // stub - a dummy function to make linking work ok.
188 void PostDominanceFrontier::stub() {