promote extload i1 -> extload i8
[oota-llvm.git] / lib / Analysis / PostDominators.cpp
index 7dbeb35c253a37f87d275a4384c0bc4f40efc62d..4f9d1d160696f336866748311c1cdbf7642ad835 100644 (file)
@@ -1,14 +1,22 @@
 //===- 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.
 //
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/PostDominators.h"
-#include "llvm/iTerminators.h"
+#include "llvm/Instructions.h"
 #include "llvm/Support/CFG.h"
-#include "Support/DepthFirstIterator.h"
-#include "Support/SetOperations.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/SetOperations.h"
+using namespace llvm;
 
 //===----------------------------------------------------------------------===//
 //  PostDominatorSet Implementation
@@ -32,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);
   }
 
@@ -51,12 +58,12 @@ bool PostDominatorSet::runOnFunction(Function &F) {
   do {
     Changed = false;
 
-    std::set<const BasicBlock*> Visited;
+    std::set<BasicBlock*> Visited;
     DomSetType WorkingSet;
 
     for (unsigned i = 0, e = Roots.size(); i != e; ++i)
-      for (idf_iterator<BasicBlock*> It = idf_begin(Roots[i]),
-             E = idf_end(Roots[i]); It != E; ++It) {
+      for (idf_ext_iterator<BasicBlock*> It = idf_ext_begin(Roots[i], Visited),
+             E = idf_ext_end(Roots[i], Visited); It != E; ++It) {
         BasicBlock *BB = *It;
         succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
         if (SI != SE) {                // Is there SOME successor?
@@ -101,6 +108,43 @@ bool PostDominatorSet::runOnFunction(Function &F) {
 static RegisterAnalysis<ImmediatePostDominators>
 D("postidom", "Immediate Post-Dominators Construction", true);
 
+
+// calcIDoms - Calculate the immediate dominator mapping, given a set of
+// dominators for every basic block.
+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(); 
+       DI != DEnd; ++DI) {
+    BasicBlock *BB = DI->first;
+    const DominatorSet::DomSetType &Dominators = DI->second;
+    unsigned DomSetSize = Dominators.size();
+    if (DomSetSize == 1) continue;  // Root node... IDom = null
+
+    // Loop over all dominators of this node.  This corresponds to looping over
+    // nodes in the dominator chain, looking for a node whose dominator set is
+    // equal to the current nodes, except that the current node does not exist
+    // in it.  This means that it is one level higher in the dom chain than the
+    // current node, and it is our idom!
+    //
+    DominatorSet::DomSetType::const_iterator I = Dominators.begin();
+    DominatorSet::DomSetType::const_iterator End = Dominators.end();
+    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 
+      // 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;
+      }
+    }
+  }
+}
+
 //===----------------------------------------------------------------------===//
 //  PostDominatorTree Implementation
 //===----------------------------------------------------------------------===//
@@ -137,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
@@ -181,7 +224,7 @@ PostDominanceFrontier::calculate(const PostDominatorTree &DT,
   if (BB)
     for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
          SI != SE; ++SI)
-      // Does Node immediately dominate this predeccessor?
+      // Does Node immediately dominate this predecessor?
       if (DT[*SI]->getIDom() != Node)
         S.insert(*SI);
 
@@ -207,3 +250,4 @@ PostDominanceFrontier::calculate(const PostDominatorTree &DT,
 // stub - a dummy function to make linking work ok.
 void PostDominanceFrontier::stub() {
 }
+