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