Make the LLVM headers "-ansi -pedantic -Wno-long-long" clean.
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSNode.h
index 7d91918dba11d392c83b8b095308f7eb99645720..83c9aba5d85d92d44ff7329c8a90290551392641 100644 (file)
@@ -1,5 +1,12 @@
 //===- DSNode.h - Node definition for datastructure graphs ------*- C++ -*-===//
 //
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+//
 // Data structure graph nodes and some implementation of DSNodeHandle.
 //
 //===----------------------------------------------------------------------===//
 #ifndef LLVM_ANALYSIS_DSNODE_H
 #define LLVM_ANALYSIS_DSNODE_H
 
-#include "llvm/Analysis/DSSupport.h"
+#include "llvm/Analysis/DataStructure/DSSupport.h"
+#include "llvm/ADT/hash_map"
+
+namespace llvm {
 
 template<typename BaseType>
 class DSNodeIterator;          // Data structure graph traversal iterator
+class TargetData;
 
 //===----------------------------------------------------------------------===//
 /// DSNode - Data structure node class
@@ -30,8 +41,15 @@ class DSNode {
   /// 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;
 
+  /// Next, Prev - These instance variables are used to keep the node on a
+  /// doubly-linked ilist in the DSGraph.
+  ///
+  DSNode *Next, *Prev;
+  friend struct ilist_traits<DSNode>;
+
   /// Size - The current size of the node.  This should be equal to the size of
   /// the current type record.
   ///
@@ -66,7 +84,7 @@ public:
     AllocaNode  = 1 << 0,   // This node was allocated with alloca
     HeapNode    = 1 << 1,   // This node was allocated with malloc
     GlobalNode  = 1 << 2,   // This node was allocated by a global var decl
-    UnknownNode = 1 << 3,   // This node points to unknown allocated memory 
+    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
@@ -77,9 +95,9 @@ public:
     DEAD        = 1 << 8,   // This node is dead and should not be pointed to
     //#endif
 
-    Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
+    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.
@@ -88,8 +106,17 @@ private:
   unsigned short NodeType;
 public:
 
+  /// DSNode ctor - Create a node of the specified type, inserting it into the
+  /// specified graph.
+  ///
   DSNode(const Type *T, DSGraph *G);
-  DSNode(const DSNode &, DSGraph *G);
+
+  /// DSNode "copy ctor" - Copy the specified node, inserting it into the
+  /// specified graph.  If NullLinks is true, then null out all of the links,
+  /// but keep the same number of them.  This can be used for efficiency if the
+  /// links are just going to be clobbered anyway.
+  ///
+  DSNode(const DSNode &, DSGraph *G, bool NullLinks = false);
 
   ~DSNode() {
     dropAllReferences();
@@ -111,8 +138,10 @@ public:
   ///
   unsigned getSize() const { return Size; }
 
-  // getType - Return the node type of this object...
+  /// getType - Return the node type of this object...
+  ///
   const Type *getType() const { return Ty; }
+
   bool isArray() const { return NodeType & Array; }
 
   /// hasNoReferrers - Return true if nothing is pointing to this node at all.
@@ -128,13 +157,29 @@ public:
   void setParentGraph(DSGraph *G) { ParentGraph = G; }
 
 
+  /// getTargetData - Get the target data object used to construct this node.
+  ///
+  const TargetData &getTargetData() const;
+
   /// getForwardNode - This method returns the node that this node is forwarded
   /// to, if any.
+  ///
   DSNode *getForwardNode() const { return ForwardNH.getNode(); }
+
+  /// isForwarding - Return true if this node is forwarding to another.
+  ///
+  bool isForwarding() const { return !ForwardNH.isNull(); }
+
+  /// stopForwarding - When the last reference to this forwarding node has been
+  /// dropped, delete the node.
+  ///
   void stopForwarding() {
-    assert(!ForwardNH.isNull() &&
-           "Node isn't forwarding, cannot stopForwarding!");
-    ForwardNH.setNode(0);
+    assert(isForwarding() &&
+           "Node isn't forwarding, cannot stopForwarding()!");
+    ForwardNH.setTo(0, 0);
+    assert(ParentGraph == 0 &&
+           "Forwarding nodes must have been removed from graph!");
+    delete this;
   }
 
   /// hasLink - Return true if this memory object has a link in slot #LinkNo
@@ -148,6 +193,7 @@ public:
   }
 
   /// getLink - Return the link at the specified offset.
+  ///
   DSNodeHandle &getLink(unsigned Offset) {
     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
            "Pointer offset not aligned correctly!");
@@ -167,6 +213,16 @@ public:
   ///
   unsigned getNumLinks() const { return Links.size(); }
 
+  /// edge_* - Provide iterators for accessing outgoing edges.  Some outgoing
+  /// edges may be null.
+  typedef std::vector<DSNodeHandle>::iterator edge_iterator;
+  typedef std::vector<DSNodeHandle>::const_iterator const_edge_iterator;
+  edge_iterator edge_begin() { return Links.begin(); }
+  edge_iterator edge_end() { return Links.end(); }
+  const_edge_iterator edge_begin() const { return Links.begin(); }
+  const_edge_iterator edge_end() const { return Links.end(); }
+
+
   /// 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
@@ -227,7 +283,36 @@ public:
   /// also marks the node with the 'G' flag if it does not already have it.
   ///
   void addGlobal(GlobalValue *GV);
-  const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
+
+  /// removeGlobal - Remove the specified global that is explicitly in the
+  /// globals list.
+  void removeGlobal(GlobalValue *GV);
+
+  void mergeGlobals(const std::vector<GlobalValue*> &RHS);
+  void clearGlobals() { std::vector<GlobalValue*>().swap(Globals); }
+
+  /// getGlobalsList - Return the set of global leaders that are represented by
+  /// this node.  Note that globals that are in this equivalence class but are
+  /// not leaders are not returned: for that, use addFullGlobalsList().
+  const std::vector<GlobalValue*> &getGlobalsList() const { return Globals; }
+
+  /// addFullGlobalsList - Compute the full set of global values that are
+  /// represented by this node.  Unlike getGlobalsList(), this requires fair
+  /// amount of work to compute, so don't treat this method call as free.
+  void addFullGlobalsList(std::vector<GlobalValue*> &List) const;
+
+  /// addFullFunctionList - Identical to addFullGlobalsList, but only return the
+  /// functions in the full list.
+  void addFullFunctionList(std::vector<Function*> &List) const;
+
+  /// globals_iterator/begin/end - Provide iteration methods over the global
+  /// value leaders set that is merged into this node.  Like the getGlobalsList
+  /// method, these iterators do not return globals that are part of the
+  /// equivalence classes for globals in this node, but aren't leaders.
+  typedef std::vector<GlobalValue*>::const_iterator globals_iterator;
+  globals_iterator globals_begin() const { return Globals.begin(); }
+  globals_iterator globals_end() const { return Globals.end(); }
+
 
   /// maskNodeTypes - Apply a mask to the node types bitfield.
   ///
@@ -235,8 +320,13 @@ public:
     NodeType &= Mask;
   }
 
+  void mergeNodeFlags(unsigned RHS) {
+    NodeType |= RHS;
+  }
+
   /// 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; }
@@ -259,6 +349,7 @@ public:
   DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
   DSNode *setModifiedMarker()   { NodeType |= Modified;   return this; }
   DSNode *setReadMarker()       { NodeType |= Read;       return this; }
+  DSNode *setArrayMarker()      { NodeType |= Array; return this; }
 
   void makeNodeDead() {
     Globals.clear();
@@ -278,19 +369,20 @@ public:
 
   void dropAllReferences() {
     Links.clear();
-    if (!ForwardNH.isNull())
-      ForwardNH.setNode(0);
+    if (isForwarding())
+      ForwardNH.setTo(0, 0);
   }
 
   /// remapLinks - Change all of the Links in the current node according to the
   /// specified mapping.
+  ///
   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);
+  void markReachableNodes(hash_set<const DSNode*> &ReachableNodes) const;
 
 private:
   friend class DSNodeHandle;
@@ -299,23 +391,60 @@ private:
   static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
 };
 
+//===----------------------------------------------------------------------===//
+// Define the ilist_traits specialization for the DSGraph ilist.
+//
+template<>
+struct ilist_traits<DSNode> {
+  static DSNode *getPrev(const DSNode *N) { return N->Prev; }
+  static DSNode *getNext(const DSNode *N) { return N->Next; }
+
+  static void setPrev(DSNode *N, DSNode *Prev) { N->Prev = Prev; }
+  static void setNext(DSNode *N, DSNode *Next) { N->Next = Next; }
+
+  static DSNode *createSentinel() { return new DSNode(0,0); }
+  static void destroySentinel(DSNode *N) { delete N; }
+  //static DSNode *createNode(const DSNode &V) { return new DSNode(V); }
+
+
+  void addNodeToList(DSNode *NTy) {}
+  void removeNodeFromList(DSNode *NTy) {}
+  void transferNodesFromList(iplist<DSNode, ilist_traits> &L2,
+                             ilist_iterator<DSNode> first,
+                             ilist_iterator<DSNode> last) {}
+};
+
+template<>
+struct ilist_traits<const DSNode> : public ilist_traits<DSNode> {};
 
 //===----------------------------------------------------------------------===//
 // 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())
+  // Disabling this assertion because it is failing on a "magic" struct
+  // in named (from bind).  The fourth field is an array of length 0,
+  // presumably used to create struct instances of different sizes.
+  // In a variable length struct, Offset could exceed Size when getNode()
+  // is called before such a node is folded. In this case, the DS Analysis now 
+  // correctly folds this node after calling getNode.
+  /*  assert((!N ||
+          N->isNodeCompletelyFolded() ||
+          (N->Size == 0 && Offset == 0) ||
+          (int(Offset) >= 0 && Offset < N->Size) ||
+          (int(Offset) < 0 && -int(Offset) < int(N->Size)) ||
+          N->isForwarding()) && "Node handle offset out of range!");
+  */
+  if (N == 0 || !N->isForwarding())
     return N;
 
   return HandleForwarding();
 }
 
-inline void DSNodeHandle::setNode(DSNode *n) {
-  assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
-  if (N) N->NumReferrers--;
+inline void DSNodeHandle::setTo(DSNode *n, unsigned NewOffset) const {
+  assert(!n || !n->isForwarding() && "Cannot set node to a forwarded node!");
+  if (N) getNode()->NumReferrers--;
   N = n;
+  Offset = NewOffset;
   if (N) {
     N->NumReferrers++;
     if (Offset >= N->Size) {
@@ -326,7 +455,7 @@ inline void DSNodeHandle::setNode(DSNode *n) {
   }
   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!");
+          N->isForwarding()) && "Node handle offset out of range!");
 }
 
 inline bool DSNodeHandle::hasLink(unsigned Num) const {
@@ -352,7 +481,7 @@ inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
   getNode()->setLink(Off+Offset, NH);
 }
 
-///  addEdgeTo - Add an edge from the current node to the specified node.  This
+/// addEdgeTo - Add an edge from the current node to the specified node.  This
 /// can cause merging of nodes in the graph.
 ///
 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
@@ -363,11 +492,16 @@ inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
 /// mergeWith - Merge the logical node pointed to by 'this' with the node
 /// pointed to by 'N'.
 ///
-inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
-  if (N != 0)
+inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) const {
+  if (!isNull())
     getNode()->mergeWith(Node, Offset);
-  else     // No node to merge with, so just point to Node
-    *this = Node;
+  else {   // No node to merge with, so just point to Node
+    Offset = 0;
+    DSNode *NN = Node.getNode();
+    setTo(NN, Node.getOffset());
+  }
 }
 
+} // End llvm namespace
+
 #endif