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)) {
116 Instruction *Insn = I->getTerminator();
117 // Unreachable block is not a root node.
118 if (!isa<UnreachableInst>(Insn))
124 // Step #1: Number blocks in depth-first order and initialize variables used
125 // in later stages of the algorithm.
127 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
128 N = DFSPass(Roots[i], Info[Roots[i]], N);
130 for (unsigned i = N; i >= 2; --i) {
131 BasicBlock *W = Vertex[i];
132 InfoRec &WInfo = Info[W];
134 // Step #2: Calculate the semidominators of all vertices
135 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
136 if (Info.count(*SI)) { // Only if this predecessor is reachable!
137 unsigned SemiU = Info[Eval(*SI)].Semi;
138 if (SemiU < WInfo.Semi)
142 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
144 BasicBlock *WParent = WInfo.Parent;
145 Link(WParent, W, WInfo);
147 // Step #3: Implicitly define the immediate dominator of vertices
148 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
149 while (!WParentBucket.empty()) {
150 BasicBlock *V = WParentBucket.back();
151 WParentBucket.pop_back();
152 BasicBlock *U = Eval(V);
153 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
157 // Step #4: Explicitly define the immediate dominator of each vertex
158 for (unsigned i = 2; i <= N; ++i) {
159 BasicBlock *W = Vertex[i];
160 BasicBlock *&WIDom = IDoms[W];
161 if (WIDom != Vertex[Info[W].Semi])
162 WIDom = IDoms[WIDom];
165 if (Roots.empty()) return;
167 // Add a node for the root. This node might be the actual root, if there is
168 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
169 // which postdominates all real exits if there are multiple exit blocks.
170 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
171 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
173 // Loop over all of the reachable blocks in the function...
174 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
175 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
176 DomTreeNode *&BBNode = DomTreeNodes[I];
177 if (!BBNode) { // Haven't calculated this node yet?
178 // Get or calculate the node for the immediate dominator
179 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
181 // Add a new tree node for this BasicBlock, and link it as a child of
183 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
185 BBNode = IPDomNode->addChild(C);
189 // Free temporary memory used to construct idom's
192 std::vector<BasicBlock*>().swap(Vertex);
195 // Iterate over all nodes in depth first order...
196 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
197 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
198 E = idf_end(Roots[i]); I != E; ++I) {
199 if (!getNodeForBlock(*I)->getIDom())
200 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
206 DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
207 DomTreeNode *&BBNode = DomTreeNodes[BB];
208 if (BBNode) return BBNode;
210 // Haven't calculated this node yet? Get or calculate the node for the
211 // immediate postdominator.
212 BasicBlock *IPDom = getIDom(BB);
213 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
215 // Add a new tree node for this BasicBlock, and link it as a child of
217 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
218 DomTreeNodes[BB] = C;
219 return BBNode = IPDomNode->addChild(C);
222 //===----------------------------------------------------------------------===//
223 // PostDominanceFrontier Implementation
224 //===----------------------------------------------------------------------===//
226 static RegisterPass<PostDominanceFrontier>
227 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
229 const DominanceFrontier::DomSetType &
230 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
231 const DomTreeNode *Node) {
232 // Loop over CFG successors to calculate DFlocal[Node]
233 BasicBlock *BB = Node->getBlock();
234 DomSetType &S = Frontiers[BB]; // The new set to fill in...
235 if (getRoots().empty()) return S;
238 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
240 // Does Node immediately dominate this predecessor?
241 DomTreeNode *SINode = DT[*SI];
242 if (SINode && SINode->getIDom() != Node)
246 // At this point, S is DFlocal. Now we union in DFup's of our children...
247 // Loop through and visit the nodes that Node immediately dominates (Node's
248 // children in the IDomTree)
250 for (DomTreeNode::const_iterator
251 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
252 DomTreeNode *IDominee = *NI;
253 const DomSetType &ChildDF = calculate(DT, IDominee);
255 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
256 for (; CDFI != CDFE; ++CDFI) {
257 if (!DT.properlyDominates(Node, DT[*CDFI]))
265 // Ensure that this .cpp file gets linked when PostDominators.h is used.
266 DEFINING_FILE_FOR(PostDominanceFrontier)