1d245d474f5db7b9f0178deec444e397eabdec31
[oota-llvm.git] / lib / VMCore / DominatorCalculation.h
1 //==- DominatorCalculation.h - Dominator Calculation -------------*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_VMCORE_DOMINATOR_CALCULATION_H
11 #define LLVM_VMCORE_DOMINATOR_CALCULATION_H
12
13 #include "llvm/Analysis/Dominators.h"
14 #include "llvm/Analysis/DominatorInternals.h"
15
16 //===----------------------------------------------------------------------===//
17 //
18 // DominatorTree construction - This pass constructs immediate dominator
19 // information for a flow-graph based on the algorithm described in this
20 // document:
21 //
22 //   A Fast Algorithm for Finding Dominators in a Flowgraph
23 //   T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
24 //
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:
30 //
31 #define BALANCE_IDOM_TREE 0
32 //
33 //===----------------------------------------------------------------------===//
34
35 namespace llvm {
36   
37 void DTcalculate(DominatorTree& DT, Function &F) {
38   BasicBlock* Root = DT.Roots[0];
39
40   // Add a node for the root...
41   DT.DomTreeNodes[Root] = DT.RootNode = new DomTreeNode(Root, 0);
42
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);
46
47   for (unsigned i = N; i >= 2; --i) {
48     BasicBlock *W = DT.Vertex[i];
49     DominatorTree::InfoRec &WInfo = DT.Info[W];
50
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)
56           WInfo.Semi = SemiU;
57       }
58
59     DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
60
61     BasicBlock *WParent = WInfo.Parent;
62     Link<GraphTraits<BasicBlock*> >(DT, WParent, W, WInfo);
63
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;
71     }
72   }
73
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];
80   }
81
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?
87
88       // Get or calculate the node for the immediate dominator
89       DomTreeNode *IDomNode = DT.getNodeForBlock(ImmDom);
90
91       // Add a new tree node for this BasicBlock, and link it as a child of
92       // IDomNode
93       DomTreeNode *C = new DomTreeNode(I, IDomNode);
94       DT.DomTreeNodes[I] = IDomNode->addChild(C);
95     }
96
97   // Free temporary memory used to construct idom's
98   DT.Info.clear();
99   DT.IDoms.clear();
100   std::vector<BasicBlock*>().swap(DT.Vertex);
101
102   DT.updateDFSNumbers();
103 }
104
105 }
106 #endif