Rewrite sqrt and powi to use anyfloat. By popular demand.
[oota-llvm.git] / lib / Analysis / PostDominatorCalculation.h
1 //==- PostDominatorCalculation.h - Post-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 // PostDominatorTree calculation implementation.
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
14 #define LLVM_ANALYSIS_POST_DOMINATOR_CALCULATION_H
15
16 #include "llvm/Analysis/PostDominators.h"
17 #include "llvm/Analysis/DominatorInternals.h"
18
19 namespace llvm {
20
21 void PDTcalculate(PostDominatorTree& PDT, Function &F) {
22   // Step #0: Scan the function looking for the root nodes of the post-dominance
23   // relationships.  These blocks, which have no successors, end with return and
24   // unwind instructions.
25   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
26     TerminatorInst *Insn = I->getTerminator();
27     if (Insn->getNumSuccessors() == 0) {
28       // Unreachable block is not a root node.
29       if (!isa<UnreachableInst>(Insn))
30         PDT.Roots.push_back(I);
31     }
32     
33     // Prepopulate maps so that we don't get iterator invalidation issues later.
34     PDT.IDoms[I] = 0;
35     PDT.DomTreeNodes[I] = 0;
36   }
37   
38   PDT.Vertex.push_back(0);
39   
40   // Step #1: Number blocks in depth-first order and initialize variables used
41   // in later stages of the algorithm.
42   unsigned N = 0;
43   for (unsigned i = 0, e = PDT.Roots.size(); i != e; ++i)
44     N = DFSPass<GraphTraits<Inverse<BasicBlock*> > >(PDT, PDT.Roots[i], N);
45   
46   for (unsigned i = N; i >= 2; --i) {
47     BasicBlock *W = PDT.Vertex[i];
48     PostDominatorTree::InfoRec &WInfo = PDT.Info[W];
49     
50     // Step #2: Calculate the semidominators of all vertices
51     for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
52       if (PDT.Info.count(*SI)) {  // Only if this predecessor is reachable!
53         unsigned SemiU =
54              PDT.Info[Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, *SI)].Semi;
55         if (SemiU < WInfo.Semi)
56           WInfo.Semi = SemiU;
57       }
58         
59     PDT.Info[PDT.Vertex[WInfo.Semi]].Bucket.push_back(W);
60     
61     BasicBlock *WParent = WInfo.Parent;
62     Link<GraphTraits<Inverse<BasicBlock*> > >(PDT, WParent, W, WInfo);
63     
64     // Step #3: Implicitly define the immediate dominator of vertices
65     std::vector<BasicBlock*> &WParentBucket = PDT.Info[WParent].Bucket;
66     while (!WParentBucket.empty()) {
67       BasicBlock *V = WParentBucket.back();
68       WParentBucket.pop_back();
69       BasicBlock *U = Eval<GraphTraits<Inverse<BasicBlock*> > >(PDT, V);
70       PDT.IDoms[V] = PDT.Info[U].Semi < PDT.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 = PDT.Vertex[i];
77     BasicBlock *&WIDom = PDT.IDoms[W];
78     if (WIDom != PDT.Vertex[PDT.Info[W].Semi])
79       WIDom = PDT.IDoms[WIDom];
80   }
81   
82   if (PDT.Roots.empty()) return;
83
84   // Add a node for the root.  This node might be the actual root, if there is
85   // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
86   // which postdominates all real exits if there are multiple exit blocks.
87   BasicBlock *Root = PDT.Roots.size() == 1 ? PDT.Roots[0] : 0;
88   PDT.DomTreeNodes[Root] = PDT.RootNode = new DomTreeNode(Root, 0);
89   
90   // Loop over all of the reachable blocks in the function...
91   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
92     if (BasicBlock *ImmPostDom = PDT.getIDom(I)) {  // Reachable block.
93       DomTreeNode *&BBNode = PDT.DomTreeNodes[I];
94       if (!BBNode) {  // Haven't calculated this node yet?
95                       // Get or calculate the node for the immediate dominator
96         DomTreeNode *IPDomNode = PDT.getNodeForBlock(ImmPostDom);
97         
98         // Add a new tree node for this BasicBlock, and link it as a child of
99         // IDomNode
100         DomTreeNode *C = new DomTreeNode(I, IPDomNode);
101         PDT.DomTreeNodes[I] = C;
102         BBNode = IPDomNode->addChild(C);
103       }
104     }
105
106   // Free temporary memory used to construct idom's
107   PDT.IDoms.clear();
108   PDT.Info.clear();
109   std::vector<BasicBlock*>().swap(PDT.Vertex);
110
111   // Start out with the DFS numbers being invalid.  Let them be computed if
112   // demanded.
113   PDT.DFSInfoValid = false;
114 }
115
116 }
117 #endif