Support for llvm_ostreams.
[oota-llvm.git] / include / llvm / Analysis / Dominators.h
index 9dcabcfd477a227dadebdcdbc2b3f4be2a246796..939300f8aca1598ec1970940fde619a31631b02d 100644 (file)
@@ -101,7 +101,17 @@ public:
   inline BasicBlock *operator[](BasicBlock *BB) const {
     return get(BB);
   }
+  
+  /// dominates - Return true if A dominates B.
+  ///
+  bool dominates(BasicBlock *A, BasicBlock *B) const;
 
+  /// properlyDominates - Return true if A dominates B and A != B.
+  ///
+  bool properlyDominates(BasicBlock *A, BasicBlock *B) const {
+    return A != B || properlyDominates(A, B);
+  }
+  
   /// get() - Synonym for operator[].
   ///
   inline BasicBlock *get(BasicBlock *BB) const {
@@ -140,7 +150,8 @@ public:
 /// ImmediateDominators Class - Concrete subclass of ImmediateDominatorsBase
 /// that is used to compute a normal immediate dominator set.
 ///
-struct ImmediateDominators : public ImmediateDominatorsBase {
+class ImmediateDominators : public ImmediateDominatorsBase {
+public:
   ImmediateDominators() : ImmediateDominatorsBase(false) {}
 
   BasicBlock *getRoot() const {
@@ -169,7 +180,8 @@ private:
 /// is unreachable in this function, the set will be empty.  This cannot happen
 /// for reachable code, because every block dominates at least itself.
 ///
-struct DominatorSetBase : public DominatorBase {
+class DominatorSetBase : public DominatorBase {
+public:
   typedef std::set<BasicBlock*> DomSetType;    // Dom set for a bb
   // Map of dom sets
   typedef std::map<BasicBlock*, DomSetType> DomSetMapType;
@@ -255,7 +267,8 @@ public:
 /// DominatorSet Class - Concrete subclass of DominatorSetBase that is used to
 /// compute a normal dominator set.
 ///
-struct DominatorSet : public DominatorSetBase {
+class DominatorSet : public DominatorSetBase {
+public:
   DominatorSet() : DominatorSetBase(false) {}
 
   virtual bool runOnFunction(Function &F);
@@ -273,14 +286,15 @@ struct DominatorSet : public DominatorSetBase {
   }
 
   // stub - dummy function, just ignore it
-  static void stub();
+  static int stub;
 };
 
 
 //===----------------------------------------------------------------------===//
 /// DominatorTree - Calculate the immediate dominator tree for a function.
 ///
-struct DominatorTreeBase : public DominatorBase {
+class DominatorTreeBase : public DominatorBase {
+public:
   class Node;
 protected:
   std::map<BasicBlock*, Node*> Nodes;
@@ -290,9 +304,9 @@ protected:
   Node *RootNode;
 public:
   class Node {
-    friend struct DominatorTree;
+    friend class DominatorTree;
     friend struct PostDominatorTree;
-    friend struct DominatorTreeBase;
+    friend class DominatorTreeBase;
     BasicBlock *TheBB;
     Node *IDom;
     std::vector<Node*> Children;
@@ -385,17 +399,82 @@ public:
     N->setIDom(NewIDom);
   }
 
+  /// removeNode - Removes a node from the dominator tree.  Block must not
+  /// dominate any other blocks.  Invalidates any node pointing to removed
+  /// block.
+  void removeNode(BasicBlock *BB) {
+    assert(getNode(BB) && "Removing node that isn't in dominator tree.");
+    Nodes.erase(BB);
+  }
+
   /// print - Convert to human readable form
   ///
   virtual void print(std::ostream &OS, const Module* = 0) const;
 };
 
+//===-------------------------------------
+/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
+/// compute a normal dominator tree.
+///
+class DominatorTree : public DominatorTreeBase {
+public:
+  DominatorTree() : DominatorTreeBase(false) {}
+  
+  BasicBlock *getRoot() const {
+    assert(Roots.size() == 1 && "Should always have entry node!");
+    return Roots[0];
+  }
+  
+  virtual bool runOnFunction(Function &F) {
+    reset();     // Reset from the last time we were run...
+    ImmediateDominators &ID = getAnalysis<ImmediateDominators>();
+    Roots = ID.getRoots();
+    calculate(ID);
+    return false;
+  }
+  
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+    AU.addRequired<ImmediateDominators>();
+  }
+private:
+  void calculate(const ImmediateDominators &ID);
+  Node *getNodeForBlock(BasicBlock *BB);
+};
+
+//===-------------------------------------
+/// DominatorTree GraphTraits specialization so the DominatorTree can be
+/// iterable by generic graph iterators.
+///
+template <> struct GraphTraits<DominatorTree::Node*> {
+  typedef DominatorTree::Node NodeType;
+  typedef NodeType::iterator  ChildIteratorType;
+  
+  static NodeType *getEntryNode(NodeType *N) {
+    return N;
+  }
+  static inline ChildIteratorType child_begin(NodeType* N) {
+    return N->begin();
+  }
+  static inline ChildIteratorType child_end(NodeType* N) {
+    return N->end();
+  }
+};
+
+template <> struct GraphTraits<DominatorTree*>
+  : public GraphTraits<DominatorTree::Node*> {
+  static NodeType *getEntryNode(DominatorTree *DT) {
+    return DT->getRootNode();
+  }
+};
+
 
 //===-------------------------------------
 /// ET-Forest Class - Class used to construct forwards and backwards 
 /// ET-Forests
 ///
-struct ETForestBase : public DominatorBase {
+class ETForestBase : public DominatorBase {
+public:
   ETForestBase(bool isPostDom) : DominatorBase(isPostDom), Nodes(), 
                                  DFSInfoValid(false), SlowQueries(0) {}
   
@@ -491,7 +570,8 @@ protected:
 /// ETForest Class - Concrete subclass of ETForestBase that is used to
 /// compute a forwards ET-Forest.
 
-struct ETForest : public ETForestBase {
+class ETForest : public ETForestBase {
+public:
   ETForest() : ETForestBase(false) {}
 
   BasicBlock *getRoot() const {
@@ -511,65 +591,12 @@ struct ETForest : public ETForestBase {
   ETNode *getNodeForBlock(BasicBlock *BB);
 };
 
-//===-------------------------------------
-/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
-/// compute a normal dominator tree.
-///
-struct DominatorTree : public DominatorTreeBase {
-  DominatorTree() : DominatorTreeBase(false) {}
-
-  BasicBlock *getRoot() const {
-    assert(Roots.size() == 1 && "Should always have entry node!");
-    return Roots[0];
-  }
-
-  virtual bool runOnFunction(Function &F) {
-    reset();     // Reset from the last time we were run...
-    ImmediateDominators &ID = getAnalysis<ImmediateDominators>();
-    Roots = ID.getRoots();
-    calculate(ID);
-    return false;
-  }
-
-  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.setPreservesAll();
-    AU.addRequired<ImmediateDominators>();
-  }
-private:
-  void calculate(const ImmediateDominators &ID);
-  Node *getNodeForBlock(BasicBlock *BB);
-};
-
-//===-------------------------------------
-/// DominatorTree GraphTraits specialization so the DominatorTree can be
-/// iterable by generic graph iterators.
-///
-template <> struct GraphTraits<DominatorTree::Node*> {
-  typedef DominatorTree::Node NodeType;
-  typedef NodeType::iterator  ChildIteratorType;
-
-  static NodeType *getEntryNode(NodeType *N) {
-    return N;
-  }
-  static inline ChildIteratorType child_begin(NodeType* N) {
-    return N->begin();
-  }
-  static inline ChildIteratorType child_end(NodeType* N) {
-    return N->end();
-  }
-};
-
-template <> struct GraphTraits<DominatorTree*>
-  : public GraphTraits<DominatorTree::Node*> {
-  static NodeType *getEntryNode(DominatorTree *DT) {
-    return DT->getRootNode();
-  }
-};
-
 //===----------------------------------------------------------------------===//
-/// DominanceFrontier - Calculate the dominance frontiers for a function.
+/// DominanceFrontierBase - Common base class for computing forward and inverse
+/// dominance frontiers for a function.
 ///
-struct DominanceFrontierBase : public DominatorBase {
+class DominanceFrontierBase : public DominatorBase {
+public:
   typedef std::set<BasicBlock*>             DomSetType;    // Dom set for a bb
   typedef std::map<BasicBlock*, DomSetType> DomSetMapType; // Dom set map
 protected:
@@ -612,10 +639,11 @@ public:
 
 
 //===-------------------------------------
-/// DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to
-/// compute a normal dominator tree.
+/// DominanceFrontier Class - Concrete subclass of DominanceFrontierBase that is
+/// used to compute a forward dominator frontiers.
 ///
-struct DominanceFrontier : public DominanceFrontierBase {
+class DominanceFrontier : public DominanceFrontierBase {
+public:
   DominanceFrontier() : DominanceFrontierBase(false) {}
 
   BasicBlock *getRoot() const {
@@ -642,9 +670,9 @@ private:
 };
 
 
-// Make sure that any clients of this file link in Dominators.cpp
-static IncludeFile
-DOMINATORS_INCLUDE_FILE((void*)&DominatorSet::stub);
 } // End llvm namespace
 
+// Make sure that any clients of this file link in Dominators.cpp
+FORCE_DEFINING_FILE_TO_BE_LINKED(DominatorSet)
+
 #endif