Constness changes
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSNode.h
index 403b4fa23ac63a810b70fc7c68cf34d68a5964d8..7d91918dba11d392c83b8b095308f7eb99645720 100644 (file)
@@ -9,6 +9,9 @@
 
 #include "llvm/Analysis/DSSupport.h"
 
+template<typename BaseType>
+class DSNodeIterator;          // Data structure graph traversal iterator
+
 //===----------------------------------------------------------------------===//
 /// DSNode - Data structure node class
 ///
 /// different types represented in this object.
 ///
 class DSNode {
-  /// Referrers - Keep track of all of the node handles that point to this
-  /// DSNode.  These pointers may need to be updated to point to a different
-  /// node if this node gets merged with it.
+  /// NumReferrers - The number of DSNodeHandles pointing to this node... if
+  /// this is a forwarding node, then this is the number of node handles which
+  /// are still forwarding over us.
+  ///
+  unsigned NumReferrers;
+
+  /// ForwardNH - This NodeHandle contain the node (and offset into the node)
+  /// that this node really is.  When nodes get folded together, the node to be
+  /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
+  /// null.
+  DSNodeHandle ForwardNH;
+
+  /// Size - The current size of the node.  This should be equal to the size of
+  /// the current type record.
+  ///
+  unsigned Size;
+
+  /// ParentGraph - The graph this node is currently embedded into.
+  ///
+  DSGraph *ParentGraph;
+
+  /// Ty - Keep track of the current outer most type of this object, in addition
+  /// to whether or not it has been indexed like an array or not.  If the
+  /// isArray bit is set, the node cannot grow.
   ///
-  std::vector<DSNodeHandle*> Referrers;
+  const Type *Ty;                 // The type itself...
 
   /// Links - Contains one entry for every sizeof(void*) bytes in this memory
   /// object.  Note that if the node is not a multiple of size(void*) bytes
@@ -34,18 +58,8 @@ class DSNode {
   ///
   std::vector<GlobalValue*> Globals;
 
-  /// Type - Keep track of the current outer most type of this object, in
-  /// addition to whether or not it has been indexed like an array or not.  If
-  /// the isArray bit is set, the node cannot grow.
-  ///
-  DSTypeRec Ty;
-
-  /// Size - The current size of the node.  This should be equal to the size of
-  /// the current type record.
-  ///
-  unsigned Size;
-
   void operator=(const DSNode &); // DO NOT IMPLEMENT
+  DSNode(const DSNode &);         // DO NOT IMPLEMENT
 public:
   enum NodeTy {
     ShadowNode  = 0,        // Nothing is known about this node...
@@ -54,32 +68,41 @@ public:
     GlobalNode  = 1 << 2,   // This node was allocated by a global var decl
     UnknownNode = 1 << 3,   // This node points to unknown allocated memory 
     Incomplete  = 1 << 4,   // This node may not be complete
+
     Modified    = 1 << 5,   // This node is modified in this context
     Read        = 1 << 6,   // This node is read in this context
+
+    Array       = 1 << 7,   // This node is treated like an array
+    //#ifndef NDEBUG
+    DEAD        = 1 << 8,   // This node is dead and should not be pointed to
+    //#endif
+
+    Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
   };
   
   /// NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
   /// to the nodes in the data structure graph, so it is possible to have nodes
-  /// with a value of 0 for their NodeType.  Scalar and Alloca markers go away
-  /// when function graphs are inlined.
+  /// with a value of 0 for their NodeType.
   ///
-  unsigned char NodeType;
+private:
+  unsigned short NodeType;
+public:
 
-  DSNode(enum NodeTy NT, const Type *T);
-  DSNode(const DSNode &);
+  DSNode(const Type *T, DSGraph *G);
+  DSNode(const DSNode &, DSGraph *G);
 
   ~DSNode() {
-#ifndef NDEBUG
-    dropAllReferences();  // Only needed to satisfy assertion checks...
-    assert(Referrers.empty() && "Referrers to dead node exist!");
-#endif
+    dropAllReferences();
+    assert(hasNoReferrers() && "Referrers to dead node exist!");
   }
 
-  // Iterator for graph interface...
-  typedef DSNodeIterator iterator;
-  typedef DSNodeIterator const_iterator;
-  inline iterator begin() const;   // Defined in DSGraphTraits.h
-  inline iterator end() const;
+  // Iterator for graph interface... Defined in DSGraphTraits.h
+  typedef DSNodeIterator<DSNode> iterator;
+  typedef DSNodeIterator<const DSNode> const_iterator;
+  inline iterator begin();
+  inline iterator end();
+  inline const_iterator begin() const;
+  inline const_iterator end() const;
 
   //===--------------------------------------------------
   // Accessors
@@ -89,21 +112,31 @@ public:
   unsigned getSize() const { return Size; }
 
   // getType - Return the node type of this object...
-  const DSTypeRec &getType() const { return Ty; }
+  const Type *getType() const { return Ty; }
+  bool isArray() const { return NodeType & Array; }
 
-  /// getReferrers - Return a list of the pointers to this node...
+  /// hasNoReferrers - Return true if nothing is pointing to this node at all.
   ///
-  const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
+  bool hasNoReferrers() const { return getNumReferrers() == 0; }
 
-  /// isModified - Return true if this node may be modified in this context
-  ///
-  bool isModified() const { return (NodeType & Modified) != 0; }
+  /// getNumReferrers - This method returns the number of referrers to the
+  /// current node.  Note that if this node is a forwarding node, this will
+  /// return the number of nodes forwarding over the node!
+  unsigned getNumReferrers() const { return NumReferrers; }
 
-  /// isRead - Return true if this node may be read in this context
-  ///
-  bool isRead() const { return (NodeType & Read) != 0; }
+  DSGraph *getParentGraph() const { return ParentGraph; }
+  void setParentGraph(DSGraph *G) { ParentGraph = G; }
 
 
+  /// getForwardNode - This method returns the node that this node is forwarded
+  /// to, if any.
+  DSNode *getForwardNode() const { return ForwardNH.getNode(); }
+  void stopForwarding() {
+    assert(!ForwardNH.isNull() &&
+           "Node isn't forwarding, cannot stopForwarding!");
+    ForwardNH.setNode(0);
+  }
+
   /// hasLink - Return true if this memory object has a link in slot #LinkNo
   ///
   bool hasLink(unsigned Offset) const {
@@ -113,6 +146,8 @@ public:
     assert(Index < Links.size() && "Link index is out of range!");
     return Links[Index].getNode();
   }
+
+  /// getLink - Return the link at the specified offset.
   DSNodeHandle &getLink(unsigned Offset) {
     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
            "Pointer offset not aligned correctly!");
@@ -128,6 +163,10 @@ public:
     return Links[Index];
   }
 
+  /// getNumLinks - Return the number of links in a node...
+  ///
+  unsigned getNumLinks() const { return Links.size(); }
+
   /// mergeTypeInfo - This method merges the specified type into the current
   /// node at the specified offset.  This may update the current node's type
   /// record if this gives more information to the node, it may do nothing to
@@ -138,7 +177,8 @@ public:
   /// This method returns true if the node is completely folded, otherwise
   /// false.
   ///
-  bool mergeTypeInfo(const Type *Ty, unsigned Offset);
+  bool mergeTypeInfo(const Type *Ty, unsigned Offset,
+                     bool FoldIfIncompatible = true);
 
   /// foldNodeCompletely - If we determine that this node has some funny
   /// behavior happening to it that we cannot represent, we fold it down to a
@@ -165,6 +205,10 @@ public:
     Links[Index] = NH;
   }
 
+  /// getPointerSize - Return the size of a pointer for the current target.
+  ///
+  unsigned getPointerSize() const { return DS::PointerSize; }
+
   /// addEdgeTo - Add an edge from the current node to the specified node.  This
   /// can cause merging of nodes in the graph.
   ///
@@ -184,40 +228,110 @@ public:
   ///
   void addGlobal(GlobalValue *GV);
   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
-  std::vector<GlobalValue*> &getGlobals() { return Globals; }
+
+  /// maskNodeTypes - Apply a mask to the node types bitfield.
+  ///
+  void maskNodeTypes(unsigned Mask) {
+    NodeType &= Mask;
+  }
+
+  /// getNodeFlags - Return all of the flags set on the node.  If the DEAD flag
+  /// is set, hide it from the caller.
+  unsigned getNodeFlags() const { return NodeType & ~DEAD; }
+
+  bool isAllocaNode()  const { return NodeType & AllocaNode; }
+  bool isHeapNode()    const { return NodeType & HeapNode; }
+  bool isGlobalNode()  const { return NodeType & GlobalNode; }
+  bool isUnknownNode() const { return NodeType & UnknownNode; }
+
+  bool isModified() const   { return NodeType & Modified; }
+  bool isRead() const       { return NodeType & Read; }
+
+  bool isIncomplete() const { return NodeType & Incomplete; }
+  bool isComplete() const   { return !isIncomplete(); }
+  bool isDeadNode() const   { return NodeType & DEAD; }
+
+  DSNode *setAllocaNodeMarker()  { NodeType |= AllocaNode;  return this; }
+  DSNode *setHeapNodeMarker()    { NodeType |= HeapNode;    return this; }
+  DSNode *setGlobalNodeMarker()  { NodeType |= GlobalNode;  return this; }
+  DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
+
+  DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
+  DSNode *setModifiedMarker()   { NodeType |= Modified;   return this; }
+  DSNode *setReadMarker()       { NodeType |= Read;       return this; }
+
+  void makeNodeDead() {
+    Globals.clear();
+    assert(hasNoReferrers() && "Dead node shouldn't have refs!");
+    NodeType = DEAD;
+  }
+
+  /// forwardNode - Mark this node as being obsolete, and all references to it
+  /// should be forwarded to the specified node and offset.
+  ///
+  void forwardNode(DSNode *To, unsigned Offset);
 
   void print(std::ostream &O, const DSGraph *G) const;
   void dump() const;
 
+  void assertOK() const;
+
   void dropAllReferences() {
     Links.clear();
+    if (!ForwardNH.isNull())
+      ForwardNH.setNode(0);
   }
 
   /// remapLinks - Change all of the Links in the current node according to the
   /// specified mapping.
-  void remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap);
+  void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
+
+  /// markReachableNodes - This method recursively traverses the specified
+  /// DSNodes, marking any nodes which are reachable.  All reachable nodes it
+  /// adds to the set, which allows it to only traverse visited nodes once.
+  ///
+  void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
 
 private:
   friend class DSNodeHandle;
-  // addReferrer - Keep the referrer set up to date...
-  void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
-  void removeReferrer(DSNodeHandle *H);
+
+  // static mergeNodes - Helper for mergeWith()
+  static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
 };
 
 
 //===----------------------------------------------------------------------===//
 // Define inline DSNodeHandle functions that depend on the definition of DSNode
 //
+inline DSNode *DSNodeHandle::getNode() const {
+  assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
+          !N->ForwardNH.isNull()) && "Node handle offset out of range!");
+  if (!N || N->ForwardNH.isNull())
+    return N;
+
+  return HandleForwarding();
+}
 
 inline void DSNodeHandle::setNode(DSNode *n) {
-  if (N) N->removeReferrer(this);
+  assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
+  if (N) N->NumReferrers--;
   N = n;
-  if (N) N->addReferrer(this);
+  if (N) {
+    N->NumReferrers++;
+    if (Offset >= N->Size) {
+      assert((Offset == 0 || N->Size == 1) &&
+             "Pointer to non-collapsed node with invalid offset!");
+      Offset = 0;
+    }
+  }
+  assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
+  assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
+          !N->ForwardNH.isNull()) && "Node handle offset out of range!");
 }
 
 inline bool DSNodeHandle::hasLink(unsigned Num) const {
   assert(N && "DSNodeHandle does not point to a node yet!");
-  return N->hasLink(Num+Offset);
+  return getNode()->hasLink(Num+Offset);
 }
 
 
@@ -226,16 +340,16 @@ inline bool DSNodeHandle::hasLink(unsigned Num) const {
 ///
 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
   assert(N && "DSNodeHandle does not point to a node yet!");
-  return N->getLink(Offset+Off);
+  return getNode()->getLink(Offset+Off);
 }
 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
   assert(N && "DSNodeHandle does not point to a node yet!");
-  return N->getLink(Off+Offset);
+  return getNode()->getLink(Off+Offset);
 }
 
 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
   assert(N && "DSNodeHandle does not point to a node yet!");
-  N->setLink(Off+Offset, NH);
+  getNode()->setLink(Off+Offset, NH);
 }
 
 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
@@ -243,7 +357,7 @@ inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
 ///
 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
   assert(N && "DSNodeHandle does not point to a node yet!");
-  N->addEdgeTo(Off+Offset, Node);
+  getNode()->addEdgeTo(Off+Offset, Node);
 }
 
 /// mergeWith - Merge the logical node pointed to by 'this' with the node
@@ -251,7 +365,7 @@ inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
 ///
 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
   if (N != 0)
-    N->mergeWith(Node, Offset);
+    getNode()->mergeWith(Node, Offset);
   else     // No node to merge with, so just point to Node
     *this = Node;
 }