1 //===- DSNode.h - Node definition for datastructure graphs ------*- C++ -*-===//
3 // Data structure graph nodes and some implementation of DSNodeHandle.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DSNODE_H
8 #define LLVM_ANALYSIS_DSNODE_H
10 #include "llvm/Analysis/DSSupport.h"
12 template<typename BaseType>
13 class DSNodeIterator; // Data structure graph traversal iterator
15 //===----------------------------------------------------------------------===//
16 /// DSNode - Data structure node class
18 /// This class represents an untyped memory object of Size bytes. It keeps
19 /// track of any pointers that have been stored into the object as well as the
20 /// different types represented in this object.
23 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
24 /// this is a forwarding node, then this is the number of node handles which
25 /// are still forwarding over us.
27 unsigned NumReferrers;
29 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
30 /// that this node really is. When nodes get folded together, the node to be
31 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
33 DSNodeHandle ForwardNH;
35 /// Size - The current size of the node. This should be equal to the size of
36 /// the current type record.
40 /// ParentGraph - The graph this node is currently embedded into.
44 /// Ty - Keep track of the current outer most type of this object, in addition
45 /// to whether or not it has been indexed like an array or not. If the
46 /// isArray bit is set, the node cannot grow.
48 const Type *Ty; // The type itself...
50 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
51 /// object. Note that if the node is not a multiple of size(void*) bytes
52 /// large, that there is an extra entry for the "remainder" of the node as
53 /// well. For this reason, nodes of 1 byte in size do have one link.
55 std::vector<DSNodeHandle> Links;
57 /// Globals - The list of global values that are merged into this node.
59 std::vector<GlobalValue*> Globals;
61 void operator=(const DSNode &); // DO NOT IMPLEMENT
62 DSNode(const DSNode &); // DO NOT IMPLEMENT
65 ShadowNode = 0, // Nothing is known about this node...
66 AllocaNode = 1 << 0, // This node was allocated with alloca
67 HeapNode = 1 << 1, // This node was allocated with malloc
68 GlobalNode = 1 << 2, // This node was allocated by a global var decl
69 UnknownNode = 1 << 3, // This node points to unknown allocated memory
70 Incomplete = 1 << 4, // This node may not be complete
72 Modified = 1 << 5, // This node is modified in this context
73 Read = 1 << 6, // This node is read in this context
75 Array = 1 << 7, // This node is treated like an array
77 DEAD = 1 << 8, // This node is dead and should not be pointed to
80 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
83 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
84 /// to the nodes in the data structure graph, so it is possible to have nodes
85 /// with a value of 0 for their NodeType.
88 unsigned short NodeType;
91 DSNode(const Type *T, DSGraph *G);
92 DSNode(const DSNode &, DSGraph *G);
96 assert(hasNoReferrers() && "Referrers to dead node exist!");
99 // Iterator for graph interface... Defined in DSGraphTraits.h
100 typedef DSNodeIterator<DSNode> iterator;
101 typedef DSNodeIterator<const DSNode> const_iterator;
102 inline iterator begin();
103 inline iterator end();
104 inline const_iterator begin() const;
105 inline const_iterator end() const;
107 //===--------------------------------------------------
110 /// getSize - Return the maximum number of bytes occupied by this object...
112 unsigned getSize() const { return Size; }
114 // getType - Return the node type of this object...
115 const Type *getType() const { return Ty; }
116 bool isArray() const { return NodeType & Array; }
118 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
120 bool hasNoReferrers() const { return getNumReferrers() == 0; }
122 /// getNumReferrers - This method returns the number of referrers to the
123 /// current node. Note that if this node is a forwarding node, this will
124 /// return the number of nodes forwarding over the node!
125 unsigned getNumReferrers() const { return NumReferrers; }
127 DSGraph *getParentGraph() const { return ParentGraph; }
128 void setParentGraph(DSGraph *G) { ParentGraph = G; }
131 /// getForwardNode - This method returns the node that this node is forwarded
133 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
134 void stopForwarding() {
135 assert(!ForwardNH.isNull() &&
136 "Node isn't forwarding, cannot stopForwarding!");
137 ForwardNH.setNode(0);
140 /// hasLink - Return true if this memory object has a link in slot #LinkNo
142 bool hasLink(unsigned Offset) const {
143 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
144 "Pointer offset not aligned correctly!");
145 unsigned Index = Offset >> DS::PointerShift;
146 assert(Index < Links.size() && "Link index is out of range!");
147 return Links[Index].getNode();
150 /// getLink - Return the link at the specified offset.
151 DSNodeHandle &getLink(unsigned Offset) {
152 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
153 "Pointer offset not aligned correctly!");
154 unsigned Index = Offset >> DS::PointerShift;
155 assert(Index < Links.size() && "Link index is out of range!");
158 const DSNodeHandle &getLink(unsigned Offset) const {
159 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
160 "Pointer offset not aligned correctly!");
161 unsigned Index = Offset >> DS::PointerShift;
162 assert(Index < Links.size() && "Link index is out of range!");
166 /// getNumLinks - Return the number of links in a node...
168 unsigned getNumLinks() const { return Links.size(); }
170 /// mergeTypeInfo - This method merges the specified type into the current
171 /// node at the specified offset. This may update the current node's type
172 /// record if this gives more information to the node, it may do nothing to
173 /// the node if this information is already known, or it may merge the node
174 /// completely (and return true) if the information is incompatible with what
175 /// is already known.
177 /// This method returns true if the node is completely folded, otherwise
180 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
181 bool FoldIfIncompatible = true);
183 /// foldNodeCompletely - If we determine that this node has some funny
184 /// behavior happening to it that we cannot represent, we fold it down to a
185 /// single, completely pessimistic, node. This node is represented as a
186 /// single byte with a single TypeEntry of "void" with isArray = true.
188 void foldNodeCompletely();
190 /// isNodeCompletelyFolded - Return true if this node has been completely
191 /// folded down to something that can never be expanded, effectively losing
192 /// all of the field sensitivity that may be present in the node.
194 bool isNodeCompletelyFolded() const;
196 /// setLink - Set the link at the specified offset to the specified
197 /// NodeHandle, replacing what was there. It is uncommon to use this method,
198 /// instead one of the higher level methods should be used, below.
200 void setLink(unsigned Offset, const DSNodeHandle &NH) {
201 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
202 "Pointer offset not aligned correctly!");
203 unsigned Index = Offset >> DS::PointerShift;
204 assert(Index < Links.size() && "Link index is out of range!");
208 /// getPointerSize - Return the size of a pointer for the current target.
210 unsigned getPointerSize() const { return DS::PointerSize; }
212 /// addEdgeTo - Add an edge from the current node to the specified node. This
213 /// can cause merging of nodes in the graph.
215 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
217 /// mergeWith - Merge this node and the specified node, moving all links to
218 /// and from the argument node into the current node, deleting the node
219 /// argument. Offset indicates what offset the specified node is to be merged
220 /// into the current node.
222 /// The specified node may be a null pointer (in which case, nothing happens).
224 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
226 /// addGlobal - Add an entry for a global value to the Globals list. This
227 /// also marks the node with the 'G' flag if it does not already have it.
229 void addGlobal(GlobalValue *GV);
230 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
232 /// maskNodeTypes - Apply a mask to the node types bitfield.
234 void maskNodeTypes(unsigned Mask) {
238 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
239 /// is set, hide it from the caller.
240 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
242 bool isAllocaNode() const { return NodeType & AllocaNode; }
243 bool isHeapNode() const { return NodeType & HeapNode; }
244 bool isGlobalNode() const { return NodeType & GlobalNode; }
245 bool isUnknownNode() const { return NodeType & UnknownNode; }
247 bool isModified() const { return NodeType & Modified; }
248 bool isRead() const { return NodeType & Read; }
250 bool isIncomplete() const { return NodeType & Incomplete; }
251 bool isDeadNode() const { return NodeType & DEAD; }
253 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
254 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
255 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
256 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
258 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
259 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
260 DSNode *setReadMarker() { NodeType |= Read; return this; }
262 void makeNodeDead() {
264 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
268 /// forwardNode - Mark this node as being obsolete, and all references to it
269 /// should be forwarded to the specified node and offset.
271 void forwardNode(DSNode *To, unsigned Offset);
273 void print(std::ostream &O, const DSGraph *G) const;
276 void assertOK() const;
278 void dropAllReferences() {
280 if (!ForwardNH.isNull())
281 ForwardNH.setNode(0);
284 /// remapLinks - Change all of the Links in the current node according to the
285 /// specified mapping.
286 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
288 /// markReachableNodes - This method recursively traverses the specified
289 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
290 /// adds to the set, which allows it to only traverse visited nodes once.
292 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
295 friend class DSNodeHandle;
297 // static mergeNodes - Helper for mergeWith()
298 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
302 //===----------------------------------------------------------------------===//
303 // Define inline DSNodeHandle functions that depend on the definition of DSNode
305 inline DSNode *DSNodeHandle::getNode() const {
306 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
307 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
308 if (!N || N->ForwardNH.isNull())
311 return HandleForwarding();
314 inline void DSNodeHandle::setNode(DSNode *n) {
315 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
316 if (N) N->NumReferrers--;
320 if (Offset >= N->Size) {
321 assert((Offset == 0 || N->Size == 1) &&
322 "Pointer to non-collapsed node with invalid offset!");
326 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
327 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
328 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
331 inline bool DSNodeHandle::hasLink(unsigned Num) const {
332 assert(N && "DSNodeHandle does not point to a node yet!");
333 return getNode()->hasLink(Num+Offset);
337 /// getLink - Treat this current node pointer as a pointer to a structure of
338 /// some sort. This method will return the pointer a mem[this+Num]
340 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
341 assert(N && "DSNodeHandle does not point to a node yet!");
342 return getNode()->getLink(Offset+Off);
344 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
345 assert(N && "DSNodeHandle does not point to a node yet!");
346 return getNode()->getLink(Off+Offset);
349 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
350 assert(N && "DSNodeHandle does not point to a node yet!");
351 getNode()->setLink(Off+Offset, NH);
354 /// addEdgeTo - Add an edge from the current node to the specified node. This
355 /// can cause merging of nodes in the graph.
357 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
358 assert(N && "DSNodeHandle does not point to a node yet!");
359 getNode()->addEdgeTo(Off+Offset, Node);
362 /// mergeWith - Merge the logical node pointed to by 'this' with the node
363 /// pointed to by 'N'.
365 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
367 getNode()->mergeWith(Node, Offset);
368 else // No node to merge with, so just point to Node