bf90a97a4ea019786654b7d68e6e32bbd06026b8
[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   DT.Vertex.push_back(0);
44
45   // Step #1: Number blocks in depth-first order and initialize variables used
46   // in later stages of the algorithm.
47   unsigned N = DFSPass<GraphTraits<BasicBlock*> >(DT, Root, 0);
48
49   for (unsigned i = N; i >= 2; --i) {
50     BasicBlock *W = DT.Vertex[i];
51     DominatorTree::InfoRec &WInfo = DT.Info[W];
52
53     // Step #2: Calculate the semidominators of all vertices
54     for (pred_iterator PI = pred_begin(W), E = pred_end(W); PI != E; ++PI)
55       if (DT.Info.count(*PI)) {  // Only if this predecessor is reachable!
56         unsigned SemiU = DT.Info[Eval(DT, *PI)].Semi;
57         if (SemiU < WInfo.Semi)
58           WInfo.Semi = SemiU;
59       }
60
61     DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
62
63     BasicBlock *WParent = WInfo.Parent;
64     Link(DT, WParent, W, WInfo);
65
66     // Step #3: Implicitly define the immediate dominator of vertices
67     std::vector<BasicBlock*> &WParentBucket = DT.Info[WParent].Bucket;
68     while (!WParentBucket.empty()) {
69       BasicBlock *V = WParentBucket.back();
70       WParentBucket.pop_back();
71       BasicBlock *U = Eval(DT, V);
72       DT.IDoms[V] = DT.Info[U].Semi < DT.Info[V].Semi ? U : WParent;
73     }
74   }
75
76   // Step #4: Explicitly define the immediate dominator of each vertex
77   for (unsigned i = 2; i <= N; ++i) {
78     BasicBlock *W = DT.Vertex[i];
79     BasicBlock *&WIDom = DT.IDoms[W];
80     if (WIDom != DT.Vertex[DT.Info[W].Semi])
81       WIDom = DT.IDoms[WIDom];
82   }
83
84   // Loop over all of the reachable blocks in the function...
85   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
86     if (BasicBlock *ImmDom = DT.getIDom(I)) {  // Reachable block.
87       DomTreeNode *BBNode = DT.DomTreeNodes[I];
88       if (BBNode) continue;  // Haven't calculated this node yet?
89
90       // Get or calculate the node for the immediate dominator
91       DomTreeNode *IDomNode = DT.getNodeForBlock(ImmDom);
92
93       // Add a new tree node for this BasicBlock, and link it as a child of
94       // IDomNode
95       DomTreeNode *C = new DomTreeNode(I, IDomNode);
96       DT.DomTreeNodes[I] = IDomNode->addChild(C);
97     }
98
99   // Free temporary memory used to construct idom's
100   DT.Info.clear();
101   DT.IDoms.clear();
102   std::vector<BasicBlock*>().swap(DT.Vertex);
103
104   DT.updateDFSNumbers();
105 }
106
107 }
108 #endif