Fix more -Wshorten-64-to-32 warnings.
[oota-llvm.git] / include / llvm / Analysis / Dominators.h
index f1e374a20cc33ef50eb7d081478efe930a5d8773..6ce3260b8f5294bd000afd6f94aab0d7fdee128d 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     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 is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -34,6 +34,7 @@
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Compiler.h"
 #include <algorithm>
+#include <map>
 #include <set>
 
 namespace llvm {
@@ -43,12 +44,12 @@ namespace llvm {
 /// inherit from.
 ///
 template <class NodeT>
-class DominatorBase : public FunctionPass {
+class DominatorBase {
 protected:
   std::vector<NodeT*> Roots;
   const bool IsPostDominators;
-  inline DominatorBase(intptr_t ID, bool isPostDom) : 
-    FunctionPass(ID), Roots(), IsPostDominators(isPostDom) {}
+  inline explicit DominatorBase(bool isPostDom) :
+    Roots(), IsPostDominators(isPostDom) {}
 public:
 
   /// getRoots -  Return the root blocks of the current CFG.  This may include
@@ -102,10 +103,14 @@ public:
     return C;
   }
   
+  size_t getNumChildren() const {
+    return Children.size();
+  }
+  
   void setIDom(DomTreeNodeBase<NodeT> *NewIDom) {
     assert(IDom && "No immediate dominator?");
     if (IDom != NewIDom) {
-      std::vector<DomTreeNodeBase<BasicBlock>*>::iterator I =
+      typename std::vector<DomTreeNodeBase<NodeT>*>::iterator I =
                   std::find(IDom->Children.begin(), IDom->Children.end(), this);
       assert(I != IDom->Children.end() &&
              "Not in immediate dominator children set!");
@@ -132,6 +137,7 @@ private:
 };
 
 EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
+EXTERN_TEMPLATE_INSTANTIATION(class DomTreeNodeBase<MachineBasicBlock>);
 
 template<class NodeT>
 static std::ostream &operator<<(std::ostream &o,
@@ -156,15 +162,14 @@ static void PrintDomTree(const DomTreeNodeBase<NodeT> *N, std::ostream &o,
 }
 
 typedef DomTreeNodeBase<BasicBlock> DomTreeNode;
-typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
 
 //===----------------------------------------------------------------------===//
 /// DominatorTree - Calculate the immediate dominator tree for a function.
 ///
 
-template<class N, class GraphT>
-void Calculate(DominatorTreeBase<typename GraphT::NodeType>& DT,
-               Function& F);
+template<class FuncT, class N>
+void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
+               FuncT& F);
 
 template<class NodeT>
 class DominatorTreeBase : public DominatorBase<NodeT> {
@@ -177,13 +182,16 @@ protected:
   unsigned int SlowQueries;
   // Information record used during immediate dominators computation.
   struct InfoRec {
+    unsigned DFSNum;
     unsigned Semi;
     unsigned Size;
-    NodeT *Label, *Parent, *Child, *Ancestor;
+    NodeT *Label, *Child;
+    unsigned Parent, Ancestor;
 
     std::vector<NodeT*> Bucket;
 
-    InfoRec() : Semi(0), Size(0), Label(0), Parent(0), Child(0), Ancestor(0) {}
+    InfoRec() : DFSNum(0), Semi(0), Size(0), Label(0), Child(0), Parent(0),
+                Ancestor(0) {}
   };
 
   DenseMap<NodeT*, NodeT*> IDoms;
@@ -229,7 +237,7 @@ protected:
       bool NewBBDominatesNewBBSucc = true;
       {
         typename GraphT::NodeType* OnePred = PredBlocks[0];
-        unsigned i = 1, e = PredBlocks.size();
+        size_t i = 1, e = PredBlocks.size();
         for (i = 1; !DT.isReachableFromEntry(OnePred); ++i) {
           assert(i != e && "Didn't find reachable pred?");
           OnePred = PredBlocks[i];
@@ -267,7 +275,7 @@ protected:
 
     // Find NewBB's immediate dominator and create new dominator tree node for
     // NewBB.
-    BasicBlock *NewBBIDom = 0;
+    NodeT *NewBBIDom = 0;
     unsigned i = 0;
     for (i = 0; i < PredBlocks.size(); ++i)
       if (DT.isReachableFromEntry(PredBlocks[i])) {
@@ -282,20 +290,20 @@ protected:
     assert(NewBBIDom && "No immediate dominator found??");
 
     // Create the new dominator tree node... and set the idom of NewBB.
-    DomTreeNode *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
+    DomTreeNodeBase<NodeT> *NewBBNode = DT.addNewBlock(NewBB, NewBBIDom);
 
     // If NewBB strictly dominates other blocks, then it is now the immediate
     // dominator of NewBBSucc.  Update the dominator tree as appropriate.
     if (NewBBDominatesNewBBSucc) {
-      DomTreeNode *NewBBSuccNode = DT.getNode(NewBBSucc);
+      DomTreeNodeBase<NodeT> *NewBBSuccNode = DT.getNode(NewBBSucc);
       DT.changeImmediateDominator(NewBBSuccNode, NewBBNode);
     }
   }
 
 public:
-  DominatorTreeBase(intptr_t ID, bool isPostDom) 
-    : DominatorBase<NodeT>(ID, isPostDom), DFSInfoValid(false), SlowQueries(0) {}
-  ~DominatorTreeBase() { reset(); }
+  explicit DominatorTreeBase(bool isPostDom)
+    : DominatorBase<NodeT>(isPostDom), DFSInfoValid(false), SlowQueries(0) {}
+  virtual ~DominatorTreeBase() { reset(); }
 
   // FIXME: Should remove this
   virtual bool runOnFunction(Function &F) { return false; }
@@ -345,10 +353,10 @@ public:
 
   /// isReachableFromEntry - Return true if A is dominated by the entry
   /// block of the function containing it.
-  const bool isReachableFromEntry(NodeT* A) {
+  bool isReachableFromEntry(NodeT* A) {
     assert (!this->isPostDominator() 
             && "This is not implemented for post dominators");
-    return dominates(&A->getParent()->getEntryBlock(), A);
+    return dominates(&A->getParent()->front(), A);
   }
   
   /// dominates - Returns true iff A dominates B.  Note that this is not a
@@ -398,7 +406,7 @@ public:
             && "Two blocks are not in same function");
 
     // If either A or B is a entry block then it is nearest common dominator.
-    NodeT &Entry  = A->getParent()->getEntryBlock();
+    NodeT &Entry  = A->getParent()->front();
     if (A == &Entry || B == &Entry)
       return &Entry;
 
@@ -447,7 +455,7 @@ public:
     assert(IDomNode && "Not immediate dominator specified for block!");
     DFSInfoValid = false;
     return DomTreeNodes[BB] = 
-      IDomNode->addChild(new DomTreeNode(BB, IDomNode));
+      IDomNode->addChild(new DomTreeNodeBase<NodeT>(BB, IDomNode));
   }
 
   /// changeImmediateDominator - This method is used to update the dominator
@@ -508,7 +516,10 @@ public:
   ///
   virtual void print(std::ostream &o, const Module* ) const {
     o << "=============================--------------------------------\n";
-    o << "Inorder Dominator Tree: ";
+    if (this->isPostDominator())
+      o << "Inorder PostDominator Tree: ";
+    else
+      o << "Inorder Dominator Tree: ";
     if (this->DFSInfoValid)
       o << "DFSNumbers invalid: " << SlowQueries << " slow queries.";
     o << "\n";
@@ -536,8 +547,7 @@ protected:
 
   template<class GraphT>
   friend void Link(DominatorTreeBase<typename GraphT::NodeType>& DT,
-                   typename GraphT::NodeType* V,
-                   typename GraphT::NodeType* W,
+                   unsigned DFSNumV, typename GraphT::NodeType* W,
          typename DominatorTreeBase<typename GraphT::NodeType>::InfoRec &WInfo);
   
   template<class GraphT>
@@ -545,9 +555,9 @@ protected:
                           typename GraphT::NodeType* V,
                           unsigned N);
   
-  template<class N, class GraphT>
-  friend void Calculate(DominatorTreeBase<typename GraphT::NodeType>& DT,
-                        Function& F);
+  template<class FuncT, class N>
+  friend void Calculate(DominatorTreeBase<typename GraphTraits<N>::NodeType>& DT,
+                        FuncT& F);
   
   /// updateDFSNumbers - Assign In and Out numbers to the nodes while walking
   /// dominator tree in dfs order.
@@ -557,7 +567,7 @@ protected:
     SmallVector<std::pair<DomTreeNodeBase<NodeT>*,
                 typename DomTreeNodeBase<NodeT>::iterator>, 32> WorkStack;
 
-    for (unsigned i = 0, e = this->Roots.size(); i != e; ++i) {
+    for (unsigned i = 0, e = (unsigned)this->Roots.size(); i != e; ++i) {
       DomTreeNodeBase<NodeT> *ThisRoot = getNode(this->Roots[i]);
       WorkStack.push_back(std::make_pair(ThisRoot, ThisRoot->begin()));
       ThisRoot->DFSNumIn = DFSNum++;
@@ -594,6 +604,8 @@ protected:
     // Haven't calculated this node yet?  Get or calculate the node for the
     // immediate dominator.
     NodeT *IDom = getIDom(BB);
+
+    assert(IDom || this->DomTreeNodes[NULL]);
     DomTreeNodeBase<NodeT> *IDomNode = getNodeForBlock(IDom);
 
     // Add a new tree node for this BasicBlock, and link it as a child of
@@ -607,32 +619,34 @@ protected:
     return I != IDoms.end() ? I->second : 0;
   }
   
+  inline void addRoot(NodeT* BB) {
+    this->Roots.push_back(BB);
+  }
+  
 public:
   /// recalculate - compute a dominator tree for the given function
-  void recalculate(Function& F) {
+  template<class FT>
+  void recalculate(FT& F) {
     if (!this->IsPostDominators) {
       reset();
       
       // Initialize roots
-      this->Roots.push_back(&F.getEntryBlock());
-      this->IDoms[&F.getEntryBlock()] = 0;
-      this->DomTreeNodes[&F.getEntryBlock()] = 0;
+      this->Roots.push_back(&F.front());
+      this->IDoms[&F.front()] = 0;
+      this->DomTreeNodes[&F.front()] = 0;
       this->Vertex.push_back(0);
       
-      Calculate<NodeT*, GraphTraits<NodeT*> >(*this, F);
+      Calculate<FT, NodeT*>(*this, F);
       
       updateDFSNumbers();
     } else {
       reset();     // Reset from the last time we were run...
 
       // Initialize the roots list
-      for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
-        TerminatorInst *Insn = I->getTerminator();
-        if (Insn->getNumSuccessors() == 0) {
-          // Unreachable block is not a root node.
-          if (!isa<UnreachableInst>(Insn))
-            this->Roots.push_back(I);
-        }
+      for (typename FT::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+        if (std::distance(GraphTraits<FT*>::child_begin(I),
+                          GraphTraits<FT*>::child_end(I)) == 0)
+          addRoot(I);
 
         // Prepopulate maps so that we don't get iterator invalidation issues later.
         this->IDoms[I] = 0;
@@ -641,7 +655,7 @@ public:
 
       this->Vertex.push_back(0);
       
-      Calculate<Inverse<NodeT*>, GraphTraits<Inverse<NodeT*> > >(*this, F);
+      Calculate<FT, Inverse<NodeT*> >(*this, F);
     }
   }
 };
@@ -658,7 +672,7 @@ public:
   DominatorTreeBase<BasicBlock>* DT;
   
   DominatorTree() : FunctionPass(intptr_t(&ID)) {
-    DT = new DominatorTreeBase<BasicBlock>(intptr_t(&ID), false);
+    DT = new DominatorTreeBase<BasicBlock>(false);
   }
   
   ~DominatorTree() {
@@ -666,6 +680,8 @@ public:
     delete DT;
   }
   
+  DominatorTreeBase<BasicBlock>& getBase() { return *DT; }
+  
   /// getRoots -  Return the root blocks of the current CFG.  This may include
   /// multiple blocks if we are computing post dominators.  For forward
   /// dominators, this will always be a single block (the entry node).
@@ -817,15 +833,28 @@ template <> struct GraphTraits<DominatorTree*>
 /// DominanceFrontierBase - Common base class for computing forward and inverse
 /// dominance frontiers for a function.
 ///
-class DominanceFrontierBase : public DominatorBase<BasicBlock> {
+class DominanceFrontierBase : public FunctionPass {
 public:
   typedef std::set<BasicBlock*>             DomSetType;    // Dom set for a bb
   typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
 protected:
   DomSetMapType Frontiers;
+    std::vector<BasicBlock*> Roots;
+    const bool IsPostDominators;
+  
 public:
   DominanceFrontierBase(intptr_t ID, bool isPostDom) 
-    : DominatorBase<BasicBlock>(ID, isPostDom) {}
+    : FunctionPass(ID), IsPostDominators(isPostDom) {}
+
+  /// getRoots -  Return the root blocks of the current CFG.  This may include
+  /// multiple blocks if we are computing post dominators.  For forward
+  /// dominators, this will always be a single block (the entry node).
+  ///
+  inline const std::vector<BasicBlock*> &getRoots() const { return Roots; }
+  
+  /// isPostDominator - Returns true if analysis based of postdoms
+  ///
+  bool isPostDominator() const { return IsPostDominators; }
 
   virtual void releaseMemory() { Frontiers.clear(); }