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 // PostDominatorTree Implementation
23 //===----------------------------------------------------------------------===//
25 char PostDominatorTree::ID = 0;
26 char PostDominanceFrontier::ID = 0;
27 static RegisterPass<PostDominatorTree>
28 F("postdomtree", "Post-Dominator Tree Construction", true);
30 unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
32 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
33 std::set<BasicBlock *> visited;
34 workStack.push_back(std::make_pair(V, &VInfo));
37 BasicBlock *currentBB = workStack.back().first;
38 InfoRec *currentVInfo = workStack.back().second;
40 // Visit each block only once.
41 if (visited.count(currentBB) == 0) {
43 visited.insert(currentBB);
44 currentVInfo->Semi = ++N;
45 currentVInfo->Label = currentBB;
47 Vertex.push_back(currentBB); // Vertex[n] = current;
48 // Info[currentBB].Ancestor = 0;
50 // Child[currentBB] = 0;
51 currentVInfo->Size = 1; // Size[currentBB] = 1
55 bool visitChild = false;
56 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
57 PI != PE && !visitChild; ++PI) {
58 InfoRec &SuccVInfo = Info[*PI];
59 if (SuccVInfo.Semi == 0) {
60 SuccVInfo.Parent = currentBB;
61 if (visited.count (*PI) == 0) {
62 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
68 // If all children are visited or if this block has no child then pop this
69 // block out of workStack.
73 } while (!workStack.empty());
78 void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
79 BasicBlock *VAncestor = VInfo.Ancestor;
80 InfoRec &VAInfo = Info[VAncestor];
81 if (VAInfo.Ancestor == 0)
84 Compress(VAncestor, VAInfo);
86 BasicBlock *VAncestorLabel = VAInfo.Label;
87 BasicBlock *VLabel = VInfo.Label;
88 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
89 VInfo.Label = VAncestorLabel;
91 VInfo.Ancestor = VAInfo.Ancestor;
94 BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
95 InfoRec &VInfo = Info[V];
97 // Higher-complexity but faster implementation
98 if (VInfo.Ancestor == 0)
104 void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
106 // Higher-complexity but faster implementation
110 void PostDominatorTree::calculate(Function &F) {
111 // Step #0: Scan the function looking for the root nodes of the post-dominance
112 // relationships. These blocks, which have no successors, end with return and
113 // unwind instructions.
114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
115 if (succ_begin(I) == succ_end(I))
120 // Step #1: Number blocks in depth-first order and initialize variables used
121 // in later stages of the algorithm.
123 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
124 N = DFSPass(Roots[i], Info[Roots[i]], N);
126 for (unsigned i = N; i >= 2; --i) {
127 BasicBlock *W = Vertex[i];
128 InfoRec &WInfo = Info[W];
130 // Step #2: Calculate the semidominators of all vertices
131 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
132 if (Info.count(*SI)) { // Only if this predecessor is reachable!
133 unsigned SemiU = Info[Eval(*SI)].Semi;
134 if (SemiU < WInfo.Semi)
138 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
140 BasicBlock *WParent = WInfo.Parent;
141 Link(WParent, W, WInfo);
143 // Step #3: Implicitly define the immediate dominator of vertices
144 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
145 while (!WParentBucket.empty()) {
146 BasicBlock *V = WParentBucket.back();
147 WParentBucket.pop_back();
148 BasicBlock *U = Eval(V);
149 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
153 // Step #4: Explicitly define the immediate dominator of each vertex
154 for (unsigned i = 2; i <= N; ++i) {
155 BasicBlock *W = Vertex[i];
156 BasicBlock *&WIDom = IDoms[W];
157 if (WIDom != Vertex[Info[W].Semi])
158 WIDom = IDoms[WIDom];
161 if (Roots.empty()) return;
163 // Add a node for the root. This node might be the actual root, if there is
164 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
165 // which postdominates all real exits if there are multiple exit blocks.
166 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
167 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
169 // Loop over all of the reachable blocks in the function...
170 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
171 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
172 DomTreeNode *&BBNode = DomTreeNodes[I];
173 if (!BBNode) { // Haven't calculated this node yet?
174 // Get or calculate the node for the immediate dominator
175 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
177 // Add a new tree node for this BasicBlock, and link it as a child of
179 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
181 BBNode = IPDomNode->addChild(C);
185 // Free temporary memory used to construct idom's
188 std::vector<BasicBlock*>().swap(Vertex);
191 // Iterate over all nodes in depth first order...
192 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
193 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
194 E = idf_end(Roots[i]); I != E; ++I) {
195 if (!getNodeForBlock(*I)->getIDom())
196 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
202 DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
203 DomTreeNode *&BBNode = DomTreeNodes[BB];
204 if (BBNode) return BBNode;
206 // Haven't calculated this node yet? Get or calculate the node for the
207 // immediate postdominator.
208 BasicBlock *IPDom = getIDom(BB);
209 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
211 // Add a new tree node for this BasicBlock, and link it as a child of
213 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
214 DomTreeNodes[BB] = C;
215 return BBNode = IPDomNode->addChild(C);
218 //===----------------------------------------------------------------------===//
219 // PostDominanceFrontier Implementation
220 //===----------------------------------------------------------------------===//
222 static RegisterPass<PostDominanceFrontier>
223 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
225 const DominanceFrontier::DomSetType &
226 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
227 const DomTreeNode *Node) {
228 // Loop over CFG successors to calculate DFlocal[Node]
229 BasicBlock *BB = Node->getBlock();
230 DomSetType &S = Frontiers[BB]; // The new set to fill in...
231 if (getRoots().empty()) return S;
234 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
236 // Does Node immediately dominate this predecessor?
237 DomTreeNode *SINode = DT[*SI];
238 if (SINode && SINode->getIDom() != Node)
242 // At this point, S is DFlocal. Now we union in DFup's of our children...
243 // Loop through and visit the nodes that Node immediately dominates (Node's
244 // children in the IDomTree)
246 for (DomTreeNode::const_iterator
247 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
248 DomTreeNode *IDominee = *NI;
249 const DomSetType &ChildDF = calculate(DT, IDominee);
251 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
252 for (; CDFI != CDFE; ++CDFI) {
253 if (!DT.properlyDominates(Node, DT[*CDFI]))
261 // Ensure that this .cpp file gets linked when PostDominators.h is used.
262 DEFINING_FILE_FOR(PostDominanceFrontier)