1 //===- DSNode.h - Node definition for datastructure graphs ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Data structure graph nodes and some implementation of DSNodeHandle.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_DSNODE_H
15 #define LLVM_ANALYSIS_DSNODE_H
17 #include "llvm/Analysis/DSSupport.h"
21 template<typename BaseType>
22 class DSNodeIterator; // Data structure graph traversal iterator
25 //===----------------------------------------------------------------------===//
26 /// DSNode - Data structure node class
28 /// This class represents an untyped memory object of Size bytes. It keeps
29 /// track of any pointers that have been stored into the object as well as the
30 /// different types represented in this object.
33 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
34 /// this is a forwarding node, then this is the number of node handles which
35 /// are still forwarding over us.
37 unsigned NumReferrers;
39 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
40 /// that this node really is. When nodes get folded together, the node to be
41 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
43 DSNodeHandle ForwardNH;
45 /// Size - The current size of the node. This should be equal to the size of
46 /// the current type record.
50 /// ParentGraph - The graph this node is currently embedded into.
54 /// Ty - Keep track of the current outer most type of this object, in addition
55 /// to whether or not it has been indexed like an array or not. If the
56 /// isArray bit is set, the node cannot grow.
58 const Type *Ty; // The type itself...
60 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
61 /// object. Note that if the node is not a multiple of size(void*) bytes
62 /// large, that there is an extra entry for the "remainder" of the node as
63 /// well. For this reason, nodes of 1 byte in size do have one link.
65 std::vector<DSNodeHandle> Links;
67 /// Globals - The list of global values that are merged into this node.
69 std::vector<GlobalValue*> Globals;
71 void operator=(const DSNode &); // DO NOT IMPLEMENT
72 DSNode(const DSNode &); // DO NOT IMPLEMENT
75 ShadowNode = 0, // Nothing is known about this node...
76 AllocaNode = 1 << 0, // This node was allocated with alloca
77 HeapNode = 1 << 1, // This node was allocated with malloc
78 GlobalNode = 1 << 2, // This node was allocated by a global var decl
79 UnknownNode = 1 << 3, // This node points to unknown allocated memory
80 Incomplete = 1 << 4, // This node may not be complete
82 Modified = 1 << 5, // This node is modified in this context
83 Read = 1 << 6, // This node is read in this context
85 Array = 1 << 7, // This node is treated like an array
87 DEAD = 1 << 8, // This node is dead and should not be pointed to
90 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
93 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
94 /// to the nodes in the data structure graph, so it is possible to have nodes
95 /// with a value of 0 for their NodeType.
98 unsigned short NodeType;
101 DSNode(const Type *T, DSGraph *G);
102 DSNode(const DSNode &, DSGraph *G);
106 assert(hasNoReferrers() && "Referrers to dead node exist!");
109 // Iterator for graph interface... Defined in DSGraphTraits.h
110 typedef DSNodeIterator<DSNode> iterator;
111 typedef DSNodeIterator<const DSNode> const_iterator;
112 inline iterator begin();
113 inline iterator end();
114 inline const_iterator begin() const;
115 inline const_iterator end() const;
117 //===--------------------------------------------------
120 /// getSize - Return the maximum number of bytes occupied by this object...
122 unsigned getSize() const { return Size; }
124 // getType - Return the node type of this object...
125 const Type *getType() const { return Ty; }
126 bool isArray() const { return NodeType & Array; }
128 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
130 bool hasNoReferrers() const { return getNumReferrers() == 0; }
132 /// getNumReferrers - This method returns the number of referrers to the
133 /// current node. Note that if this node is a forwarding node, this will
134 /// return the number of nodes forwarding over the node!
135 unsigned getNumReferrers() const { return NumReferrers; }
137 DSGraph *getParentGraph() const { return ParentGraph; }
138 void setParentGraph(DSGraph *G) { ParentGraph = G; }
141 /// getTargetData - Get the target data object used to construct this node.
143 const TargetData &getTargetData() const;
145 /// getForwardNode - This method returns the node that this node is forwarded
147 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
148 void stopForwarding() {
149 assert(!ForwardNH.isNull() &&
150 "Node isn't forwarding, cannot stopForwarding!");
151 ForwardNH.setNode(0);
154 /// hasLink - Return true if this memory object has a link in slot #LinkNo
156 bool hasLink(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!");
161 return Links[Index].getNode();
164 /// getLink - Return the link at the specified offset.
165 DSNodeHandle &getLink(unsigned Offset) {
166 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
167 "Pointer offset not aligned correctly!");
168 unsigned Index = Offset >> DS::PointerShift;
169 assert(Index < Links.size() && "Link index is out of range!");
172 const DSNodeHandle &getLink(unsigned Offset) const {
173 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
174 "Pointer offset not aligned correctly!");
175 unsigned Index = Offset >> DS::PointerShift;
176 assert(Index < Links.size() && "Link index is out of range!");
180 /// getNumLinks - Return the number of links in a node...
182 unsigned getNumLinks() const { return Links.size(); }
184 /// mergeTypeInfo - This method merges the specified type into the current
185 /// node at the specified offset. This may update the current node's type
186 /// record if this gives more information to the node, it may do nothing to
187 /// the node if this information is already known, or it may merge the node
188 /// completely (and return true) if the information is incompatible with what
189 /// is already known.
191 /// This method returns true if the node is completely folded, otherwise
194 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
195 bool FoldIfIncompatible = true);
197 /// foldNodeCompletely - If we determine that this node has some funny
198 /// behavior happening to it that we cannot represent, we fold it down to a
199 /// single, completely pessimistic, node. This node is represented as a
200 /// single byte with a single TypeEntry of "void" with isArray = true.
202 void foldNodeCompletely();
204 /// isNodeCompletelyFolded - Return true if this node has been completely
205 /// folded down to something that can never be expanded, effectively losing
206 /// all of the field sensitivity that may be present in the node.
208 bool isNodeCompletelyFolded() const;
210 /// setLink - Set the link at the specified offset to the specified
211 /// NodeHandle, replacing what was there. It is uncommon to use this method,
212 /// instead one of the higher level methods should be used, below.
214 void setLink(unsigned Offset, const DSNodeHandle &NH) {
215 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
216 "Pointer offset not aligned correctly!");
217 unsigned Index = Offset >> DS::PointerShift;
218 assert(Index < Links.size() && "Link index is out of range!");
222 /// getPointerSize - Return the size of a pointer for the current target.
224 unsigned getPointerSize() const { return DS::PointerSize; }
226 /// addEdgeTo - Add an edge from the current node to the specified node. This
227 /// can cause merging of nodes in the graph.
229 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
231 /// mergeWith - Merge this node and the specified node, moving all links to
232 /// and from the argument node into the current node, deleting the node
233 /// argument. Offset indicates what offset the specified node is to be merged
234 /// into the current node.
236 /// The specified node may be a null pointer (in which case, nothing happens).
238 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
240 /// addGlobal - Add an entry for a global value to the Globals list. This
241 /// also marks the node with the 'G' flag if it does not already have it.
243 void addGlobal(GlobalValue *GV);
244 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
246 /// maskNodeTypes - Apply a mask to the node types bitfield.
248 void maskNodeTypes(unsigned Mask) {
252 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
253 /// is set, hide it from the caller.
254 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
256 bool isAllocaNode() const { return NodeType & AllocaNode; }
257 bool isHeapNode() const { return NodeType & HeapNode; }
258 bool isGlobalNode() const { return NodeType & GlobalNode; }
259 bool isUnknownNode() const { return NodeType & UnknownNode; }
261 bool isModified() const { return NodeType & Modified; }
262 bool isRead() const { return NodeType & Read; }
264 bool isIncomplete() const { return NodeType & Incomplete; }
265 bool isComplete() const { return !isIncomplete(); }
266 bool isDeadNode() const { return NodeType & DEAD; }
268 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
269 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
270 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
271 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
273 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
274 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
275 DSNode *setReadMarker() { NodeType |= Read; return this; }
277 void makeNodeDead() {
279 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
283 /// forwardNode - Mark this node as being obsolete, and all references to it
284 /// should be forwarded to the specified node and offset.
286 void forwardNode(DSNode *To, unsigned Offset);
288 void print(std::ostream &O, const DSGraph *G) const;
291 void assertOK() const;
293 void dropAllReferences() {
295 if (!ForwardNH.isNull())
296 ForwardNH.setNode(0);
299 /// remapLinks - Change all of the Links in the current node according to the
300 /// specified mapping.
301 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
303 /// markReachableNodes - This method recursively traverses the specified
304 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
305 /// adds to the set, which allows it to only traverse visited nodes once.
307 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
310 friend class DSNodeHandle;
312 // static mergeNodes - Helper for mergeWith()
313 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
317 //===----------------------------------------------------------------------===//
318 // Define inline DSNodeHandle functions that depend on the definition of DSNode
320 inline DSNode *DSNodeHandle::getNode() const {
321 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
322 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
323 if (!N || N->ForwardNH.isNull())
326 return HandleForwarding();
329 inline void DSNodeHandle::setNode(DSNode *n) const {
330 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
331 if (N) N->NumReferrers--;
335 if (Offset >= N->Size) {
336 assert((Offset == 0 || N->Size == 1) &&
337 "Pointer to non-collapsed node with invalid offset!");
341 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
342 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
343 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
346 inline bool DSNodeHandle::hasLink(unsigned Num) const {
347 assert(N && "DSNodeHandle does not point to a node yet!");
348 return getNode()->hasLink(Num+Offset);
352 /// getLink - Treat this current node pointer as a pointer to a structure of
353 /// some sort. This method will return the pointer a mem[this+Num]
355 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
356 assert(N && "DSNodeHandle does not point to a node yet!");
357 return getNode()->getLink(Offset+Off);
359 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
360 assert(N && "DSNodeHandle does not point to a node yet!");
361 return getNode()->getLink(Off+Offset);
364 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
365 assert(N && "DSNodeHandle does not point to a node yet!");
366 getNode()->setLink(Off+Offset, NH);
369 /// addEdgeTo - Add an edge from the current node to the specified node. This
370 /// can cause merging of nodes in the graph.
372 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
373 assert(N && "DSNodeHandle does not point to a node yet!");
374 getNode()->addEdgeTo(Off+Offset, Node);
377 /// mergeWith - Merge the logical node pointed to by 'this' with the node
378 /// pointed to by 'N'.
380 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) const {
382 getNode()->mergeWith(Node, Offset);
383 else { // No node to merge with, so just point to Node
385 setNode(Node.getNode());
386 Offset = Node.getOffset();
390 } // End llvm namespace