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 char PostETForest::ID = 0;
28 static RegisterPass<PostDominatorTree>
29 F("postdomtree", "Post-Dominator Tree Construction", true);
31 unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
33 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
34 std::set<BasicBlock *> visited;
35 workStack.push_back(std::make_pair(V, &VInfo));
38 BasicBlock *currentBB = workStack.back().first;
39 InfoRec *currentVInfo = workStack.back().second;
41 // Visit each block only once.
42 if (visited.count(currentBB) == 0) {
44 visited.insert(currentBB);
45 currentVInfo->Semi = ++N;
46 currentVInfo->Label = currentBB;
48 Vertex.push_back(currentBB); // Vertex[n] = current;
49 // Info[currentBB].Ancestor = 0;
51 // Child[currentBB] = 0;
52 currentVInfo->Size = 1; // Size[currentBB] = 1
56 bool visitChild = false;
57 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
58 PI != PE && !visitChild; ++PI) {
59 InfoRec &SuccVInfo = Info[*PI];
60 if (SuccVInfo.Semi == 0) {
61 SuccVInfo.Parent = currentBB;
62 if (visited.count (*PI) == 0) {
63 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
69 // If all children are visited or if this block has no child then pop this
70 // block out of workStack.
74 } while (!workStack.empty());
79 void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
80 BasicBlock *VAncestor = VInfo.Ancestor;
81 InfoRec &VAInfo = Info[VAncestor];
82 if (VAInfo.Ancestor == 0)
85 Compress(VAncestor, VAInfo);
87 BasicBlock *VAncestorLabel = VAInfo.Label;
88 BasicBlock *VLabel = VInfo.Label;
89 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
90 VInfo.Label = VAncestorLabel;
92 VInfo.Ancestor = VAInfo.Ancestor;
95 BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
96 InfoRec &VInfo = Info[V];
98 // Higher-complexity but faster implementation
99 if (VInfo.Ancestor == 0)
105 void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
107 // Higher-complexity but faster implementation
111 void PostDominatorTree::calculate(Function &F) {
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 if (Roots.empty()) return;
164 // Add a node for the root. This node might be the actual root, if there is
165 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
166 // which postdominates all real exits if there are multiple exit blocks.
167 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
168 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
170 // Loop over all of the reachable blocks in the function...
171 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
172 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
173 DomTreeNode *&BBNode = DomTreeNodes[I];
174 if (!BBNode) { // Haven't calculated this node yet?
175 // Get or calculate the node for the immediate dominator
176 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
178 // Add a new tree node for this BasicBlock, and link it as a child of
180 BBNode = IPDomNode->addChild(new DomTreeNode(I, IPDomNode));
184 // Free temporary memory used to construct idom's
187 std::vector<BasicBlock*>().swap(Vertex);
191 DominatorTreeBase::DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
192 DomTreeNode *&BBNode = DomTreeNodes[BB];
193 if (BBNode) return BBNode;
195 // Haven't calculated this node yet? Get or calculate the node for the
196 // immediate postdominator.
197 BasicBlock *IPDom = getIDom(BB);
198 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
200 // Add a new tree node for this BasicBlock, and link it as a child of
202 return BBNode = IPDomNode->addChild(new DomTreeNode(BB, IPDomNode));
205 //===----------------------------------------------------------------------===//
206 // PostETForest Implementation
207 //===----------------------------------------------------------------------===//
209 static RegisterPass<PostETForest>
210 G("postetforest", "Post-ET-Forest Construction", true);
212 ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
213 ETNode *&BBNode = Nodes[BB];
214 if (BBNode) return BBNode;
216 // Haven't calculated this node yet? Get or calculate the node for the
217 // immediate dominator.
218 PostDominatorTree::DomTreeNode *node = getAnalysis<PostDominatorTree>().getNode(BB);
220 // If we are unreachable, we may not have an immediate dominator.
223 else if (!node->getIDom())
224 return BBNode = new ETNode(BB);
226 ETNode *IDomNode = getNodeForBlock(node->getIDom()->getBlock());
228 // Add a new tree node for this BasicBlock, and link it as a child of
230 BBNode = new ETNode(BB);
231 BBNode->setFather(IDomNode);
236 void PostETForest::calculate(const PostDominatorTree &DT) {
237 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
238 Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
240 // Iterate over all nodes in inverse depth first order.
241 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
242 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
243 E = idf_end(Roots[i]); I != E; ++I) {
245 ETNode *&BBNode = Nodes[BB];
247 ETNode *IDomNode = NULL;
248 PostDominatorTree::DomTreeNode *node = DT.getNode(BB);
249 if (node && node->getIDom())
250 IDomNode = getNodeForBlock(node->getIDom()->getBlock());
252 // Add a new ETNode for this BasicBlock, and set it's parent
253 // to it's immediate dominator.
254 BBNode = new ETNode(BB);
256 BBNode->setFather(IDomNode);
261 // Iterate over all nodes in depth first order...
262 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
263 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
264 E = idf_end(Roots[i]); I != E; ++I) {
265 if (!getNodeForBlock(*I)->hasFather())
266 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
271 //===----------------------------------------------------------------------===//
272 // PostDominanceFrontier Implementation
273 //===----------------------------------------------------------------------===//
275 static RegisterPass<PostDominanceFrontier>
276 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
278 const DominanceFrontier::DomSetType &
279 PostDominanceFrontier::calculate(const PostDominatorTree &DT,
280 const DominatorTree::DomTreeNode *Node) {
281 // Loop over CFG successors to calculate DFlocal[Node]
282 BasicBlock *BB = Node->getBlock();
283 DomSetType &S = Frontiers[BB]; // The new set to fill in...
284 if (getRoots().empty()) return S;
287 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
289 // Does Node immediately dominate this predecessor?
290 DominatorTree::DomTreeNode *SINode = DT[*SI];
291 if (SINode && SINode->getIDom() != Node)
295 // At this point, S is DFlocal. Now we union in DFup's of our children...
296 // Loop through and visit the nodes that Node immediately dominates (Node's
297 // children in the IDomTree)
299 for (PostDominatorTree::DomTreeNode::const_iterator
300 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
301 DominatorTree::DomTreeNode *IDominee = *NI;
302 const DomSetType &ChildDF = calculate(DT, IDominee);
304 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
305 for (; CDFI != CDFE; ++CDFI) {
306 if (!Node->properlyDominates(DT[*CDFI]))
314 // Ensure that this .cpp file gets linked when PostDominators.h is used.
315 DEFINING_FILE_FOR(PostDominanceFrontier)