1 //==- DominatorCalculation.h - 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 #ifndef LLVM_VMCORE_DOMINATOR_CALCULATION_H
11 #define LLVM_VMCORE_DOMINATOR_CALCULATION_H
13 #include "llvm/Analysis/Dominators.h"
15 //===----------------------------------------------------------------------===//
17 // DominatorTree construction - This pass constructs immediate dominator
18 // information for a flow-graph based on the algorithm described in this
21 // A Fast Algorithm for Finding Dominators in a Flowgraph
22 // T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
24 // This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
25 // LINK, but it turns out that the theoretically slower O(n*log(n))
26 // implementation is actually faster than the "efficient" algorithm (even for
27 // large CFGs) because the constant overheads are substantially smaller. The
28 // lower-complexity version can be enabled with the following #define:
30 #define BALANCE_IDOM_TREE 0
32 //===----------------------------------------------------------------------===//
36 void DTcalculate(DominatorTree& DT, Function &F) {
37 BasicBlock* Root = DT.Roots[0];
39 // Add a node for the root...
40 DT.DomTreeNodes[Root] = DT.RootNode = new DomTreeNode(Root, 0);
42 DT.Vertex.push_back(0);
44 // Step #1: Number blocks in depth-first order and initialize variables used
45 // in later stages of the algorithm.
46 unsigned N = DT.DFSPass(Root, 0);
48 for (unsigned i = N; i >= 2; --i) {
49 BasicBlock *W = DT.Vertex[i];
50 DominatorTree::InfoRec &WInfo = DT.Info[W];
52 // Step #2: Calculate the semidominators of all vertices
53 for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
54 if (DT.Info.count(*PI)) { // Only if this predecessor is reachable!
55 unsigned SemiU = DT.Info[Eval(DT, *PI)].Semi;
56 if (SemiU < WInfo.Semi)
60 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
62 BasicBlock *WParent = WInfo.Parent;
63 Link(DT, WParent, W, WInfo);
65 // Step #3: Implicitly define the immediate dominator of vertices
66 std::vector<BasicBlock*> &WParentBucket = DT.Info[WParent].Bucket;
67 while (!WParentBucket.empty()) {
68 BasicBlock *V = WParentBucket.back();
69 WParentBucket.pop_back();
70 BasicBlock *U = Eval(DT, V);
71 DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
75 // Step #4: Explicitly define the immediate dominator of each vertex
76 for (unsigned i = 2; i <= N; ++i) {
77 BasicBlock *W = DT.Vertex[i];
78 BasicBlock *&WIDom = DT.IDoms[W];
79 if (WIDom != DT.Vertex[DT.Info[W].Semi])
80 WIDom = DT.IDoms[WIDom];
83 // Loop over all of the reachable blocks in the function...
84 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
85 if (BasicBlock *ImmDom = DT.getIDom(I)) { // Reachable block.
86 DomTreeNode *BBNode = DT.DomTreeNodes[I];
87 if (BBNode) continue; // Haven't calculated this node yet?
89 // Get or calculate the node for the immediate dominator
90 DomTreeNode *IDomNode = DT.getNodeForBlock(ImmDom);
92 // Add a new tree node for this BasicBlock, and link it as a child of
94 DomTreeNode *C = new DomTreeNode(I, IDomNode);
95 DT.DomTreeNodes[I] = IDomNode->addChild(C);
98 // Free temporary memory used to construct idom's
101 std::vector<BasicBlock*>().swap(DT.Vertex);
103 DT.updateDFSNumbers();