//===- 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.
//
#include "llvm/Analysis/DSSupport.h"
+namespace llvm {
+
+template<typename BaseType>
+class DSNodeIterator; // Data structure graph traversal iterator
+class TargetData;
+
//===----------------------------------------------------------------------===//
/// DSNode - Data structure node class
///
/// different types represented in this object.
///
class DSNode {
- /// Links - Contains one entry for every _distinct_ pointer field in the
- /// memory block. These are demand allocated and indexed by the MergeMap
- /// vector.
+ /// 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.
///
- std::vector<DSNodeHandle> Links;
+ unsigned NumReferrers;
- /// MergeMap - Maps from every byte in the object to a signed byte number.
- /// This map is neccesary due to the merging that is possible as part of the
- /// unification algorithm. To merge two distinct bytes of the object together
- /// into a single logical byte, the indexes for the two bytes are set to the
- /// same value. This fully general merging is capable of representing all
- /// manners of array merging if neccesary.
+ /// 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.
///
- /// This map is also used to map outgoing pointers to various byte offsets in
- /// this data structure node. If this value is >= 0, then it indicates that
- /// the numbered entry in the Links vector contains the outgoing edge for this
- /// byte offset. In this way, the Links vector can be demand allocated and
- /// byte elements of the node may be merged without needing a Link allocated
- /// for it.
+ DSNodeHandle ForwardNH;
+
+ /// Next, Prev - These instance variables are used to keep the node on a
+ /// doubly-linked ilist in the DSGraph.
///
- /// Initially, each each element of the MergeMap is assigned a unique negative
- /// number, which are then merged as the unification occurs.
+ DSNode *Next, *Prev;
+ friend class ilist_traits<DSNode>;
+
+ /// Size - The current size of the node. This should be equal to the size of
+ /// the current type record.
///
- std::vector<signed char> MergeMap;
+ unsigned Size;
- /// 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.
+ /// ParentGraph - The graph this node is currently embedded into.
///
- std::vector<DSNodeHandle*> Referrers;
+ DSGraph *ParentGraph;
- /// TypeEntries - As part of the merging process of this algorithm, nodes of
- /// different types can be represented by this single DSNode. This vector is
- /// kept sorted.
+ /// 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<DSTypeRec> TypeEntries;
+ 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
+ /// large, that there is an extra entry for the "remainder" of the node as
+ /// well. For this reason, nodes of 1 byte in size do have one link.
+ ///
+ std::vector<DSNodeHandle> Links;
/// Globals - The list of global values that are merged into this node.
///
std::vector<GlobalValue*> Globals;
void operator=(const DSNode &); // DO NOT IMPLEMENT
+ DSNode(const DSNode &); // DO NOT IMPLEMENT
public:
enum NodeTy {
ShadowNode = 0, // Nothing is known about this node...
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.
+ ///
+private:
+ unsigned short NodeType;
+public:
+
+ /// DSNode ctor - Create a node of the specified type, inserting it into the
+ /// specified graph.
///
- unsigned char NodeType;
+ DSNode(const Type *T, DSGraph *G);
- DSNode(enum NodeTy NT, const Type *T);
- DSNode(const DSNode &);
+ /// 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() {
-#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
/// getSize - Return the maximum number of bytes occupied by this object...
///
- unsigned getSize() const { return MergeMap.size(); }
+ unsigned getSize() const { return Size; }
- // getTypeEntries - Return the possible types and their offsets in this object
- const std::vector<DSTypeRec> &getTypeEntries() const { return TypeEntries; }
+ /// getType - Return the node type of this object...
+ ///
+ const Type *getType() const { return Ty; }
- /// getReferrers - Return a list of the pointers to this node...
+ bool isArray() const { return NodeType & Array; }
+
+ /// 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; }
+
+ /// 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; }
- /// isModified - Return true if this node may be modified in this context
+ DSGraph *getParentGraph() const { return ParentGraph; }
+ void setParentGraph(DSGraph *G) { ParentGraph = G; }
+
+
+ /// getTargetData - Get the target data object used to construct this node.
///
- bool isModified() const { return (NodeType & Modified) != 0; }
+ const TargetData &getTargetData() const;
- /// isRead - Return true if this node may be read in this context
+ /// getForwardNode - This method returns the node that this node is forwarded
+ /// to, if any.
///
- bool isRead() const { return (NodeType & Read) != 0; }
+ DSNode *getForwardNode() const { return ForwardNH.getNode(); }
+ /// isForwarding - Return true if this node is forwarding to another.
+ ///
+ bool isForwarding() const { return !ForwardNH.isNull(); }
- /// hasLink - Return true if this memory object has a link at the specified
- /// location.
+ /// stopForwarding - When the last reference to this forwarding node has been
+ /// dropped, delete the node.
///
- bool hasLink(unsigned i) const {
- assert(i < getSize() && "Field Link index is out of range!");
- return MergeMap[i] >= 0;
+ void stopForwarding() {
+ assert(isForwarding() &&
+ "Node isn't forwarding, cannot stopForwarding()!");
+ ForwardNH.setNode(0);
+ assert(ParentGraph == 0 &&
+ "Forwarding nodes must have been removed from graph!");
+ delete this;
}
- DSNodeHandle *getLink(unsigned i) {
- if (hasLink(i))
- return &Links[MergeMap[i]];
- return 0;
- }
- const DSNodeHandle *getLink(unsigned i) const {
- if (hasLink(i))
- return &Links[MergeMap[i]];
- return 0;
+ /// hasLink - Return true if this memory object has a link in slot #LinkNo
+ ///
+ bool hasLink(unsigned Offset) const {
+ assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
+ "Pointer offset not aligned correctly!");
+ unsigned Index = Offset >> DS::PointerShift;
+ assert(Index < Links.size() && "Link index is out of range!");
+ return Links[Index].getNode();
}
- /// getMergeMapLabel - Return the merge map entry specified, to allow printing
- /// out of DSNodes nicely for DOT graphs.
+ /// getLink - Return the link at the specified offset.
///
- int getMergeMapLabel(unsigned i) const {
- assert(i < MergeMap.size() && "MergeMap index out of range!");
- return MergeMap[i];
+ DSNodeHandle &getLink(unsigned Offset) {
+ assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
+ "Pointer offset not aligned correctly!");
+ unsigned Index = Offset >> DS::PointerShift;
+ assert(Index < Links.size() && "Link index is out of range!");
+ return Links[Index];
+ }
+ const DSNodeHandle &getLink(unsigned Offset) const {
+ assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
+ "Pointer offset not aligned correctly!");
+ unsigned Index = Offset >> DS::PointerShift;
+ assert(Index < Links.size() && "Link index is out of range!");
+ return Links[Index];
}
- /// getTypeRec - This method returns the specified type record if it exists.
- /// If it does not yet exist, the method checks to see whether or not the
- /// request would result in an untrackable state. If adding it would cause
- /// untrackable state, we foldNodeCompletely the node and return the void
- /// record, otherwise we add an new TypeEntry and return it.
+ /// 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
+ /// the node if this information is already known, or it may merge the node
+ /// completely (and return true) if the information is incompatible with what
+ /// is already known.
+ ///
+ /// This method returns true if the node is completely folded, otherwise
+ /// false.
///
- DSTypeRec &getTypeRec(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
/// single, completely pessimistic, node. This node is represented as a
- /// single byte with a single TypeEntry of "void".
+ /// single byte with a single TypeEntry of "void" with isArray = true.
///
void foldNodeCompletely();
/// NodeHandle, replacing what was there. It is uncommon to use this method,
/// instead one of the higher level methods should be used, below.
///
- void setLink(unsigned i, const DSNodeHandle &NH);
+ void setLink(unsigned Offset, const DSNodeHandle &NH) {
+ assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
+ "Pointer offset not aligned correctly!");
+ unsigned Index = Offset >> DS::PointerShift;
+ assert(Index < Links.size() && "Link index is out of range!");
+ 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.
///
void mergeWith(const DSNodeHandle &NH, unsigned Offset);
- /// mergeIndexes - If we discover that two indexes are equivalent and must be
- /// merged, this function is used to do the dirty work.
- ///
- void mergeIndexes(unsigned idx1, unsigned idx2) {
- assert(idx1 < getSize() && idx2 < getSize() && "Indexes out of range!");
- signed char MV1 = MergeMap[idx1];
- signed char MV2 = MergeMap[idx2];
- if (MV1 != MV2)
- mergeMappedValues(MV1, MV2);
- }
-
-
/// addGlobal - Add an entry for a global value to the Globals list. This
/// also marks the node with the 'G' flag if it does not already have it.
///
void addGlobal(GlobalValue *GV);
+ void mergeGlobals(const std::vector<GlobalValue*> &RHS);
const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
- std::vector<GlobalValue*> &getGlobals() { return Globals; }
+
+ typedef std::vector<GlobalValue*>::const_iterator global_iterator;
+ global_iterator global_begin() const { return Globals.begin(); }
+ global_iterator global_end() const { return Globals.end(); }
+
+
+ /// maskNodeTypes - Apply a mask to the node types bitfield.
+ ///
+ void maskNodeTypes(unsigned Mask) {
+ 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; }
+ 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; }
+ DSNode *setArrayMarker() { NodeType |= Array; 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 (isForwarding())
+ 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);
-
- /// rewriteMergeMap - Loop over the mergemap, replacing any references to the
- /// index From to be references to the index To.
- ///
- void rewriteMergeMap(signed char From, signed char To) {
- assert(From != To && "Cannot change something into itself!");
- for (unsigned i = 0, e = MergeMap.size(); i != e; ++i)
- if (MergeMap[i] == From)
- MergeMap[i] = To;
- }
- /// mergeMappedValues - This is the higher level form of rewriteMergeMap. It
- /// is fully capable of merging links together if neccesary as well as simply
- /// rewriting the map entries.
- ///
- void mergeMappedValues(signed char V1, signed char V2);
+ // static mergeNodes - Helper for mergeWith()
+ static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
+};
- /// growNode - Attempt to grow the node to the specified size. This may do
- /// one of three things:
- /// 1. Grow the node, return false
- /// 2. Refuse to grow the node, but maintain a trackable situation, return
- /// false.
- /// 3. Be unable to track if node was that size, so collapse the node and
- /// return true.
- ///
- bool growNode(unsigned RequestedSize);
+//===----------------------------------------------------------------------===//
+// 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 *createNode() { return new DSNode(0,0); }
+ //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->isForwarding()) && "Node handle offset out of range!");
+ if (N == 0 || !N->isForwarding())
+ return N;
-inline void DSNodeHandle::setNode(DSNode *n) {
- if (N) N->removeReferrer(this);
+ return HandleForwarding();
+}
+
+inline void DSNodeHandle::setNode(DSNode *n) const {
+ assert(!n || !n->isForwarding() && "Cannot set node to a forwarded node!");
+ if (N) getNode()->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->isForwarding()) && "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);
}
/// getLink - Treat this current node pointer as a pointer to a structure of
/// some sort. This method will return the pointer a mem[this+Num]
///
-inline const DSNodeHandle *DSNodeHandle::getLink(unsigned Num) const {
+inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
assert(N && "DSNodeHandle does not point to a node yet!");
- return N->getLink(Num+Offset);
+ return getNode()->getLink(Offset+Off);
}
-inline DSNodeHandle *DSNodeHandle::getLink(unsigned Num) {
+inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
assert(N && "DSNodeHandle does not point to a node yet!");
- return N->getLink(Num+Offset);
+ return getNode()->getLink(Off+Offset);
}
-inline void DSNodeHandle::setLink(unsigned Num, const DSNodeHandle &NH) {
+inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
assert(N && "DSNodeHandle does not point to a node yet!");
- N->setLink(Num+Offset, 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 LinkNo, const DSNodeHandle &Node) {
+inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
assert(N && "DSNodeHandle does not point to a node yet!");
- N->addEdgeTo(LinkNo+Offset, Node);
+ getNode()->addEdgeTo(Off+Offset, 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)
- N->mergeWith(Node, Offset);
+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;
+ Offset = 0;
+ setNode(Node.getNode());
+ Offset = Node.getOffset();
}
}
+} // End llvm namespace
+
#endif