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 /// Referrers - Keep track of all of the node handles that point to this
23 /// DSNode. These pointers may need to be updated to point to a different
24 /// node if this node gets merged with it.
26 std::vector<DSNodeHandle*> Referrers;
28 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
29 /// object. Note that if the node is not a multiple of size(void*) bytes
30 /// large, that there is an extra entry for the "remainder" of the node as
31 /// well. For this reason, nodes of 1 byte in size do have one link.
33 std::vector<DSNodeHandle> Links;
35 /// Globals - The list of global values that are merged into this node.
37 std::vector<GlobalValue*> Globals;
39 /// Ty - Keep track of the current outer most type of this object, in addition
40 /// to whether or not it has been indexed like an array or not. If the
41 /// isArray bit is set, the node cannot grow.
43 const Type *Ty; // The type itself...
45 /// Size - The current size of the node. This should be equal to the size of
46 /// the current type record.
50 void operator=(const DSNode &); // DO NOT IMPLEMENT
53 ShadowNode = 0, // Nothing is known about this node...
54 AllocaNode = 1 << 0, // This node was allocated with alloca
55 HeapNode = 1 << 1, // This node was allocated with malloc
56 GlobalNode = 1 << 2, // This node was allocated by a global var decl
57 UnknownNode = 1 << 3, // This node points to unknown allocated memory
58 Incomplete = 1 << 4, // This node may not be complete
59 Modified = 1 << 5, // This node is modified in this context
60 Read = 1 << 6, // This node is read in this context
61 Array = 1 << 7, // This node is treated like an array
63 DEAD = 1 << 8, // This node is dead and should not be pointed to
67 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
68 /// to the nodes in the data structure graph, so it is possible to have nodes
69 /// with a value of 0 for their NodeType. Scalar and Alloca markers go away
70 /// when function graphs are inlined.
72 unsigned short NodeType;
74 DSNode(enum NodeTy NT, const Type *T);
75 DSNode(const DSNode &);
79 assert(Referrers.empty() && "Referrers to dead node exist!");
82 // Iterator for graph interface... Defined in DSGraphTraits.h
83 typedef DSNodeIterator<DSNode> iterator;
84 typedef DSNodeIterator<const DSNode> const_iterator;
85 inline iterator begin();
86 inline iterator end();
87 inline const_iterator begin() const;
88 inline const_iterator end() const;
90 //===--------------------------------------------------
93 /// getSize - Return the maximum number of bytes occupied by this object...
95 unsigned getSize() const { return Size; }
97 // getType - Return the node type of this object...
98 const Type *getType() const { return Ty; }
99 bool isArray() const { return NodeType & Array; }
101 /// getReferrers - Return a list of the pointers to this node...
103 const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
105 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
107 bool hasNoReferrers() const { return Referrers.empty(); }
109 /// isModified - Return true if this node may be modified in this context
111 bool isModified() const { return (NodeType & Modified) != 0; }
113 /// isRead - Return true if this node may be read in this context
115 bool isRead() const { return (NodeType & Read) != 0; }
118 /// hasLink - Return true if this memory object has a link in slot #LinkNo
120 bool hasLink(unsigned Offset) const {
121 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
122 "Pointer offset not aligned correctly!");
123 unsigned Index = Offset >> DS::PointerShift;
124 assert(Index < Links.size() && "Link index is out of range!");
125 return Links[Index].getNode();
128 /// getLink - Return the link at the specified offset.
129 DSNodeHandle &getLink(unsigned Offset) {
130 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
131 "Pointer offset not aligned correctly!");
132 unsigned Index = Offset >> DS::PointerShift;
133 assert(Index < Links.size() && "Link index is out of range!");
136 const DSNodeHandle &getLink(unsigned Offset) const {
137 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
138 "Pointer offset not aligned correctly!");
139 unsigned Index = Offset >> DS::PointerShift;
140 assert(Index < Links.size() && "Link index is out of range!");
144 /// getNumLinks - Return the number of links in a node...
146 unsigned getNumLinks() const { return Links.size(); }
148 /// mergeTypeInfo - This method merges the specified type into the current
149 /// node at the specified offset. This may update the current node's type
150 /// record if this gives more information to the node, it may do nothing to
151 /// the node if this information is already known, or it may merge the node
152 /// completely (and return true) if the information is incompatible with what
153 /// is already known.
155 /// This method returns true if the node is completely folded, otherwise
158 bool mergeTypeInfo(const Type *Ty, unsigned Offset);
160 /// foldNodeCompletely - If we determine that this node has some funny
161 /// behavior happening to it that we cannot represent, we fold it down to a
162 /// single, completely pessimistic, node. This node is represented as a
163 /// single byte with a single TypeEntry of "void" with isArray = true.
165 void foldNodeCompletely();
167 /// isNodeCompletelyFolded - Return true if this node has been completely
168 /// folded down to something that can never be expanded, effectively losing
169 /// all of the field sensitivity that may be present in the node.
171 bool isNodeCompletelyFolded() const;
173 /// setLink - Set the link at the specified offset to the specified
174 /// NodeHandle, replacing what was there. It is uncommon to use this method,
175 /// instead one of the higher level methods should be used, below.
177 void setLink(unsigned Offset, const DSNodeHandle &NH) {
178 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
179 "Pointer offset not aligned correctly!");
180 unsigned Index = Offset >> DS::PointerShift;
181 assert(Index < Links.size() && "Link index is out of range!");
185 /// getPointerSize - Return the size of a pointer for the current target.
187 unsigned getPointerSize() const { return DS::PointerSize; }
189 /// addEdgeTo - Add an edge from the current node to the specified node. This
190 /// can cause merging of nodes in the graph.
192 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
194 /// mergeWith - Merge this node and the specified node, moving all links to
195 /// and from the argument node into the current node, deleting the node
196 /// argument. Offset indicates what offset the specified node is to be merged
197 /// into the current node.
199 /// The specified node may be a null pointer (in which case, nothing happens).
201 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
203 /// addGlobal - Add an entry for a global value to the Globals list. This
204 /// also marks the node with the 'G' flag if it does not already have it.
206 void addGlobal(GlobalValue *GV);
207 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
208 std::vector<GlobalValue*> &getGlobals() { return Globals; }
210 void print(std::ostream &O, const DSGraph *G) const;
213 void dropAllReferences() {
217 /// remapLinks - Change all of the Links in the current node according to the
218 /// specified mapping.
219 void remapLinks(std::map<const DSNode*, DSNodeHandle> &OldNodeMap);
222 friend class DSNodeHandle;
223 // addReferrer - Keep the referrer set up to date...
224 void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
225 void removeReferrer(DSNodeHandle *H);
229 //===----------------------------------------------------------------------===//
230 // Define inline DSNodeHandle functions that depend on the definition of DSNode
232 inline void DSNodeHandle::setNode(DSNode *n) {
233 if (N) N->removeReferrer(this);
235 if (N) N->addReferrer(this);
236 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
239 inline bool DSNodeHandle::hasLink(unsigned Num) const {
240 assert(N && "DSNodeHandle does not point to a node yet!");
241 return N->hasLink(Num+Offset);
245 /// getLink - Treat this current node pointer as a pointer to a structure of
246 /// some sort. This method will return the pointer a mem[this+Num]
248 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
249 assert(N && "DSNodeHandle does not point to a node yet!");
250 return N->getLink(Offset+Off);
252 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
253 assert(N && "DSNodeHandle does not point to a node yet!");
254 return N->getLink(Off+Offset);
257 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
258 assert(N && "DSNodeHandle does not point to a node yet!");
259 N->setLink(Off+Offset, NH);
262 /// addEdgeTo - Add an edge from the current node to the specified node. This
263 /// can cause merging of nodes in the graph.
265 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
266 assert(N && "DSNodeHandle does not point to a node yet!");
267 N->addEdgeTo(Off+Offset, Node);
270 /// mergeWith - Merge the logical node pointed to by 'this' with the node
271 /// pointed to by 'N'.
273 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
275 N->mergeWith(Node, Offset);
276 else // No node to merge with, so just point to Node
280 inline void DSNodeHandle::swap(DSNodeHandle &NH) {
281 std::swap(Offset, NH.Offset);
284 N->removeReferrer(this);
288 N->removeReferrer(&NH);
289 N->addReferrer(this);