1 //==- PostDominatorCalculation.h - Post-Dominator Calculation ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Owen Anderson and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // PostDominatorTree calculation implementation.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
14 #define LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
16 #include "llvm/Analysis/PostDominators.h"
17 #include "llvm/Analysis/DominatorInternals.h"
21 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
22 // Step #0: Scan the function looking for the root nodes of the post-dominance
23 // relationships. These blocks, which have no successors, end with return and
24 // unwind instructions.
25 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
26 TerminatorInst *Insn = I->getTerminator();
27 if (Insn->getNumSuccessors() == 0) {
28 // Unreachable block is not a root node.
29 if (!isa<UnreachableInst>(Insn))
30 PDT.Roots.push_back(I);
33 // Prepopulate maps so that we don't get iterator invalidation issues later.
35 PDT.DomTreeNodes[I] = 0;
38 PDT.Vertex.push_back(0);
40 // Step #1: Number blocks in depth-first order and initialize variables used
41 // in later stages of the algorithm.
43 for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
44 N = DFSPass<GraphTraits<Inverse<BasicBlock*> > >(PDT, PDT.Roots[i], N);
46 for (unsigned i = N; i >= 2; --i) {
47 BasicBlock *W = PDT.Vertex[i];
48 PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
50 // Step #2: Calculate the semidominators of all vertices
51 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
52 if (PDT.Info.count(*SI)) { // Only if this predecessor is reachable!
54 PDT.Info[Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, *SI)].Semi;
55 if (SemiU < WInfo.Semi)
59 PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
61 BasicBlock *WParent = WInfo.Parent;
62 Link<GraphTraits<Inverse<BasicBlock*> > >(PDT, WParent, W, WInfo);
64 // Step #3: Implicitly define the immediate dominator of vertices
65 std::vector<BasicBlock*> &WParentBucket = PDT.Info[WParent].Bucket;
66 while (!WParentBucket.empty()) {
67 BasicBlock *V = WParentBucket.back();
68 WParentBucket.pop_back();
69 BasicBlock *U = Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, V);
70 PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
74 // Step #4: Explicitly define the immediate dominator of each vertex
75 for (unsigned i = 2; i <= N; ++i) {
76 BasicBlock *W = PDT.Vertex[i];
77 BasicBlock *&WIDom = PDT.IDoms[W];
78 if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
79 WIDom = PDT.IDoms[WIDom];
82 if (PDT.Roots.empty()) return;
84 // Add a node for the root. This node might be the actual root, if there is
85 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
86 // which postdominates all real exits if there are multiple exit blocks.
87 BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
88 PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
90 // Loop over all of the reachable blocks in the function...
91 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
92 if (BasicBlock *ImmPostDom = PDT.getIDom(I)) { // Reachable block.
93 DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
94 if (!BBNode) { // Haven't calculated this node yet?
95 // Get or calculate the node for the immediate dominator
96 DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
98 // Add a new tree node for this BasicBlock, and link it as a child of
100 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
101 PDT.DomTreeNodes[I] = C;
102 BBNode = IPDomNode->addChild(C);
106 // Free temporary memory used to construct idom's
109 std::vector<BasicBlock*>().swap(PDT.Vertex);
111 // Start out with the DFS numbers being invalid. Let them be computed if
113 PDT.DFSInfoValid = false;