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