llvm.org GIT mirror llvm / 1f23e16
Major repairs to the post-dominators implementation. Patch from Florian Brandner! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49768 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 12 years ago
4 changed file(s) with 115 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
4141 // documentation purposes.
4242 #if 0
4343 InfoRec &VInfo = DT.Info[DT.Roots[i]];
44 VInfo.Semi = ++N;
44 VInfo.DFSNum = VInfo.Semi = ++N;
4545 VInfo.Label = V;
4646
4747 Vertex.push_back(V); // Vertex[n] = V;
5757 }
5858 }
5959 #else
60 bool IsChilOfArtificialExit = (N != 0);
61
6062 std::vector
6163 typename GraphT::ChildIteratorType> > Worklist;
6264 Worklist.push_back(std::make_pair(V, GraphT::child_begin(V)));
6466 typename GraphT::NodeType* BB = Worklist.back().first;
6567 typename GraphT::ChildIteratorType NextSucc = Worklist.back().second;
6668
69 typename DominatorTreeBase::InfoRec &BBInfo =
70 DT.Info[BB];
71
6772 // First time we visited this BB?
6873 if (NextSucc == GraphT::child_begin(BB)) {
69 typename DominatorTreeBase::InfoRec &BBInfo =
70 DT.Info[BB];
71 BBInfo.Semi = ++N;
74 BBInfo.DFSNum = BBInfo.Semi = ++N;
7275 BBInfo.Label = BB;
7376
7477 DT.Vertex.push_back(BB); // Vertex[n] = V;
7578 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
7679 //BBInfo[V].Child = 0; // Child[v] = 0
7780 BBInfo.Size = 1; // Size[v] = 1
78 }
79
81
82 if (IsChilOfArtificialExit)
83 BBInfo.Parent = 1;
84
85 IsChilOfArtificialExit = false;
86 }
87
88 // store the DFS number of the current BB - the reference to BBInfo might
89 // get invalidated when processing the successors.
90 unsigned BBDFSNum = BBInfo.DFSNum;
91
8092 // If we are done with this block, remove it from the worklist.
8193 if (NextSucc == GraphT::child_end(BB)) {
8294 Worklist.pop_back();
92104 typename DominatorTreeBase::InfoRec &SuccVInfo =
93105 DT.Info[Succ];
94106 if (SuccVInfo.Semi == 0) {
95 SuccVInfo.Parent = BB;
107 SuccVInfo.Parent = BBDFSNum;
96108 Worklist.push_back(std::make_pair(Succ, GraphT::child_begin(Succ)));
97109 }
98110 }
105117 typename GraphT::NodeType *VIn) {
106118 std::vector Work;
107119 SmallPtrSet Visited;
108 typename GraphT::NodeType* VInAncestor = DT.Info[VIn].Ancestor;
109120 typename DominatorTreeBase::InfoRec &VInVAInfo =
110 DT.Info[VInAncestor];
121 DT.Info[DT.Vertex[DT.Info[VIn].Ancestor]];
111122
112123 if (VInVAInfo.Ancestor != 0)
113124 Work.push_back(VIn);
116127 typename GraphT::NodeType* V = Work.back();
117128 typename DominatorTreeBase::InfoRec &VInfo =
118129 DT.Info[V];
119 typename GraphT::NodeType* VAncestor = VInfo.Ancestor;
130 typename GraphT::NodeType* VAncestor = DT.Vertex[VInfo.Ancestor];
120131 typename DominatorTreeBase::InfoRec &VAInfo =
121132 DT.Info[VAncestor];
122133
167178
168179 template
169180 void Link(DominatorTreeBase& DT,
170 typename GraphT::NodeType* V, typename GraphT::NodeType* W,
181 unsigned DFSNumV, typename GraphT::NodeType* W,
171182 typename DominatorTreeBase::InfoRec &WInfo) {
172183 #if !BALANCE_IDOM_TREE
173184 // Higher-complexity but faster implementation
174 WInfo.Ancestor = V;
185 WInfo.Ancestor = DFSNumV;
175186 #else
176187 // Lower-complexity but slower implementation
177188 GraphT::NodeType* WLabel = WInfo.Label;
219230 void Calculate(DominatorTreeBase::NodeType>& DT,
220231 FuncT& F) {
221232 typedef GraphTraits GraphT;
222
233
234 unsigned N = 0;
235
236 // Add a node for the root. This node might be the actual root, if there is
237 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
238 // which postdominates all real exits if there are multiple exit blocks.
239 typename GraphT::NodeType* Root = DT.Roots.size() == 1 ? DT.Roots[0]
240 : 0;
241 bool MultipleRoots = (DT.Roots.size() > 1);
242
243 if (MultipleRoots) {
244 typename DominatorTreeBase::InfoRec &BBInfo =
245 DT.Info[NULL];
246 BBInfo.DFSNum = BBInfo.Semi = ++N;
247 BBInfo.Label = NULL;
248
249 DT.Vertex.push_back(NULL); // Vertex[n] = V;
250 //BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
251 //BBInfo[V].Child = 0; // Child[v] = 0
252 BBInfo.Size = 1; // Size[v] = 1
253 }
254
223255 // Step #1: Number blocks in depth-first order and initialize variables used
224256 // in later stages of the algorithm.
225 unsigned N = 0;
226257 for (unsigned i = 0, e = DT.Roots.size(); i != e; ++i)
227258 N = DFSPass(DT, DT.Roots[i], N);
228259
232263 DT.Info[W];
233264
234265 // Step #2: Calculate the semidominators of all vertices
266 bool HasChildOutsideDFS = false;
267
268 // initialize the semi dominator to point to the parent node
269 WInfo.Semi = WInfo.Parent;
235270 for (typename GraphTraits >::ChildIteratorType CI =
236271 GraphTraits >::child_begin(W),
237 E = GraphTraits >::child_end(W); CI != E; ++CI)
272 E = GraphTraits >::child_end(W); CI != E; ++CI) {
238273 if (DT.Info.count(*CI)) { // Only if this predecessor is reachable!
239274 unsigned SemiU = DT.Info[Eval(DT, *CI)].Semi;
240275 if (SemiU < WInfo.Semi)
241276 WInfo.Semi = SemiU;
242277 }
278 else {
279 // if the child has no DFS number it is not post-dominated by any exit,
280 // and so is the current block.
281 HasChildOutsideDFS = true;
282 }
283 }
284
285 // if some child has no DFS number it is not post-dominated by any exit,
286 // and so is the current block.
287 if (DT.isPostDominator() && HasChildOutsideDFS)
288 WInfo.Semi = 0;
243289
244290 DT.Info[DT.Vertex[WInfo.Semi]].Bucket.push_back(W);
245291
246 typename GraphT::NodeType* WParent = WInfo.Parent;
247 Link(DT, WParent, W, WInfo);
292 typename GraphT::NodeType* WParent = DT.Vertex[WInfo.Parent];
293 Link(DT, WInfo.Parent, W, WInfo);
248294
249295 // Step #3: Implicitly define the immediate dominator of vertices
250296 std::vector &WParentBucket =
270316 // Add a node for the root. This node might be the actual root, if there is
271317 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
272318 // which postdominates all real exits if there are multiple exit blocks.
273 typename GraphT::NodeType* Root = DT.Roots.size() == 1 ? DT.Roots[0]
274 : 0;
275319 DT.DomTreeNodes[Root] = DT.RootNode =
276320 new DomTreeNodeBase(Root, 0);
277321
278322 // Loop over all of the reachable blocks in the function...
279 for (typename FuncT::iterator I = F.begin(), E = F.end(); I != E; ++I)
280 if (typename GraphT::NodeType* ImmDom = DT.getIDom(I)) {
281 // Reachable block.
282 DomTreeNodeBase *BBNode = DT.DomTreeNodes[I];
283 if (BBNode) continue; // Haven't calculated this node yet?
284
285 // Get or calculate the node for the immediate dominator
286 DomTreeNodeBase *IDomNode =
323 for (unsigned i = 2; i <= N; ++i) {
324 typename GraphT::NodeType* W = DT.Vertex[i];
325
326 DomTreeNodeBase *BBNode = DT.DomTreeNodes[W];
327 if (BBNode) continue; // Haven't calculated this node yet?
328
329 typename GraphT::NodeType* ImmDom = DT.getIDom(W);
330
331 // skip all non root nodes that have no dominator - this occures with
332 // infinite loops.
333 if (!ImmDom && std::count(DT.Roots.begin(), DT.Roots.end(), W) == 0)
334 continue;
335
336 // Get or calculate the node for the immediate dominator
337 DomTreeNodeBase *IDomNode =
287338 DT.getNodeForBlock(ImmDom);
288339
289 // Add a new tree node for this BasicBlock, and link it as a child of
290 // IDomNode
291 DomTreeNodeBase *C =
292 new DomTreeNodeBase(I, IDomNode);
293 DT.DomTreeNodes[I] = IDomNode->addChild(C);
294 }
295
340 // skip all children that are dominated by a non root node that, by itself,
341 // has no dominator.
342 if (!IDomNode)
343 continue;
344
345 // Add a new tree node for this BasicBlock, and link it as a child of
346 // IDomNode
347 DomTreeNodeBase *C =
348 new DomTreeNodeBase(W, IDomNode);
349 DT.DomTreeNodes[W] = IDomNode->addChild(C);
350 }
351
296352 // Free temporary memory used to construct idom's
297353 DT.IDoms.clear();
298354 DT.Info.clear();
181181 unsigned int SlowQueries;
182182 // Information record used during immediate dominators computation.
183183 struct InfoRec {
184 unsigned DFSNum;
184185 unsigned Semi;
185186 unsigned Size;
186 NodeT *Label, *Parent, *Child, *Ancestor;
187 NodeT *Label, *Child;
188 unsigned Parent, Ancestor;
187189
188190 std::vector Bucket;
189191
190 InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0) {}
192 InfoRec() : DFSNum(0), Semi(0), Size(0), Label(0), Child(0), Parent(0),
193 Ancestor(0) {}
191194 };
192195
193196 DenseMap IDoms;
543546
544547 template
545548 friend void Link(DominatorTreeBase& DT,
546 typename GraphT::NodeType* V,
547 typename GraphT::NodeType* W,
549 unsigned DFSNumV, typename GraphT::NodeType* W,
548550 typename DominatorTreeBase::InfoRec &WInfo);
549551
550552 template
601603 // Haven't calculated this node yet? Get or calculate the node for the
602604 // immediate dominator.
603605 NodeT *IDom = getIDom(BB);
606
607 // skip all non root nodes that have no dominator
608 if (!IDom && std::count(this->Roots.begin(), this->Roots.end(), BB) == 0)
609 return NULL;
610
604611 DomTreeNodeBase *IDomNode = getNodeForBlock(IDom);
612
613 // skip all nodes that are dominated by a non root node that, by itself,
614 // has no dominator.
615 if (!IDomNode)
616 return NULL;
605617
606618 // Add a new tree node for this BasicBlock, and link it as a child of
607619 // IDomNode
615627 }
616628
617629 inline void addRoot(NodeT* BB) {
618 // Unreachable block is not a root node.
619 if (!isa(&BB->back()))
620 this->Roots.push_back(BB);
630 this->Roots.push_back(BB);
621631 }
622632
623633 public:
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #define DEBUG_TYPE "postdomtree"
14
1315 #include "llvm/Analysis/PostDominators.h"
1416 #include "llvm/Instructions.h"
1517 #include "llvm/Support/CFG.h"
18 #include "llvm/Support/Debug.h"
1619 #include "llvm/ADT/DepthFirstIterator.h"
1720 #include "llvm/ADT/SetOperations.h"
1821 #include "llvm/Analysis/DominatorInternals.h"
2932
3033 bool PostDominatorTree::runOnFunction(Function &F) {
3134 DT->recalculate(F);
35 DEBUG(DT->dump());
3236 return false;
3337 }
3438
1313 //
1414 //===----------------------------------------------------------------------===//
1515
16 #define DEBUG_TYPE "domtree"
17
1618 #include "llvm/Analysis/Dominators.h"
1719 #include "llvm/Support/CFG.h"
1820 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
1922 #include "llvm/ADT/DepthFirstIterator.h"
2023 #include "llvm/ADT/SetOperations.h"
2124 #include "llvm/ADT/SmallPtrSet.h"
5760
5861 bool DominatorTree::runOnFunction(Function &F) {
5962 DT->recalculate(F);
60
63 DEBUG(DT->dump());
6164 return false;
6265 }
6366