Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===- PostDominators.cpp - Post-Dominator Calculation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the post-dominator construction algorithms. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/PostDominators.h" |
| 15 | #include "llvm/Instructions.h" |
| 16 | #include "llvm/Support/CFG.h" |
| 17 | #include "llvm/ADT/DepthFirstIterator.h" |
| 18 | #include "llvm/ADT/SetOperations.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // PostDominatorTree Implementation |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | char PostDominatorTree::ID = 0; |
| 26 | char PostDominanceFrontier::ID = 0; |
| 27 | static RegisterPass<PostDominatorTree> |
| 28 | F("postdomtree", "Post-Dominator Tree Construction", true); |
| 29 | |
| 30 | unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo, |
| 31 | unsigned N) { |
| 32 | std::vector<std::pair<BasicBlock *, InfoRec *> > workStack; |
| 33 | std::set<BasicBlock *> visited; |
| 34 | workStack.push_back(std::make_pair(V, &VInfo)); |
| 35 | |
| 36 | do { |
| 37 | BasicBlock *currentBB = workStack.back().first; |
| 38 | InfoRec *currentVInfo = workStack.back().second; |
| 39 | |
| 40 | // Visit each block only once. |
| 41 | if (visited.count(currentBB) == 0) { |
| 42 | |
| 43 | visited.insert(currentBB); |
| 44 | currentVInfo->Semi = ++N; |
| 45 | currentVInfo->Label = currentBB; |
| 46 | |
| 47 | Vertex.push_back(currentBB); // Vertex[n] = current; |
| 48 | // Info[currentBB].Ancestor = 0; |
| 49 | // Ancestor[n] = 0 |
| 50 | // Child[currentBB] = 0; |
| 51 | currentVInfo->Size = 1; // Size[currentBB] = 1 |
| 52 | } |
| 53 | |
| 54 | // Visit children |
| 55 | bool visitChild = false; |
| 56 | for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB); |
| 57 | PI != PE && !visitChild; ++PI) { |
| 58 | InfoRec &SuccVInfo = Info[*PI]; |
| 59 | if (SuccVInfo.Semi == 0) { |
| 60 | SuccVInfo.Parent = currentBB; |
| 61 | if (visited.count (*PI) == 0) { |
| 62 | workStack.push_back(std::make_pair(*PI, &SuccVInfo)); |
| 63 | visitChild = true; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // If all children are visited or if this block has no child then pop this |
| 69 | // block out of workStack. |
| 70 | if (!visitChild) |
| 71 | workStack.pop_back(); |
| 72 | |
| 73 | } while (!workStack.empty()); |
| 74 | |
| 75 | return N; |
| 76 | } |
| 77 | |
| 78 | void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) { |
| 79 | BasicBlock *VAncestor = VInfo.Ancestor; |
| 80 | InfoRec &VAInfo = Info[VAncestor]; |
| 81 | if (VAInfo.Ancestor == 0) |
| 82 | return; |
| 83 | |
| 84 | Compress(VAncestor, VAInfo); |
| 85 | |
| 86 | BasicBlock *VAncestorLabel = VAInfo.Label; |
| 87 | BasicBlock *VLabel = VInfo.Label; |
| 88 | if (Info[VAncestorLabel].Semi < Info[VLabel].Semi) |
| 89 | VInfo.Label = VAncestorLabel; |
| 90 | |
| 91 | VInfo.Ancestor = VAInfo.Ancestor; |
| 92 | } |
| 93 | |
| 94 | BasicBlock *PostDominatorTree::Eval(BasicBlock *V) { |
| 95 | InfoRec &VInfo = Info[V]; |
| 96 | |
| 97 | // Higher-complexity but faster implementation |
| 98 | if (VInfo.Ancestor == 0) |
| 99 | return V; |
| 100 | Compress(V, VInfo); |
| 101 | return VInfo.Label; |
| 102 | } |
| 103 | |
| 104 | void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W, |
| 105 | InfoRec &WInfo) { |
| 106 | // Higher-complexity but faster implementation |
| 107 | WInfo.Ancestor = V; |
| 108 | } |
| 109 | |
| 110 | void PostDominatorTree::calculate(Function &F) { |
| 111 | // Step #0: Scan the function looking for the root nodes of the post-dominance |
| 112 | // relationships. These blocks, which have no successors, end with return and |
| 113 | // unwind instructions. |
| 114 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 115 | if (succ_begin(I) == succ_end(I)) |
| 116 | Roots.push_back(I); |
| 117 | |
| 118 | Vertex.push_back(0); |
| 119 | |
| 120 | // Step #1: Number blocks in depth-first order and initialize variables used |
| 121 | // in later stages of the algorithm. |
| 122 | unsigned N = 0; |
| 123 | for (unsigned i = 0, e = Roots.size(); i != e; ++i) |
| 124 | N = DFSPass(Roots[i], Info[Roots[i]], N); |
| 125 | |
| 126 | for (unsigned i = N; i >= 2; --i) { |
| 127 | BasicBlock *W = Vertex[i]; |
| 128 | InfoRec &WInfo = Info[W]; |
| 129 | |
| 130 | // Step #2: Calculate the semidominators of all vertices |
| 131 | for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI) |
| 132 | if (Info.count(*SI)) { // Only if this predecessor is reachable! |
| 133 | unsigned SemiU = Info[Eval(*SI)].Semi; |
| 134 | if (SemiU < WInfo.Semi) |
| 135 | WInfo.Semi = SemiU; |
| 136 | } |
| 137 | |
| 138 | Info[Vertex[WInfo.Semi]].Bucket.push_back(W); |
| 139 | |
| 140 | BasicBlock *WParent = WInfo.Parent; |
| 141 | Link(WParent, W, WInfo); |
| 142 | |
| 143 | // Step #3: Implicitly define the immediate dominator of vertices |
| 144 | std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket; |
| 145 | while (!WParentBucket.empty()) { |
| 146 | BasicBlock *V = WParentBucket.back(); |
| 147 | WParentBucket.pop_back(); |
| 148 | BasicBlock *U = Eval(V); |
| 149 | IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Step #4: Explicitly define the immediate dominator of each vertex |
| 154 | for (unsigned i = 2; i <= N; ++i) { |
| 155 | BasicBlock *W = Vertex[i]; |
| 156 | BasicBlock *&WIDom = IDoms[W]; |
| 157 | if (WIDom != Vertex[Info[W].Semi]) |
| 158 | WIDom = IDoms[WIDom]; |
| 159 | } |
| 160 | |
| 161 | if (Roots.empty()) return; |
| 162 | |
| 163 | // Add a node for the root. This node might be the actual root, if there is |
| 164 | // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0) |
| 165 | // which postdominates all real exits if there are multiple exit blocks. |
| 166 | BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0; |
| 167 | DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0); |
| 168 | |
| 169 | // Loop over all of the reachable blocks in the function... |
| 170 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 171 | if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block. |
| 172 | DomTreeNode *&BBNode = DomTreeNodes[I]; |
| 173 | if (!BBNode) { // Haven't calculated this node yet? |
| 174 | // Get or calculate the node for the immediate dominator |
| 175 | DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom); |
| 176 | |
| 177 | // Add a new tree node for this BasicBlock, and link it as a child of |
| 178 | // IDomNode |
| 179 | DomTreeNode *C = new DomTreeNode(I, IPDomNode); |
| 180 | DomTreeNodes[I] = C; |
| 181 | BBNode = IPDomNode->addChild(C); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Free temporary memory used to construct idom's |
| 186 | IDoms.clear(); |
| 187 | Info.clear(); |
| 188 | std::vector<BasicBlock*>().swap(Vertex); |
| 189 | |
| 190 | int dfsnum = 0; |
| 191 | // Iterate over all nodes in depth first order... |
| 192 | for (unsigned i = 0, e = Roots.size(); i != e; ++i) |
| 193 | for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]), |
| 194 | E = idf_end(Roots[i]); I != E; ++I) { |
| 195 | if (!getNodeForBlock(*I)->getIDom()) |
| 196 | getNodeForBlock(*I)->assignDFSNumber(dfsnum); |
| 197 | } |
| 198 | DFSInfoValid = true; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) { |
| 203 | DomTreeNode *&BBNode = DomTreeNodes[BB]; |
| 204 | if (BBNode) return BBNode; |
| 205 | |
| 206 | // Haven't calculated this node yet? Get or calculate the node for the |
| 207 | // immediate postdominator. |
| 208 | BasicBlock *IPDom = getIDom(BB); |
| 209 | DomTreeNode *IPDomNode = getNodeForBlock(IPDom); |
| 210 | |
| 211 | // Add a new tree node for this BasicBlock, and link it as a child of |
| 212 | // IDomNode |
| 213 | DomTreeNode *C = new DomTreeNode(BB, IPDomNode); |
| 214 | DomTreeNodes[BB] = C; |
| 215 | return BBNode = IPDomNode->addChild(C); |
| 216 | } |
| 217 | |
| 218 | //===----------------------------------------------------------------------===// |
| 219 | // PostDominanceFrontier Implementation |
| 220 | //===----------------------------------------------------------------------===// |
| 221 | |
| 222 | static RegisterPass<PostDominanceFrontier> |
| 223 | H("postdomfrontier", "Post-Dominance Frontier Construction", true); |
| 224 | |
| 225 | const DominanceFrontier::DomSetType & |
| 226 | PostDominanceFrontier::calculate(const PostDominatorTree &DT, |
| 227 | const DomTreeNode *Node) { |
| 228 | // Loop over CFG successors to calculate DFlocal[Node] |
| 229 | BasicBlock *BB = Node->getBlock(); |
| 230 | DomSetType &S = Frontiers[BB]; // The new set to fill in... |
| 231 | if (getRoots().empty()) return S; |
| 232 | |
| 233 | if (BB) |
| 234 | for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB); |
| 235 | SI != SE; ++SI) { |
| 236 | // Does Node immediately dominate this predecessor? |
| 237 | DomTreeNode *SINode = DT[*SI]; |
| 238 | if (SINode && SINode->getIDom() != Node) |
| 239 | S.insert(*SI); |
| 240 | } |
| 241 | |
| 242 | // At this point, S is DFlocal. Now we union in DFup's of our children... |
| 243 | // Loop through and visit the nodes that Node immediately dominates (Node's |
| 244 | // children in the IDomTree) |
| 245 | // |
| 246 | for (DomTreeNode::const_iterator |
| 247 | NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) { |
| 248 | DomTreeNode *IDominee = *NI; |
| 249 | const DomSetType &ChildDF = calculate(DT, IDominee); |
| 250 | |
| 251 | DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end(); |
| 252 | for (; CDFI != CDFE; ++CDFI) { |
| 253 | if (!DT.properlyDominates(Node, DT[*CDFI])) |
| 254 | S.insert(*CDFI); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return S; |
| 259 | } |
| 260 | |
| 261 | // Ensure that this .cpp file gets linked when PostDominators.h is used. |
| 262 | DEFINING_FILE_FOR(PostDominanceFrontier) |