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 template<typename BaseType>
11 class DSNodeIterator; // Data structure graph traversal iterator
13 //===----------------------------------------------------------------------===//
14 /// DSNode - Data structure node class
16 /// This class represents an untyped memory object of Size bytes. It keeps
17 /// track of any pointers that have been stored into the object as well as the
18 /// different types represented in this object.
21 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
22 /// this is a forwarding node, then this is the number of node handles which
23 /// are still forwarding over us.
25 unsigned NumReferrers;
27 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
28 /// that this node really is. When nodes get folded together, the node to be
29 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
31 DSNodeHandle ForwardNH;
33 /// Size - The current size of the node. This should be equal to the size of
34 /// the current type record.
38 /// ParentGraph - The graph this node is currently embedded into.
42 /// Ty - Keep track of the current outer most type of this object, in addition
43 /// to whether or not it has been indexed like an array or not. If the
44 /// isArray bit is set, the node cannot grow.
46 const Type *Ty; // The type itself...
48 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
49 /// object. Note that if the node is not a multiple of size(void*) bytes
50 /// large, that there is an extra entry for the "remainder" of the node as
51 /// well. For this reason, nodes of 1 byte in size do have one link.
53 std::vector<DSNodeHandle> Links;
55 /// Globals - The list of global values that are merged into this node.
57 std::vector<GlobalValue*> Globals;
59 void operator=(const DSNode &); // DO NOT IMPLEMENT
60 DSNode(const DSNode &); // DO NOT IMPLEMENT
63 ShadowNode = 0, // Nothing is known about this node...
64 AllocaNode = 1 << 0, // This node was allocated with alloca
65 HeapNode = 1 << 1, // This node was allocated with malloc
66 GlobalNode = 1 << 2, // This node was allocated by a global var decl
67 UnknownNode = 1 << 3, // This node points to unknown allocated memory
68 Incomplete = 1 << 4, // This node may not be complete
70 Modified = 1 << 5, // This node is modified in this context
71 Read = 1 << 6, // This node is read in this context
73 Array = 1 << 7, // This node is treated like an array
75 DEAD = 1 << 8, // This node is dead and should not be pointed to
78 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
81 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
82 /// to the nodes in the data structure graph, so it is possible to have nodes
83 /// with a value of 0 for their NodeType.
86 unsigned short NodeType;
89 DSNode(const Type *T, DSGraph *G);
90 DSNode(const DSNode &, DSGraph *G);
94 assert(hasNoReferrers() && "Referrers to dead node exist!");
97 // Iterator for graph interface... Defined in DSGraphTraits.h
98 typedef DSNodeIterator<DSNode> iterator;
99 typedef DSNodeIterator<const DSNode> const_iterator;
100 inline iterator begin();
101 inline iterator end();
102 inline const_iterator begin() const;
103 inline const_iterator end() const;
105 //===--------------------------------------------------
108 /// getSize - Return the maximum number of bytes occupied by this object...
110 unsigned getSize() const { return Size; }
112 // getType - Return the node type of this object...
113 const Type *getType() const { return Ty; }
114 bool isArray() const { return NodeType & Array; }
116 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
118 bool hasNoReferrers() const { return getNumReferrers() == 0; }
120 /// getNumReferrers - This method returns the number of referrers to the
121 /// current node. Note that if this node is a forwarding node, this will
122 /// return the number of nodes forwarding over the node!
123 unsigned getNumReferrers() const { return NumReferrers; }
125 DSGraph *getParentGraph() const { return ParentGraph; }
126 void setParentGraph(DSGraph *G) { ParentGraph = G; }
129 /// getForwardNode - This method returns the node that this node is forwarded
131 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
132 void stopForwarding() {
133 assert(!ForwardNH.isNull() &&
134 "Node isn't forwarding, cannot stopForwarding!");
135 ForwardNH.setNode(0);
138 /// hasLink - Return true if this memory object has a link in slot #LinkNo
140 bool hasLink(unsigned Offset) const {
141 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
142 "Pointer offset not aligned correctly!");
143 unsigned Index = Offset >> DS::PointerShift;
144 assert(Index < Links.size() && "Link index is out of range!");
145 return Links[Index].getNode();
148 /// getLink - Return the link at the specified offset.
149 DSNodeHandle &getLink(unsigned Offset) {
150 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
151 "Pointer offset not aligned correctly!");
152 unsigned Index = Offset >> DS::PointerShift;
153 assert(Index < Links.size() && "Link index is out of range!");
156 const DSNodeHandle &getLink(unsigned Offset) const {
157 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
158 "Pointer offset not aligned correctly!");
159 unsigned Index = Offset >> DS::PointerShift;
160 assert(Index < Links.size() && "Link index is out of range!");
164 /// getNumLinks - Return the number of links in a node...
166 unsigned getNumLinks() const { return Links.size(); }
168 /// mergeTypeInfo - This method merges the specified type into the current
169 /// node at the specified offset. This may update the current node's type
170 /// record if this gives more information to the node, it may do nothing to
171 /// the node if this information is already known, or it may merge the node
172 /// completely (and return true) if the information is incompatible with what
173 /// is already known.
175 /// This method returns true if the node is completely folded, otherwise
178 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
179 bool FoldIfIncompatible = true);
181 /// foldNodeCompletely - If we determine that this node has some funny
182 /// behavior happening to it that we cannot represent, we fold it down to a
183 /// single, completely pessimistic, node. This node is represented as a
184 /// single byte with a single TypeEntry of "void" with isArray = true.
186 void foldNodeCompletely();
188 /// isNodeCompletelyFolded - Return true if this node has been completely
189 /// folded down to something that can never be expanded, effectively losing
190 /// all of the field sensitivity that may be present in the node.
192 bool isNodeCompletelyFolded() const;
194 /// setLink - Set the link at the specified offset to the specified
195 /// NodeHandle, replacing what was there. It is uncommon to use this method,
196 /// instead one of the higher level methods should be used, below.
198 void setLink(unsigned Offset, const DSNodeHandle &NH) {
199 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
200 "Pointer offset not aligned correctly!");
201 unsigned Index = Offset >> DS::PointerShift;
202 assert(Index < Links.size() && "Link index is out of range!");
206 /// getPointerSize - Return the size of a pointer for the current target.
208 unsigned getPointerSize() const { return DS::PointerSize; }
210 /// addEdgeTo - Add an edge from the current node to the specified node. This
211 /// can cause merging of nodes in the graph.
213 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
215 /// mergeWith - Merge this node and the specified node, moving all links to
216 /// and from the argument node into the current node, deleting the node
217 /// argument. Offset indicates what offset the specified node is to be merged
218 /// into the current node.
220 /// The specified node may be a null pointer (in which case, nothing happens).
222 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
224 /// addGlobal - Add an entry for a global value to the Globals list. This
225 /// also marks the node with the 'G' flag if it does not already have it.
227 void addGlobal(GlobalValue *GV);
228 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
230 /// maskNodeTypes - Apply a mask to the node types bitfield.
232 void maskNodeTypes(unsigned Mask) {
236 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
237 /// is set, hide it from the caller.
238 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
240 bool isAllocaNode() const { return NodeType & AllocaNode; }
241 bool isHeapNode() const { return NodeType & HeapNode; }
242 bool isGlobalNode() const { return NodeType & GlobalNode; }
243 bool isUnknownNode() const { return NodeType & UnknownNode; }
245 bool isModified() const { return NodeType & Modified; }
246 bool isRead() const { return NodeType & Read; }
248 bool isIncomplete() const { return NodeType & Incomplete; }
249 bool isDeadNode() const { return NodeType & DEAD; }
251 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
252 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
253 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
254 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
256 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
257 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
258 DSNode *setReadMarker() { NodeType |= Read; return this; }
260 void makeNodeDead() {
262 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
266 /// forwardNode - Mark this node as being obsolete, and all references to it
267 /// should be forwarded to the specified node and offset.
269 void forwardNode(DSNode *To, unsigned Offset);
271 void print(std::ostream &O, const DSGraph *G) const;
274 void assertOK() const;
276 void dropAllReferences() {
278 if (!ForwardNH.isNull())
279 ForwardNH.setNode(0);
282 /// remapLinks - Change all of the Links in the current node according to the
283 /// specified mapping.
284 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
286 /// markReachableNodes - This method recursively traverses the specified
287 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
288 /// adds to the set, which allows it to only traverse visited nodes once.
290 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
293 friend class DSNodeHandle;
295 // static mergeNodes - Helper for mergeWith()
296 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
300 //===----------------------------------------------------------------------===//
301 // Define inline DSNodeHandle functions that depend on the definition of DSNode
303 inline DSNode *DSNodeHandle::getNode() const {
304 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
305 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
306 if (!N || N->ForwardNH.isNull())
309 return HandleForwarding();
312 inline void DSNodeHandle::setNode(DSNode *n) {
313 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
314 if (N) N->NumReferrers--;
318 if (Offset >= N->Size) {
319 assert((Offset == 0 || N->Size == 1) &&
320 "Pointer to non-collapsed node with invalid offset!");
324 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
325 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
326 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
329 inline bool DSNodeHandle::hasLink(unsigned Num) const {
330 assert(N && "DSNodeHandle does not point to a node yet!");
331 return getNode()->hasLink(Num+Offset);
335 /// getLink - Treat this current node pointer as a pointer to a structure of
336 /// some sort. This method will return the pointer a mem[this+Num]
338 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
339 assert(N && "DSNodeHandle does not point to a node yet!");
340 return getNode()->getLink(Offset+Off);
342 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
343 assert(N && "DSNodeHandle does not point to a node yet!");
344 return getNode()->getLink(Off+Offset);
347 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
348 assert(N && "DSNodeHandle does not point to a node yet!");
349 getNode()->setLink(Off+Offset, NH);
352 /// addEdgeTo - Add an edge from the current node to the specified node. This
353 /// can cause merging of nodes in the graph.
355 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
356 assert(N && "DSNodeHandle does not point to a node yet!");
357 getNode()->addEdgeTo(Off+Offset, Node);
360 /// mergeWith - Merge the logical node pointed to by 'this' with the node
361 /// pointed to by 'N'.
363 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
365 getNode()->mergeWith(Node, Offset);
366 else // No node to merge with, so just point to Node