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"
11 template<typename BaseType>
12 class DSNodeIterator; // Data structure graph traversal iterator
14 //===----------------------------------------------------------------------===//
15 /// DSNode - Data structure node class
17 /// This class represents an untyped memory object of Size bytes. It keeps
18 /// track of any pointers that have been stored into the object as well as the
19 /// different types represented in this object.
22 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
23 /// this is a forwarding node, then this is the number of node handles which
24 /// are still forwarding over us.
26 unsigned NumReferrers;
28 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
29 /// that this node really is. When nodes get folded together, the node to be
30 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
32 DSNodeHandle ForwardNH;
34 /// Size - The current size of the node. This should be equal to the size of
35 /// the current type record.
39 /// ParentGraph - The graph this node is currently embedded into.
43 /// Ty - Keep track of the current outer most type of this object, in addition
44 /// to whether or not it has been indexed like an array or not. If the
45 /// isArray bit is set, the node cannot grow.
47 const Type *Ty; // The type itself...
49 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
50 /// object. Note that if the node is not a multiple of size(void*) bytes
51 /// large, that there is an extra entry for the "remainder" of the node as
52 /// well. For this reason, nodes of 1 byte in size do have one link.
54 std::vector<DSNodeHandle> Links;
56 /// Globals - The list of global values that are merged into this node.
58 std::vector<GlobalValue*> Globals;
60 void operator=(const DSNode &); // DO NOT IMPLEMENT
61 DSNode(const DSNode &); // DO NOT IMPLEMENT
64 ShadowNode = 0, // Nothing is known about this node...
65 AllocaNode = 1 << 0, // This node was allocated with alloca
66 HeapNode = 1 << 1, // This node was allocated with malloc
67 GlobalNode = 1 << 2, // This node was allocated by a global var decl
68 UnknownNode = 1 << 3, // This node points to unknown allocated memory
69 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
72 Array = 1 << 7, // This node is treated like an array
74 DEAD = 1 << 8, // This node is dead and should not be pointed to
77 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
80 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
81 /// to the nodes in the data structure graph, so it is possible to have nodes
82 /// with a value of 0 for their NodeType. Scalar and Alloca markers go away
83 /// when function graphs are inlined.
85 unsigned short NodeType;
87 DSNode(unsigned NodeTy, const Type *T, DSGraph *G);
88 DSNode(const DSNode &, DSGraph *G);
92 assert(hasNoReferrers() && "Referrers to dead node exist!");
95 // Iterator for graph interface... Defined in DSGraphTraits.h
96 typedef DSNodeIterator<DSNode> iterator;
97 typedef DSNodeIterator<const DSNode> const_iterator;
98 inline iterator begin();
99 inline iterator end();
100 inline const_iterator begin() const;
101 inline const_iterator end() const;
103 //===--------------------------------------------------
106 /// getSize - Return the maximum number of bytes occupied by this object...
108 unsigned getSize() const { return Size; }
110 // getType - Return the node type of this object...
111 const Type *getType() const { return Ty; }
112 bool isArray() const { return NodeType & Array; }
114 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
116 bool hasNoReferrers() const { return getNumReferrers() == 0; }
118 /// getNumReferrers - This method returns the number of referrers to the
119 /// current node. Note that if this node is a forwarding node, this will
120 /// return the number of nodes forwarding over the node!
121 unsigned getNumReferrers() const { return NumReferrers; }
123 /// isModified - Return true if this node may be modified in this context
125 bool isModified() const { return (NodeType & Modified) != 0; }
127 /// isRead - Return true if this node may be read in this context
129 bool isRead() const { return (NodeType & Read) != 0; }
131 DSGraph *getParentGraph() const { return ParentGraph; }
132 void setParentGraph(DSGraph *G) { ParentGraph = G; }
135 /// getForwardNode - This method returns the node that this node is forwarded
137 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
138 void stopForwarding() {
139 assert(!ForwardNH.isNull() &&
140 "Node isn't forwarding, cannot stopForwarding!");
141 ForwardNH.setNode(0);
144 /// hasLink - Return true if this memory object has a link in slot #LinkNo
146 bool hasLink(unsigned Offset) const {
147 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
148 "Pointer offset not aligned correctly!");
149 unsigned Index = Offset >> DS::PointerShift;
150 assert(Index < Links.size() && "Link index is out of range!");
151 return Links[Index].getNode();
154 /// getLink - Return the link at the specified offset.
155 DSNodeHandle &getLink(unsigned Offset) {
156 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
157 "Pointer offset not aligned correctly!");
158 unsigned Index = Offset >> DS::PointerShift;
159 assert(Index < Links.size() && "Link index is out of range!");
162 const DSNodeHandle &getLink(unsigned Offset) const {
163 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
164 "Pointer offset not aligned correctly!");
165 unsigned Index = Offset >> DS::PointerShift;
166 assert(Index < Links.size() && "Link index is out of range!");
170 /// getNumLinks - Return the number of links in a node...
172 unsigned getNumLinks() const { return Links.size(); }
174 /// mergeTypeInfo - This method merges the specified type into the current
175 /// node at the specified offset. This may update the current node's type
176 /// record if this gives more information to the node, it may do nothing to
177 /// the node if this information is already known, or it may merge the node
178 /// completely (and return true) if the information is incompatible with what
179 /// is already known.
181 /// This method returns true if the node is completely folded, otherwise
184 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
185 bool FoldIfIncompatible = true);
187 /// foldNodeCompletely - If we determine that this node has some funny
188 /// behavior happening to it that we cannot represent, we fold it down to a
189 /// single, completely pessimistic, node. This node is represented as a
190 /// single byte with a single TypeEntry of "void" with isArray = true.
192 void foldNodeCompletely();
194 /// isNodeCompletelyFolded - Return true if this node has been completely
195 /// folded down to something that can never be expanded, effectively losing
196 /// all of the field sensitivity that may be present in the node.
198 bool isNodeCompletelyFolded() const;
200 /// setLink - Set the link at the specified offset to the specified
201 /// NodeHandle, replacing what was there. It is uncommon to use this method,
202 /// instead one of the higher level methods should be used, below.
204 void setLink(unsigned Offset, const DSNodeHandle &NH) {
205 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
206 "Pointer offset not aligned correctly!");
207 unsigned Index = Offset >> DS::PointerShift;
208 assert(Index < Links.size() && "Link index is out of range!");
212 /// getPointerSize - Return the size of a pointer for the current target.
214 unsigned getPointerSize() const { return DS::PointerSize; }
216 /// addEdgeTo - Add an edge from the current node to the specified node. This
217 /// can cause merging of nodes in the graph.
219 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
221 /// mergeWith - Merge this node and the specified node, moving all links to
222 /// and from the argument node into the current node, deleting the node
223 /// argument. Offset indicates what offset the specified node is to be merged
224 /// into the current node.
226 /// The specified node may be a null pointer (in which case, nothing happens).
228 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
230 /// addGlobal - Add an entry for a global value to the Globals list. This
231 /// also marks the node with the 'G' flag if it does not already have it.
233 void addGlobal(GlobalValue *GV);
234 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
235 std::vector<GlobalValue*> &getGlobals() { return Globals; }
237 /// forwardNode - Mark this node as being obsolete, and all references to it
238 /// should be forwarded to the specified node and offset.
240 void forwardNode(DSNode *To, unsigned Offset);
242 void print(std::ostream &O, const DSGraph *G) const;
245 void assertOK() const;
247 void dropAllReferences() {
249 if (!ForwardNH.isNull())
250 ForwardNH.setNode(0);
253 /// remapLinks - Change all of the Links in the current node according to the
254 /// specified mapping.
255 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
257 /// markReachableNodes - This method recursively traverses the specified
258 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
259 /// adds to the set, which allows it to only traverse visited nodes once.
261 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
264 friend class DSNodeHandle;
266 // static mergeNodes - Helper for mergeWith()
267 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
271 //===----------------------------------------------------------------------===//
272 // Define inline DSNodeHandle functions that depend on the definition of DSNode
274 inline DSNode *DSNodeHandle::getNode() const {
275 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
276 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
277 if (!N || N->ForwardNH.isNull())
280 return HandleForwarding();
283 inline void DSNodeHandle::setNode(DSNode *n) {
284 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
285 if (N) N->NumReferrers--;
289 if (Offset >= N->Size) {
290 assert((Offset == 0 || N->Size == 1) &&
291 "Pointer to non-collapsed node with invalid offset!");
295 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
297 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
298 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
301 inline bool DSNodeHandle::hasLink(unsigned Num) const {
302 assert(N && "DSNodeHandle does not point to a node yet!");
303 return getNode()->hasLink(Num+Offset);
307 /// getLink - Treat this current node pointer as a pointer to a structure of
308 /// some sort. This method will return the pointer a mem[this+Num]
310 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
311 assert(N && "DSNodeHandle does not point to a node yet!");
312 return getNode()->getLink(Offset+Off);
314 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
315 assert(N && "DSNodeHandle does not point to a node yet!");
316 return getNode()->getLink(Off+Offset);
319 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
320 assert(N && "DSNodeHandle does not point to a node yet!");
321 getNode()->setLink(Off+Offset, NH);
324 /// addEdgeTo - Add an edge from the current node to the specified node. This
325 /// can cause merging of nodes in the graph.
327 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
328 assert(N && "DSNodeHandle does not point to a node yet!");
329 getNode()->addEdgeTo(Off+Offset, Node);
332 /// mergeWith - Merge the logical node pointed to by 'this' with the node
333 /// pointed to by 'N'.
335 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
337 getNode()->mergeWith(Node, Offset);
338 else // No node to merge with, so just point to Node