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"
20 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
21 // Step #0: Scan the function looking for the root nodes of the post-dominance
22 // relationships. These blocks, which have no successors, end with return and
23 // unwind instructions.
24 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
25 TerminatorInst *Insn = I->getTerminator();
26 if (Insn->getNumSuccessors() == 0) {
27 // Unreachable block is not a root node.
28 if (!isa<UnreachableInst>(Insn))
29 PDT.Roots.push_back(I);
32 // Prepopulate maps so that we don't get iterator invalidation issues later.
34 PDT.DomTreeNodes[I] = 0;
37 PDT.Vertex.push_back(0);
39 // Step #1: Number blocks in depth-first order and initialize variables used
40 // in later stages of the algorithm.
42 for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
43 N = PDT.DFSPass(PDT.Roots[i], N);
45 for (unsigned i = N; i >= 2; --i) {
46 BasicBlock *W = PDT.Vertex[i];
47 PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
49 // Step #2: Calculate the semidominators of all vertices
50 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
51 if (PDT.Info.count(*SI)) { // Only if this predecessor is reachable!
52 unsigned SemiU = PDT.Info[Eval(PDT, *SI)].Semi;
53 if (SemiU < WInfo.Semi)
57 PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
59 BasicBlock *WParent = WInfo.Parent;
60 Link(PDT, WParent, W, WInfo);
62 // Step #3: Implicitly define the immediate dominator of vertices
63 std::vector<BasicBlock*> &WParentBucket = PDT.Info[WParent].Bucket;
64 while (!WParentBucket.empty()) {
65 BasicBlock *V = WParentBucket.back();
66 WParentBucket.pop_back();
67 BasicBlock *U = Eval(PDT, V);
68 PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
72 // Step #4: Explicitly define the immediate dominator of each vertex
73 for (unsigned i = 2; i <= N; ++i) {
74 BasicBlock *W = PDT.Vertex[i];
75 BasicBlock *&WIDom = PDT.IDoms[W];
76 if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
77 WIDom = PDT.IDoms[WIDom];
80 if (PDT.Roots.empty()) return;
82 // Add a node for the root. This node might be the actual root, if there is
83 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
84 // which postdominates all real exits if there are multiple exit blocks.
85 BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
86 PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
88 // Loop over all of the reachable blocks in the function...
89 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
90 if (BasicBlock *ImmPostDom = PDT.getIDom(I)) { // Reachable block.
91 DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
92 if (!BBNode) { // Haven't calculated this node yet?
93 // Get or calculate the node for the immediate dominator
94 DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
96 // Add a new tree node for this BasicBlock, and link it as a child of
98 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
99 PDT.DomTreeNodes[I] = C;
100 BBNode = IPDomNode->addChild(C);
104 // Free temporary memory used to construct idom's
107 std::vector<BasicBlock*>().swap(PDT.Vertex);
109 // Start out with the DFS numbers being invalid. Let them be computed if
111 PDT.DFSInfoValid = false;