Add missing end-of-file newlines.
[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
15 //===----------------------------------------------------------------------===//
16 //
17 // DominatorTree construction - This pass constructs immediate dominator
18 // information for a flow-graph based on the algorithm described in this
19 // document:
20 //
21 //   A Fast Algorithm for Finding Dominators in a Flowgraph
22 //   T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
23 //
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:
29 //
30 #define BALANCE_IDOM_TREE 0
31 //
32 //===----------------------------------------------------------------------===//
33
34 namespace llvm {
35
36 void DTcalculate(DominatorTree& DT, Function &F) {
37   BasicBlock* Root = DT.Roots[0];
38
39   // Add a node for the root...
40   DT.DomTreeNodes[Root] = DT.RootNode = new DomTreeNode(Root, 0);
41
42   DT.Vertex.push_back(0);
43
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);
47
48   for (unsigned i = N; i >= 2; --i) {
49     BasicBlock *W = DT.Vertex[i];
50     DominatorTree::InfoRec &WInfo = DT.Info[W];
51
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)
57           WInfo.Semi = SemiU;
58       }
59
60     DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
61
62     BasicBlock *WParent = WInfo.Parent;
63     Link(DT, WParent, W, WInfo);
64
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;
72     }
73   }
74
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];
81   }
82
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?
88
89       // Get or calculate the node for the immediate dominator
90       DomTreeNode *IDomNode = DT.getNodeForBlock(ImmDom);
91
92       // Add a new tree node for this BasicBlock, and link it as a child of
93       // IDomNode
94       DomTreeNode *C = new DomTreeNode(I, IDomNode);
95       DT.DomTreeNodes[I] = IDomNode->addChild(C);
96     }
97
98   // Free temporary memory used to construct idom's
99   DT.Info.clear();
100   DT.IDoms.clear();
101   std::vector<BasicBlock*>().swap(DT.Vertex);
102
103   DT.updateDFSNumbers();
104 }
105
106 }
107 #endif