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/Instructions.h"
16 #include "llvm/Support/CFG.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/SetOperations.h"
21 //===----------------------------------------------------------------------===//
22 // ImmediatePostDominators Implementation
23 //===----------------------------------------------------------------------===//
25 static RegisterPass<ImmediatePostDominators>
26 D("postidom", "Immediate Post-Dominators Construction", true);
28 unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo,
30 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
31 std::set<BasicBlock *> visited;
32 workStack.push_back(std::make_pair(V, &VInfo));
35 BasicBlock *currentBB = workStack.back().first;
36 InfoRec *currentVInfo = workStack.back().second;
38 // Visit each block only once.
39 if (visited.count(currentBB) == 0) {
41 visited.insert(currentBB);
42 currentVInfo->Semi = ++N;
43 currentVInfo->Label = currentBB;
45 Vertex.push_back(currentBB); // Vertex[n] = current;
46 // Info[currentBB].Ancestor = 0;
48 // Child[currentBB] = 0;
49 currentVInfo->Size = 1; // Size[currentBB] = 1
53 bool visitChild = false;
54 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
55 PI != PE && !visitChild; ++PI) {
56 InfoRec &SuccVInfo = Info[*PI];
57 if (SuccVInfo.Semi == 0) {
58 SuccVInfo.Parent = currentBB;
59 if (visited.count (*PI) == 0) {
60 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
66 // If all children are visited or if this block has no child then pop this
67 // block out of workStack.
71 } while (!workStack.empty());
76 void ImmediatePostDominators::Compress(BasicBlock *V, InfoRec &VInfo) {
77 BasicBlock *VAncestor = VInfo.Ancestor;
78 InfoRec &VAInfo = Info[VAncestor];
79 if (VAInfo.Ancestor == 0)
82 Compress(VAncestor, VAInfo);
84 BasicBlock *VAncestorLabel = VAInfo.Label;
85 BasicBlock *VLabel = VInfo.Label;
86 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
87 VInfo.Label = VAncestorLabel;
89 VInfo.Ancestor = VAInfo.Ancestor;
92 BasicBlock *ImmediatePostDominators::Eval(BasicBlock *V) {
93 InfoRec &VInfo = Info[V];
95 // Higher-complexity but faster implementation
96 if (VInfo.Ancestor == 0)
102 void ImmediatePostDominators::Link(BasicBlock *V, BasicBlock *W,
104 // Higher-complexity but faster implementation
108 bool ImmediatePostDominators::runOnFunction(Function &F) {
109 IDoms.clear(); // Reset from the last time we were run...
112 // Step #0: Scan the function looking for the root nodes of the post-dominance
113 // relationships. These blocks, which have no successors, end with return and
114 // unwind instructions.
115 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
116 if (succ_begin(I) == succ_end(I))
121 // Step #1: Number blocks in depth-first order and initialize variables used
122 // in later stages of the algorithm.
124 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
125 N = DFSPass(Roots[i], Info[Roots[i]], N);
127 for (unsigned i = N; i >= 2; --i) {
128 BasicBlock *W = Vertex[i];
129 InfoRec &WInfo = Info[W];
131 // Step #2: Calculate the semidominators of all vertices
132 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
133 if (Info.count(*SI)) { // Only if this predecessor is reachable!
134 unsigned SemiU = Info[Eval(*SI)].Semi;
135 if (SemiU < WInfo.Semi)
139 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
141 BasicBlock *WParent = WInfo.Parent;
142 Link(WParent, W, WInfo);
144 // Step #3: Implicitly define the immediate dominator of vertices
145 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
146 while (!WParentBucket.empty()) {
147 BasicBlock *V = WParentBucket.back();
148 WParentBucket.pop_back();
149 BasicBlock *U = Eval(V);
150 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
154 // Step #4: Explicitly define the immediate dominator of each vertex
155 for (unsigned i = 2; i <= N; ++i) {
156 BasicBlock *W = Vertex[i];
157 BasicBlock *&WIDom = IDoms[W];
158 if (WIDom != Vertex[Info[W].Semi])
159 WIDom = IDoms[WIDom];
162 // Free temporary memory used to construct idom's
164 std::vector<BasicBlock*>().swap(Vertex);
169 //===----------------------------------------------------------------------===//
170 // PostDominatorTree Implementation
171 //===----------------------------------------------------------------------===//
173 static RegisterPass<PostDominatorTree>
174 F("postdomtree", "Post-Dominator Tree Construction", true);
176 DominatorTreeBase::Node *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
177 Node *&BBNode = Nodes[BB];
178 if (BBNode) return BBNode;
180 // Haven't calculated this node yet? Get or calculate the node for the
181 // immediate postdominator.
182 BasicBlock *IPDom = getAnalysis<ImmediatePostDominators>()[BB];
183 Node *IPDomNode = getNodeForBlock(IPDom);
185 // Add a new tree node for this BasicBlock, and link it as a child of
187 return BBNode = IPDomNode->addChild(new Node(BB, IPDomNode));
190 void PostDominatorTree::calculate(const ImmediatePostDominators &IPD) {
191 if (Roots.empty()) return;
193 // Add a node for the root. This node might be the actual root, if there is
194 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
195 // which postdominates all real exits if there are multiple exit blocks.
196 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
197 Nodes[Root] = RootNode = new Node(Root, 0);
199 Function *F = Roots[0]->getParent();
200 // Loop over all of the reachable blocks in the function...
201 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
202 if (BasicBlock *ImmPostDom = IPD.get(I)) { // Reachable block.
203 Node *&BBNode = Nodes[I];
204 if (!BBNode) { // Haven't calculated this node yet?
205 // Get or calculate the node for the immediate dominator
206 Node *IPDomNode = getNodeForBlock(ImmPostDom);
208 // Add a new tree node for this BasicBlock, and link it as a child of
210 BBNode = IPDomNode->addChild(new Node(I, IPDomNode));
215 //===----------------------------------------------------------------------===//
216 // PostETForest Implementation
217 //===----------------------------------------------------------------------===//
219 static RegisterPass<PostETForest>
220 G("postetforest", "Post-ET-Forest Construction", true);
222 ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
223 ETNode *&BBNode = Nodes[BB];
224 if (BBNode) return BBNode;
226 // Haven't calculated this node yet? Get or calculate the node for the
227 // immediate dominator.
228 BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
230 // If we are unreachable, we may not have an immediate dominator.
232 return BBNode = new ETNode(BB);
234 ETNode *IDomNode = getNodeForBlock(IDom);
236 // Add a new tree node for this BasicBlock, and link it as a child of
238 BBNode = new ETNode(BB);
239 BBNode->setFather(IDomNode);
244 void PostETForest::calculate(const ImmediatePostDominators &ID) {
245 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
246 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
248 // Iterate over all nodes in inverse depth first order.
249 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
250 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
251 E = idf_end(Roots[i]); I != E; ++I) {
253 ETNode *&BBNode = Nodes[BB];
255 ETNode *IDomNode = NULL;
258 IDomNode = getNodeForBlock(ID.get(BB));
260 // Add a new ETNode for this BasicBlock, and set it's parent
261 // to it's immediate dominator.
262 BBNode = new ETNode(BB);
264 BBNode->setFather(IDomNode);
269 // Iterate over all nodes in depth first order...
270 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
271 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
272 E = idf_end(Roots[i]); I != E; ++I) {
273 if (!getNodeForBlock(*I)->hasFather())
274 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
279 //===----------------------------------------------------------------------===//
280 // PostDominanceFrontier Implementation
281 //===----------------------------------------------------------------------===//
283 static RegisterPass<PostDominanceFrontier>
284 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
286 const DominanceFrontier::DomSetType &
287 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
288 const DominatorTree::Node *Node) {
289 // Loop over CFG successors to calculate DFlocal[Node]
290 BasicBlock *BB = Node->getBlock();
291 DomSetType &S = Frontiers[BB]; // The new set to fill in...
292 if (getRoots().empty()) return S;
295 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
297 // Does Node immediately dominate this predecessor?
298 if (DT[*SI]->getIDom() != Node)
301 // At this point, S is DFlocal. Now we union in DFup's of our children...
302 // Loop through and visit the nodes that Node immediately dominates (Node's
303 // children in the IDomTree)
305 for (PostDominatorTree::Node::const_iterator
306 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
307 DominatorTree::Node *IDominee = *NI;
308 const DomSetType &ChildDF = calculate(DT, IDominee);
310 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
311 for (; CDFI != CDFE; ++CDFI) {
312 if (!Node->properlyDominates(DT[*CDFI]))
320 // Ensure that this .cpp file gets linked when PostDominators.h is used.
321 DEFINING_FILE_FOR(PostDominanceFrontier)