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"
22 //===----------------------------------------------------------------------===//
23 // PostDominatorSet Implementation
24 //===----------------------------------------------------------------------===//
26 static RegisterAnalysis<PostDominatorSet>
27 B("postdomset", "Post-Dominator Set Construction", true);
29 // Postdominator set construction. This converts the specified function to only
30 // have a single exit node (return stmt), then calculates the post dominance
31 // sets for the function.
33 bool PostDominatorSet::runOnFunction(Function &F) {
34 Doms.clear(); // Reset from the last time we were run...
36 // Scan the function looking for the root nodes of the post-dominance
37 // relationships. These blocks end with return and unwind instructions.
38 // While we are iterating over the function, we also initialize all of the
41 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
42 Doms[I]; // Initialize to empty
44 if (isa<ReturnInst>(I->getTerminator()) ||
45 isa<UnwindInst>(I->getTerminator()))
49 // If there are no exit nodes for the function, postdomsets are all empty.
50 // This can happen if the function just contains an infinite loop, for
52 if (Roots.empty()) return false;
54 // If we have more than one root, we insert an artificial "null" exit, which
55 // has "virtual edges" to each of the real exit nodes.
63 std::set<BasicBlock*> Visited;
64 DomSetType WorkingSet;
66 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
67 for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
68 E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
70 succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
71 if (SI != SE) { // Is there SOME successor?
72 // Loop until we get to a successor that has had it's dom set filled
73 // in at least once. We are guaranteed to have this because we are
74 // traversing the graph in DFO and have handled start nodes specially.
76 while (Doms[*SI].size() == 0) ++SI;
77 WorkingSet = Doms[*SI];
79 for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
80 DomSetType &SuccSet = Doms[*SI];
82 set_intersect(WorkingSet, SuccSet);
85 // If this node has no successors, it must be one of the root nodes.
86 // We will already take care of the notion that the node
87 // post-dominates itself. The only thing we have to add is that if
88 // there are multiple root nodes, we want to insert a special "null"
89 // exit node which dominates the roots as well.
94 WorkingSet.insert(BB); // A block always dominates itself
95 DomSetType &BBSet = Doms[BB];
96 if (BBSet != WorkingSet) {
97 BBSet.swap(WorkingSet); // Constant time operation!
98 Changed = true; // The sets changed.
100 WorkingSet.clear(); // Clear out the set for next iteration
106 //===----------------------------------------------------------------------===//
107 // ImmediatePostDominators Implementation
108 //===----------------------------------------------------------------------===//
110 static RegisterAnalysis<ImmediatePostDominators>
111 D("postidom", "Immediate Post-Dominators Construction", true);
113 //===----------------------------------------------------------------------===//
114 // PostDominatorTree Implementation
115 //===----------------------------------------------------------------------===//
117 static RegisterAnalysis<PostDominatorTree>
118 F("postdomtree", "Post-Dominator Tree Construction", true);
120 void PostDominatorTree::calculate(const PostDominatorSet &DS) {
121 if (Roots.empty()) return;
122 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
124 Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
126 // Iterate over all nodes in depth first order...
127 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
128 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
129 E = idf_end(Roots[i]); I != E; ++I) {
131 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
132 unsigned DomSetSize = Dominators.size();
133 if (DomSetSize == 1) continue; // Root node... IDom = null
135 // If we have already computed the immediate dominator for this node,
136 // don't revisit. This can happen due to nodes reachable from multiple
137 // roots, but which the idf_iterator doesn't know about.
138 if (Nodes.find(BB) != Nodes.end()) continue;
140 // Loop over all dominators of this node. This corresponds to looping
141 // over nodes in the dominator chain, looking for a node whose dominator
142 // set is equal to the current nodes, except that the current node does
143 // not exist in it. This means that it is one level higher in the dom
144 // chain than the current node, and it is our idom! We know that we have
145 // already added a DominatorTree node for our idom, because the idom must
146 // be a predecessor in the depth first order that we are iterating through
149 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
150 DominatorSet::DomSetType::const_iterator End = Dominators.end();
151 for (; I != End; ++I) { // Iterate over dominators...
152 // All of our dominators should form a chain, where the number
153 // of elements in the dominator set indicates what level the
154 // node is at in the chain. We want the node immediately
155 // above us, so it will have an identical dominator set,
156 // except that BB will not dominate it... therefore it's
157 // dominator set size will be one less than BB's...
159 if (DS.getDominators(*I).size() == DomSetSize - 1) {
160 // We know that the immediate dominator should already have a node,
161 // because we are traversing the CFG in depth first order!
163 Node *IDomNode = Nodes[*I];
164 assert(IDomNode && "No node for IDOM?");
166 // Add a new tree node for this BasicBlock, and link it as a child of
168 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
175 //===----------------------------------------------------------------------===//
176 // PostDominanceFrontier Implementation
177 //===----------------------------------------------------------------------===//
179 static RegisterAnalysis<PostDominanceFrontier>
180 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
182 const DominanceFrontier::DomSetType &
183 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
184 const DominatorTree::Node *Node) {
185 // Loop over CFG successors to calculate DFlocal[Node]
186 BasicBlock *BB = Node->getBlock();
187 DomSetType &S = Frontiers[BB]; // The new set to fill in...
188 if (getRoots().empty()) return S;
191 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
193 // Does Node immediately dominate this predecessor?
194 if (DT[*SI]->getIDom() != Node)
197 // At this point, S is DFlocal. Now we union in DFup's of our children...
198 // Loop through and visit the nodes that Node immediately dominates (Node's
199 // children in the IDomTree)
201 for (PostDominatorTree::Node::const_iterator
202 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
203 DominatorTree::Node *IDominee = *NI;
204 const DomSetType &ChildDF = calculate(DT, IDominee);
206 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
207 for (; CDFI != CDFE; ++CDFI) {
208 if (!Node->dominates(DT[*CDFI]))
216 // stub - a dummy function to make linking work ok.
217 void PostDominanceFrontier::stub() {
220 } // End llvm namespace