1 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
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 implements the post-dominator construction algorithms.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/PostDominators.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/Support/CFG.h"
17 #include "Support/DepthFirstIterator.h"
18 #include "Support/SetOperations.h"
20 //===----------------------------------------------------------------------===//
21 // PostDominatorSet Implementation
22 //===----------------------------------------------------------------------===//
24 static RegisterAnalysis<PostDominatorSet>
25 B("postdomset", "Post-Dominator Set Construction", true);
27 // Postdominator set construction. This converts the specified function to only
28 // have a single exit node (return stmt), then calculates the post dominance
29 // sets for the function.
31 bool PostDominatorSet::runOnFunction(Function &F) {
32 Doms.clear(); // Reset from the last time we were run...
34 // Scan the function looking for the root nodes of the post-dominance
35 // relationships. These blocks end with return and unwind instructions.
36 // While we are iterating over the function, we also initialize all of the
39 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
40 Doms[I]; // Initialize to empty
42 if (isa<ReturnInst>(I->getTerminator()) ||
43 isa<UnwindInst>(I->getTerminator()))
47 // If there are no exit nodes for the function, postdomsets are all empty.
48 // This can happen if the function just contains an infinite loop, for
50 if (Roots.empty()) return false;
52 // If we have more than one root, we insert an artificial "null" exit, which
53 // has "virtual edges" to each of the real exit nodes.
61 std::set<BasicBlock*> Visited;
62 DomSetType WorkingSet;
64 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
65 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
66 E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
68 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
69 if (SI != SE) { // Is there SOME successor?
70 // Loop until we get to a successor that has had it's dom set filled
71 // in at least once. We are guaranteed to have this because we are
72 // traversing the graph in DFO and have handled start nodes specially.
74 while (Doms[*SI].size() == 0) ++SI;
75 WorkingSet = Doms[*SI];
77 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
78 DomSetType &SuccSet = Doms[*SI];
80 set_intersect(WorkingSet, SuccSet);
83 // If this node has no successors, it must be one of the root nodes.
84 // We will already take care of the notion that the node
85 // post-dominates itself. The only thing we have to add is that if
86 // there are multiple root nodes, we want to insert a special "null"
87 // exit node which dominates the roots as well.
92 WorkingSet.insert(BB); // A block always dominates itself
93 DomSetType &BBSet = Doms[BB];
94 if (BBSet != WorkingSet) {
95 BBSet.swap(WorkingSet); // Constant time operation!
96 Changed = true; // The sets changed.
98 WorkingSet.clear(); // Clear out the set for next iteration
104 //===----------------------------------------------------------------------===//
105 // ImmediatePostDominators Implementation
106 //===----------------------------------------------------------------------===//
108 static RegisterAnalysis<ImmediatePostDominators>
109 D("postidom", "Immediate Post-Dominators Construction", true);
111 //===----------------------------------------------------------------------===//
112 // PostDominatorTree Implementation
113 //===----------------------------------------------------------------------===//
115 static RegisterAnalysis<PostDominatorTree>
116 F("postdomtree", "Post-Dominator Tree Construction", true);
118 void PostDominatorTree::calculate(const PostDominatorSet &DS) {
119 if (Roots.empty()) return;
120 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
122 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
124 // Iterate over all nodes in depth first order...
125 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
126 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
127 E = idf_end(Roots[i]); I != E; ++I) {
129 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
130 unsigned DomSetSize = Dominators.size();
131 if (DomSetSize == 1) continue; // Root node... IDom = null
133 // If we have already computed the immediate dominator for this node,
134 // don't revisit. This can happen due to nodes reachable from multiple
135 // roots, but which the idf_iterator doesn't know about.
136 if (Nodes.find(BB) != Nodes.end()) continue;
138 // Loop over all dominators of this node. This corresponds to looping
139 // over nodes in the dominator chain, looking for a node whose dominator
140 // set is equal to the current nodes, except that the current node does
141 // not exist in it. This means that it is one level higher in the dom
142 // chain than the current node, and it is our idom! We know that we have
143 // already added a DominatorTree node for our idom, because the idom must
144 // be a predecessor in the depth first order that we are iterating through
147 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
148 DominatorSet::DomSetType::const_iterator End = Dominators.end();
149 for (; I != End; ++I) { // Iterate over dominators...
150 // All of our dominators should form a chain, where the number
151 // of elements in the dominator set indicates what level the
152 // node is at in the chain. We want the node immediately
153 // above us, so it will have an identical dominator set,
154 // except that BB will not dominate it... therefore it's
155 // dominator set size will be one less than BB's...
157 if (DS.getDominators(*I).size() == DomSetSize - 1) {
158 // We know that the immediate dominator should already have a node,
159 // because we are traversing the CFG in depth first order!
161 Node *IDomNode = Nodes[*I];
162 assert(IDomNode && "No node for IDOM?");
164 // Add a new tree node for this BasicBlock, and link it as a child of
166 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
173 //===----------------------------------------------------------------------===//
174 // PostDominanceFrontier Implementation
175 //===----------------------------------------------------------------------===//
177 static RegisterAnalysis<PostDominanceFrontier>
178 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
180 const DominanceFrontier::DomSetType &
181 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
182 const DominatorTree::Node *Node) {
183 // Loop over CFG successors to calculate DFlocal[Node]
184 BasicBlock *BB = Node->getBlock();
185 DomSetType &S = Frontiers[BB]; // The new set to fill in...
186 if (getRoots().empty()) return S;
189 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
191 // Does Node immediately dominate this predecessor?
192 if (DT[*SI]->getIDom() != Node)
195 // At this point, S is DFlocal. Now we union in DFup's of our children...
196 // Loop through and visit the nodes that Node immediately dominates (Node's
197 // children in the IDomTree)
199 for (PostDominatorTree::Node::const_iterator
200 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
201 DominatorTree::Node *IDominee = *NI;
202 const DomSetType &ChildDF = calculate(DT, IDominee);
204 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
205 for (; CDFI != CDFE; ++CDFI) {
206 if (!Node->dominates(DT[*CDFI]))
214 // stub - a dummy function to make linking work ok.
215 void PostDominanceFrontier::stub() {