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 #1: Number blocks in depth-first order and initialize variables used
23 // in later stages of the algorithm.
25 for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
26 N = DFSPass<GraphTraits<Inverse<BasicBlock*> > >(PDT, PDT.Roots[i], N);
28 for (unsigned i = N; i >= 2; --i) {
29 BasicBlock *W = PDT.Vertex[i];
30 PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
32 // Step #2: Calculate the semidominators of all vertices
33 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
34 if (PDT.Info.count(*SI)) { // Only if this predecessor is reachable!
36 PDT.Info[Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, *SI)].Semi;
37 if (SemiU < WInfo.Semi)
41 PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
43 BasicBlock *WParent = WInfo.Parent;
44 Link<GraphTraits<Inverse<BasicBlock*> > >(PDT, WParent, W, WInfo);
46 // Step #3: Implicitly define the immediate dominator of vertices
47 std::vector<BasicBlock*> &WParentBucket = PDT.Info[WParent].Bucket;
48 while (!WParentBucket.empty()) {
49 BasicBlock *V = WParentBucket.back();
50 WParentBucket.pop_back();
51 BasicBlock *U = Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, V);
52 PDT.IDoms[V] = PDT.Info[U].Semi < PDT.Info[V].Semi ? U : WParent;
56 // Step #4: Explicitly define the immediate dominator of each vertex
57 for (unsigned i = 2; i <= N; ++i) {
58 BasicBlock *W = PDT.Vertex[i];
59 BasicBlock *&WIDom = PDT.IDoms[W];
60 if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
61 WIDom = PDT.IDoms[WIDom];
64 if (PDT.Roots.empty()) return;
66 // Add a node for the root. This node might be the actual root, if there is
67 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
68 // which postdominates all real exits if there are multiple exit blocks.
69 BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
70 PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
72 // Loop over all of the reachable blocks in the function...
73 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
74 if (BasicBlock *ImmPostDom = PDT.getIDom(I)) { // Reachable block.
75 DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
76 if (!BBNode) { // Haven't calculated this node yet?
77 // Get or calculate the node for the immediate dominator
78 DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
80 // Add a new tree node for this BasicBlock, and link it as a child of
82 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
83 PDT.DomTreeNodes[I] = C;
84 BBNode = IPDomNode->addChild(C);
88 // Free temporary memory used to construct idom's
91 std::vector<BasicBlock*>().swap(PDT.Vertex);
93 // Start out with the DFS numbers being invalid. Let them be computed if
95 PDT.DFSInfoValid = false;