For PR411:
[oota-llvm.git] / lib / Analysis / PostDominators.cpp
index e5908c496b4c95b2e8c43defb525d48a57161102..f9f9a42acc5578bb179c42c6236810585be103ac 100644 (file)
@@ -1,10 +1,10 @@
 //===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This file implements the post-dominator construction algorithms.
@@ -40,8 +40,7 @@ bool PostDominatorSet::runOnFunction(Function &F) {
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
     Doms[I];  // Initialize to empty
 
-    if (isa<ReturnInst>(I->getTerminator()) ||
-        isa<UnwindInst>(I->getTerminator()))
+    if (succ_begin(I) == succ_end(I))
       Roots.push_back(I);
   }
 
@@ -74,7 +73,7 @@ bool PostDominatorSet::runOnFunction(Function &F) {
           //
           while (Doms[*SI].size() == 0) ++SI;
           WorkingSet = Doms[*SI];
-          
+
           for (++SI; SI != SE; ++SI) { // Intersect all of the successor sets
             DomSetType &SuccSet = Doms[*SI];
             if (SuccSet.size())
@@ -89,7 +88,7 @@ bool PostDominatorSet::runOnFunction(Function &F) {
           if (Roots.size() > 1)
             WorkingSet.insert(0);
         }
-       
+
         WorkingSet.insert(BB);           // A block always dominates itself
         DomSetType &BBSet = Doms[BB];
         if (BBSet != WorkingSet) {
@@ -116,7 +115,7 @@ void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) {
   // Loop over all of the nodes that have dominators... figuring out the IDOM
   // for each node...
   //
-  for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end(); 
+  for (DominatorSet::const_iterator DI = DS.begin(), DEnd = DS.end();
        DI != DEnd; ++DI) {
     BasicBlock *BB = DI->first;
     const DominatorSet::DomSetType &Dominators = DI->second;
@@ -134,13 +133,13 @@ void ImmediatePostDominators::calcIDoms(const DominatorSetBase &DS) {
     for (; I != End; ++I) {   // Iterate over dominators...
       // All of our dominators should form a chain, where the number of elements
       // in the dominator set indicates what level the node is at in the chain.
-      // We want the node immediately above us, so it will have an identical 
+      // We want the node immediately above us, so it will have an identical
       // dominator set, except that BB will not dominate it... therefore it's
       // dominator set size will be one less than BB's...
       //
       if (DS.getDominators(*I).size() == DomSetSize - 1) {
-       IDoms[BB] = *I;
-       break;
+        IDoms[BB] = *I;
+        break;
       }
     }
   }
@@ -182,9 +181,8 @@ void PostDominatorTree::calculate(const PostDominatorSet &DS) {
       // be a predecessor in the depth first order that we are iterating through
       // the function.
       //
-      DominatorSet::DomSetType::const_iterator I = Dominators.begin();
-      DominatorSet::DomSetType::const_iterator End = Dominators.end();
-      for (; I != End; ++I) {   // Iterate over dominators...
+      for (DominatorSet::DomSetType::const_iterator I = Dominators.begin(),
+           E = Dominators.end(); I != E; ++I) {  // Iterate over dominators.
         // All of our dominators should form a chain, where the number
         // of elements in the dominator set indicates what level the
         // node is at in the chain.  We want the node immediately
@@ -193,12 +191,12 @@ void PostDominatorTree::calculate(const PostDominatorSet &DS) {
         // dominator set size will be one less than BB's...
         //
         if (DS.getDominators(*I).size() == DomSetSize - 1) {
-          // We know that the immediate dominator should already have a node, 
+          // We know that the immediate dominator should already have a node,
           // because we are traversing the CFG in depth first order!
           //
           Node *IDomNode = Nodes[*I];
           assert(IDomNode && "No node for IDOM?");
-         
+
           // Add a new tree node for this BasicBlock, and link it as a child of
           // IDomNode
           Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
@@ -207,6 +205,69 @@ void PostDominatorTree::calculate(const PostDominatorSet &DS) {
       }
     }
 }
+//===----------------------------------------------------------------------===//
+// PostETForest Implementation
+//===----------------------------------------------------------------------===//
+
+static RegisterAnalysis<PostETForest>
+G("postetforest", "Post-ET-Forest Construction", true);
+
+ETNode *PostETForest::getNodeForBlock(BasicBlock *BB) {
+  ETNode *&BBNode = Nodes[BB];
+  if (BBNode) return BBNode;
+
+  // Haven't calculated this node yet?  Get or calculate the node for the
+  // immediate dominator.
+  BasicBlock *IDom = getAnalysis<ImmediatePostDominators>()[BB];
+
+  // If we are unreachable, we may not have an immediate dominator.
+  if (!IDom)
+    return BBNode = new ETNode(BB);
+  else {
+    ETNode *IDomNode = getNodeForBlock(IDom);
+    
+    // Add a new tree node for this BasicBlock, and link it as a child of
+    // IDomNode
+    BBNode = new ETNode(BB);
+    BBNode->setFather(IDomNode);
+    return BBNode;
+  }
+}
+
+void PostETForest::calculate(const ImmediatePostDominators &ID) {
+  for (unsigned i = 0, e = Roots.size(); i != e; ++i)
+    Nodes[Roots[i]] = new ETNode(Roots[i]); // Add a node for the root
+
+  // Iterate over all nodes in inverse depth first order.
+  for (unsigned i = 0, e = Roots.size(); i != e; ++i)
+    for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
+           E = idf_end(Roots[i]); I != E; ++I) {
+    BasicBlock *BB = *I;
+    ETNode *&BBNode = Nodes[BB];
+    if (!BBNode) {  
+      ETNode *IDomNode =  NULL;
+
+      if (ID.get(BB))
+        IDomNode = getNodeForBlock(ID.get(BB));
+
+      // Add a new ETNode for this BasicBlock, and set it's parent
+      // to it's immediate dominator.
+      BBNode = new ETNode(BB);
+      if (IDomNode)          
+        BBNode->setFather(IDomNode);
+    }
+  }
+
+  int dfsnum = 0;
+  // Iterate over all nodes in depth first order...
+  for (unsigned i = 0, e = Roots.size(); i != e; ++i)
+    for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
+           E = idf_end(Roots[i]); I != E; ++I) {
+        if (!getNodeForBlock(*I)->hasFather())
+          getNodeForBlock(*I)->assignDFSNumber(dfsnum);
+    }
+  DFSInfoValid = true;
+}
 
 //===----------------------------------------------------------------------===//
 //  PostDominanceFrontier Implementation
@@ -216,7 +277,7 @@ static RegisterAnalysis<PostDominanceFrontier>
 H("postdomfrontier", "Post-Dominance Frontier Construction", true);
 
 const DominanceFrontier::DomSetType &
-PostDominanceFrontier::calculate(const PostDominatorTree &DT, 
+PostDominanceFrontier::calculate(const PostDominatorTree &DT,
                                  const DominatorTree::Node *Node) {
   // Loop over CFG successors to calculate DFlocal[Node]
   BasicBlock *BB = Node->getBlock();
@@ -241,8 +302,8 @@ PostDominanceFrontier::calculate(const PostDominatorTree &DT,
 
     DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
     for (; CDFI != CDFE; ++CDFI) {
-      if (!Node->dominates(DT[*CDFI]))
-       S.insert(*CDFI);
+      if (!Node->properlyDominates(DT[*CDFI]))
+        S.insert(*CDFI);
     }
   }