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
12 #include "llvm/Analysis/DSSupport.h"
13 template<typename BaseType>
14 class DSNodeIterator; // Data structure graph traversal iterator
16 //===----------------------------------------------------------------------===//
17 /// DSNode - Data structure node class
19 /// This class represents an untyped memory object of Size bytes. It keeps
20 /// track of any pointers that have been stored into the object as well as the
21 /// different types represented in this object.
24 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
25 /// this is a forwarding node, then this is the number of node handles which
26 /// are still forwarding over us.
28 unsigned NumReferrers;
30 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
31 /// that this node really is. When nodes get folded together, the node to be
32 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
34 DSNodeHandle ForwardNH;
36 /// Size - The current size of the node. This should be equal to the size of
37 /// the current type record.
41 /// ParentGraph - The graph this node is currently embedded into.
45 /// Ty - Keep track of the current outer most type of this object, in addition
46 /// to whether or not it has been indexed like an array or not. If the
47 /// isArray bit is set, the node cannot grow.
49 const Type *Ty; // The type itself...
51 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
52 /// object. Note that if the node is not a multiple of size(void*) bytes
53 /// large, that there is an extra entry for the "remainder" of the node as
54 /// well. For this reason, nodes of 1 byte in size do have one link.
56 std::vector<DSNodeHandle> Links;
58 /// Globals - The list of global values that are merged into this node.
60 std::vector<GlobalValue*> Globals;
62 void operator=(const DSNode &); // DO NOT IMPLEMENT
63 DSNode(const DSNode &); // DO NOT IMPLEMENT
66 ShadowNode = 0, // Nothing is known about this node...
67 AllocaNode = 1 << 0, // This node was allocated with alloca
68 HeapNode = 1 << 1, // This node was allocated with malloc
69 GlobalNode = 1 << 2, // This node was allocated by a global var decl
70 UnknownNode = 1 << 3, // This node points to unknown allocated memory
71 Incomplete = 1 << 4, // This node may not be complete
73 Modified = 1 << 5, // This node is modified in this context
74 Read = 1 << 6, // This node is read in this context
76 Array = 1 << 7, // This node is treated like an array
77 MultiObject = 1 << 8, // This node represents > 1 object (may alias)
79 DEAD = 1 << 9, // This node is dead and should not be pointed to
82 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
85 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
86 /// to the nodes in the data structure graph, so it is possible to have nodes
87 /// with a value of 0 for their NodeType.
90 unsigned short NodeType;
93 DSNode(const Type *T, DSGraph *G);
94 DSNode(const DSNode &, DSGraph *G);
98 assert(hasNoReferrers() && "Referrers to dead node exist!");
101 // Iterator for graph interface... Defined in DSGraphTraits.h
102 typedef DSNodeIterator<DSNode> iterator;
103 typedef DSNodeIterator<const DSNode> const_iterator;
104 inline iterator begin();
105 inline iterator end();
106 inline const_iterator begin() const;
107 inline const_iterator end() const;
109 //===--------------------------------------------------
112 /// getSize - Return the maximum number of bytes occupied by this object...
114 unsigned getSize() const { return Size; }
116 // getType - Return the node type of this object...
117 const Type *getType() const { return Ty; }
118 bool isArray() const { return NodeType & Array; }
120 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
122 bool hasNoReferrers() const { return getNumReferrers() == 0; }
124 /// getNumReferrers - This method returns the number of referrers to the
125 /// current node. Note that if this node is a forwarding node, this will
126 /// return the number of nodes forwarding over the node!
127 unsigned getNumReferrers() const { return NumReferrers; }
129 DSGraph *getParentGraph() const { return ParentGraph; }
130 void setParentGraph(DSGraph *G) { ParentGraph = G; }
133 /// getForwardNode - This method returns the node that this node is forwarded
135 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
136 void stopForwarding() {
137 assert(!ForwardNH.isNull() &&
138 "Node isn't forwarding, cannot stopForwarding!");
139 ForwardNH.setNode(0);
142 /// hasLink - Return true if this memory object has a link in slot #LinkNo
144 bool hasLink(unsigned Offset) const {
145 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
146 "Pointer offset not aligned correctly!");
147 unsigned Index = Offset >> DS::PointerShift;
148 assert(Index < Links.size() && "Link index is out of range!");
149 return Links[Index].getNode();
152 /// getLink - Return the link at the specified offset.
153 DSNodeHandle &getLink(unsigned Offset) {
154 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
155 "Pointer offset not aligned correctly!");
156 unsigned Index = Offset >> DS::PointerShift;
157 assert(Index < Links.size() && "Link index is out of range!");
160 const DSNodeHandle &getLink(unsigned Offset) const {
161 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
162 "Pointer offset not aligned correctly!");
163 unsigned Index = Offset >> DS::PointerShift;
164 assert(Index < Links.size() && "Link index is out of range!");
168 /// getNumLinks - Return the number of links in a node...
170 unsigned getNumLinks() const { return Links.size(); }
172 /// mergeTypeInfo - This method merges the specified type into the current
173 /// node at the specified offset. This may update the current node's type
174 /// record if this gives more information to the node, it may do nothing to
175 /// the node if this information is already known, or it may merge the node
176 /// completely (and return true) if the information is incompatible with what
177 /// is already known.
179 /// This method returns true if the node is completely folded, otherwise
182 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
183 bool FoldIfIncompatible = true);
185 /// foldNodeCompletely - If we determine that this node has some funny
186 /// behavior happening to it that we cannot represent, we fold it down to a
187 /// single, completely pessimistic, node. This node is represented as a
188 /// single byte with a single TypeEntry of "void" with isArray = true.
190 void foldNodeCompletely();
192 /// isNodeCompletelyFolded - Return true if this node has been completely
193 /// folded down to something that can never be expanded, effectively losing
194 /// all of the field sensitivity that may be present in the node.
196 bool isNodeCompletelyFolded() const;
198 /// setLink - Set the link at the specified offset to the specified
199 /// NodeHandle, replacing what was there. It is uncommon to use this method,
200 /// instead one of the higher level methods should be used, below.
202 void setLink(unsigned Offset, const DSNodeHandle &NH) {
203 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
204 "Pointer offset not aligned correctly!");
205 unsigned Index = Offset >> DS::PointerShift;
206 assert(Index < Links.size() && "Link index is out of range!");
210 /// getPointerSize - Return the size of a pointer for the current target.
212 unsigned getPointerSize() const { return DS::PointerSize; }
214 /// addEdgeTo - Add an edge from the current node to the specified node. This
215 /// can cause merging of nodes in the graph.
217 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
219 /// mergeWith - Merge this node and the specified node, moving all links to
220 /// and from the argument node into the current node, deleting the node
221 /// argument. Offset indicates what offset the specified node is to be merged
222 /// into the current node.
224 /// The specified node may be a null pointer (in which case, nothing happens).
226 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
228 /// addGlobal - Add an entry for a global value to the Globals list. This
229 /// also marks the node with the 'G' flag if it does not already have it.
231 void addGlobal(GlobalValue *GV);
232 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
234 /// maskNodeTypes - Apply a mask to the node types bitfield.
236 void maskNodeTypes(unsigned Mask) {
240 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
241 /// is set, hide it from the caller.
242 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
244 bool isAllocaNode() const { return NodeType & AllocaNode; }
245 bool isHeapNode() const { return NodeType & HeapNode; }
246 bool isGlobalNode() const { return NodeType & GlobalNode; }
247 bool isUnknownNode() const { return NodeType & UnknownNode; }
249 bool isModified() const { return NodeType & Modified; }
250 bool isRead() const { return NodeType & Read; }
252 bool isIncomplete() const { return NodeType & Incomplete; }
253 bool isMultiObject() const { return NodeType & MultiObject; }
254 bool isDeadNode() const { return NodeType & DEAD; }
256 DSNode *setAllocaNodeMarker() { return setCompositionMarker(AllocaNode); }
257 DSNode *setHeapNodeMarker() { return setCompositionMarker(HeapNode); }
258 DSNode *setGlobalNodeMarker() { return setCompositionMarker(GlobalNode); }
259 DSNode *setUnknownNodeMarker() { return setCompositionMarker(UnknownNode); }
261 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
262 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
263 DSNode *setReadMarker() { NodeType |= Read; return this; }
265 void makeNodeDead() {
267 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
271 /// forwardNode - Mark this node as being obsolete, and all references to it
272 /// should be forwarded to the specified node and offset.
274 void forwardNode(DSNode *To, unsigned Offset);
276 void print(std::ostream &O, const DSGraph *G) const;
279 void assertOK() const;
281 void dropAllReferences() {
283 if (!ForwardNH.isNull())
284 ForwardNH.setNode(0);
287 /// remapLinks - Change all of the Links in the current node according to the
288 /// specified mapping.
289 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
291 /// markReachableNodes - This method recursively traverses the specified
292 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
293 /// adds to the set, which allows it to only traverse visited nodes once.
295 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
298 friend class DSNodeHandle;
300 DSNode *setCompositionMarker(unsigned Marker) {
301 if (NodeType & Composition) Marker |= MultiObject;
306 // static mergeNodes - Helper for mergeWith()
307 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
311 //===----------------------------------------------------------------------===//
312 // Define inline DSNodeHandle functions that depend on the definition of DSNode
314 inline DSNode *DSNodeHandle::getNode() const {
315 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
316 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
317 if (!N || N->ForwardNH.isNull())
320 return HandleForwarding();
323 inline void DSNodeHandle::setNode(DSNode *n) {
324 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
325 if (N) N->NumReferrers--;
329 if (Offset >= N->Size) {
330 assert((Offset == 0 || N->Size == 1) &&
331 "Pointer to non-collapsed node with invalid offset!");
335 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
336 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
337 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
340 inline bool DSNodeHandle::hasLink(unsigned Num) const {
341 assert(N && "DSNodeHandle does not point to a node yet!");
342 return getNode()->hasLink(Num+Offset);
346 /// getLink - Treat this current node pointer as a pointer to a structure of
347 /// some sort. This method will return the pointer a mem[this+Num]
349 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
350 assert(N && "DSNodeHandle does not point to a node yet!");
351 return getNode()->getLink(Offset+Off);
353 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
354 assert(N && "DSNodeHandle does not point to a node yet!");
355 return getNode()->getLink(Off+Offset);
358 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
359 assert(N && "DSNodeHandle does not point to a node yet!");
360 getNode()->setLink(Off+Offset, NH);
363 /// addEdgeTo - Add an edge from the current node to the specified node. This
364 /// can cause merging of nodes in the graph.
366 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
367 assert(N && "DSNodeHandle does not point to a node yet!");
368 getNode()->addEdgeTo(Off+Offset, Node);
371 /// mergeWith - Merge the logical node pointed to by 'this' with the node
372 /// pointed to by 'N'.
374 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
376 getNode()->mergeWith(Node, Offset);
377 else // No node to merge with, so just point to Node