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 /// Next, Prev - These instance variables are used to keep the node on a
46 /// doubly-linked ilist in the DSGraph.
48 friend class ilist_traits<DSNode>;
50 /// Size - The current size of the node. This should be equal to the size of
51 /// the current type record.
55 /// ParentGraph - The graph this node is currently embedded into.
59 /// Ty - Keep track of the current outer most type of this object, in addition
60 /// to whether or not it has been indexed like an array or not. If the
61 /// isArray bit is set, the node cannot grow.
63 const Type *Ty; // The type itself...
65 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
66 /// object. Note that if the node is not a multiple of size(void*) bytes
67 /// large, that there is an extra entry for the "remainder" of the node as
68 /// well. For this reason, nodes of 1 byte in size do have one link.
70 std::vector<DSNodeHandle> Links;
72 /// Globals - The list of global values that are merged into this node.
74 std::vector<GlobalValue*> Globals;
76 void operator=(const DSNode &); // DO NOT IMPLEMENT
77 DSNode(const DSNode &); // DO NOT IMPLEMENT
80 ShadowNode = 0, // Nothing is known about this node...
81 AllocaNode = 1 << 0, // This node was allocated with alloca
82 HeapNode = 1 << 1, // This node was allocated with malloc
83 GlobalNode = 1 << 2, // This node was allocated by a global var decl
84 UnknownNode = 1 << 3, // This node points to unknown allocated memory
85 Incomplete = 1 << 4, // This node may not be complete
87 Modified = 1 << 5, // This node is modified in this context
88 Read = 1 << 6, // This node is read in this context
90 Array = 1 << 7, // This node is treated like an array
92 DEAD = 1 << 8, // This node is dead and should not be pointed to
95 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
98 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
99 /// to the nodes in the data structure graph, so it is possible to have nodes
100 /// with a value of 0 for their NodeType.
103 unsigned short NodeType;
106 /// DSNode ctor - Create a node of the specified type, inserting it into the
108 DSNode(const Type *T, DSGraph *G);
110 /// DSNode "copy ctor" - Copy the specified node, inserting it into the
111 /// specified graph. If NullLinks is true, then null out all of the links,
112 /// but keep the same number of them. This can be used for efficiency if the
113 /// links are just going to be clobbered anyway.
114 DSNode(const DSNode &, DSGraph *G, bool NullLinks = false);
118 assert(hasNoReferrers() && "Referrers to dead node exist!");
121 // Iterator for graph interface... Defined in DSGraphTraits.h
122 typedef DSNodeIterator<DSNode> iterator;
123 typedef DSNodeIterator<const DSNode> const_iterator;
124 inline iterator begin();
125 inline iterator end();
126 inline const_iterator begin() const;
127 inline const_iterator end() const;
129 //===--------------------------------------------------
132 /// getSize - Return the maximum number of bytes occupied by this object...
134 unsigned getSize() const { return Size; }
136 // getType - Return the node type of this object...
137 const Type *getType() const { return Ty; }
138 bool isArray() const { return NodeType & Array; }
140 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
142 bool hasNoReferrers() const { return getNumReferrers() == 0; }
144 /// getNumReferrers - This method returns the number of referrers to the
145 /// current node. Note that if this node is a forwarding node, this will
146 /// return the number of nodes forwarding over the node!
147 unsigned getNumReferrers() const { return NumReferrers; }
149 DSGraph *getParentGraph() const { return ParentGraph; }
150 void setParentGraph(DSGraph *G) { ParentGraph = G; }
153 /// getTargetData - Get the target data object used to construct this node.
155 const TargetData &getTargetData() const;
157 /// getForwardNode - This method returns the node that this node is forwarded
159 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
161 /// isForwarding - Return true if this node is forwarding to another.
162 bool isForwarding() const { return !ForwardNH.isNull(); }
164 void stopForwarding() {
165 assert(isForwarding() &&
166 "Node isn't forwarding, cannot stopForwarding!");
167 ForwardNH.setNode(0);
170 /// hasLink - Return true if this memory object has a link in slot #LinkNo
172 bool hasLink(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!");
177 return Links[Index].getNode();
180 /// getLink - Return the link at the specified offset.
181 DSNodeHandle &getLink(unsigned Offset) {
182 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
183 "Pointer offset not aligned correctly!");
184 unsigned Index = Offset >> DS::PointerShift;
185 assert(Index < Links.size() && "Link index is out of range!");
188 const DSNodeHandle &getLink(unsigned Offset) const {
189 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
190 "Pointer offset not aligned correctly!");
191 unsigned Index = Offset >> DS::PointerShift;
192 assert(Index < Links.size() && "Link index is out of range!");
196 /// getNumLinks - Return the number of links in a node...
198 unsigned getNumLinks() const { return Links.size(); }
200 /// mergeTypeInfo - This method merges the specified type into the current
201 /// node at the specified offset. This may update the current node's type
202 /// record if this gives more information to the node, it may do nothing to
203 /// the node if this information is already known, or it may merge the node
204 /// completely (and return true) if the information is incompatible with what
205 /// is already known.
207 /// This method returns true if the node is completely folded, otherwise
210 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
211 bool FoldIfIncompatible = true);
213 /// foldNodeCompletely - If we determine that this node has some funny
214 /// behavior happening to it that we cannot represent, we fold it down to a
215 /// single, completely pessimistic, node. This node is represented as a
216 /// single byte with a single TypeEntry of "void" with isArray = true.
218 void foldNodeCompletely();
220 /// isNodeCompletelyFolded - Return true if this node has been completely
221 /// folded down to something that can never be expanded, effectively losing
222 /// all of the field sensitivity that may be present in the node.
224 bool isNodeCompletelyFolded() const;
226 /// setLink - Set the link at the specified offset to the specified
227 /// NodeHandle, replacing what was there. It is uncommon to use this method,
228 /// instead one of the higher level methods should be used, below.
230 void setLink(unsigned Offset, const DSNodeHandle &NH) {
231 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
232 "Pointer offset not aligned correctly!");
233 unsigned Index = Offset >> DS::PointerShift;
234 assert(Index < Links.size() && "Link index is out of range!");
238 /// getPointerSize - Return the size of a pointer for the current target.
240 unsigned getPointerSize() const { return DS::PointerSize; }
242 /// addEdgeTo - Add an edge from the current node to the specified node. This
243 /// can cause merging of nodes in the graph.
245 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
247 /// mergeWith - Merge this node and the specified node, moving all links to
248 /// and from the argument node into the current node, deleting the node
249 /// argument. Offset indicates what offset the specified node is to be merged
250 /// into the current node.
252 /// The specified node may be a null pointer (in which case, nothing happens).
254 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
256 /// addGlobal - Add an entry for a global value to the Globals list. This
257 /// also marks the node with the 'G' flag if it does not already have it.
259 void addGlobal(GlobalValue *GV);
260 void mergeGlobals(const std::vector<GlobalValue*> &RHS);
261 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
263 typedef std::vector<GlobalValue*>::const_iterator global_iterator;
264 global_iterator global_begin() const { return Globals.begin(); }
265 global_iterator global_end() const { return Globals.end(); }
268 /// maskNodeTypes - Apply a mask to the node types bitfield.
270 void maskNodeTypes(unsigned Mask) {
274 void mergeNodeFlags(unsigned RHS) {
278 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
279 /// is set, hide it from the caller.
280 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
282 bool isAllocaNode() const { return NodeType & AllocaNode; }
283 bool isHeapNode() const { return NodeType & HeapNode; }
284 bool isGlobalNode() const { return NodeType & GlobalNode; }
285 bool isUnknownNode() const { return NodeType & UnknownNode; }
287 bool isModified() const { return NodeType & Modified; }
288 bool isRead() const { return NodeType & Read; }
290 bool isIncomplete() const { return NodeType & Incomplete; }
291 bool isComplete() const { return !isIncomplete(); }
292 bool isDeadNode() const { return NodeType & DEAD; }
294 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
295 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
296 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
297 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
299 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
300 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
301 DSNode *setReadMarker() { NodeType |= Read; return this; }
303 void makeNodeDead() {
305 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
309 /// forwardNode - Mark this node as being obsolete, and all references to it
310 /// should be forwarded to the specified node and offset.
312 void forwardNode(DSNode *To, unsigned Offset);
314 void print(std::ostream &O, const DSGraph *G) const;
317 void assertOK() const;
319 void dropAllReferences() {
322 ForwardNH.setNode(0);
325 /// remapLinks - Change all of the Links in the current node according to the
326 /// specified mapping.
327 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
329 /// markReachableNodes - This method recursively traverses the specified
330 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
331 /// adds to the set, which allows it to only traverse visited nodes once.
333 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
336 friend class DSNodeHandle;
338 // static mergeNodes - Helper for mergeWith()
339 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
342 //===----------------------------------------------------------------------===//
343 // Define the ilist_traits specialization for the DSGraph ilist.
346 struct ilist_traits<DSNode> {
347 static DSNode *getPrev(const DSNode *N) { return N->Prev; }
348 static DSNode *getNext(const DSNode *N) { return N->Next; }
350 static void setPrev(DSNode *N, DSNode *Prev) { N->Prev = Prev; }
351 static void setNext(DSNode *N, DSNode *Next) { N->Next = Next; }
353 static DSNode *createNode() { return new DSNode(0,0); }
354 //static DSNode *createNode(const DSNode &V) { return new DSNode(V); }
357 void addNodeToList(DSNode *NTy) {}
358 void removeNodeFromList(DSNode *NTy) {}
359 void transferNodesFromList(iplist<DSNode, ilist_traits> &L2,
360 ilist_iterator<DSNode> first,
361 ilist_iterator<DSNode> last) {}
365 struct ilist_traits<const DSNode> : public ilist_traits<DSNode> {};
367 //===----------------------------------------------------------------------===//
368 // Define inline DSNodeHandle functions that depend on the definition of DSNode
370 inline DSNode *DSNodeHandle::getNode() const {
371 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
372 N->isForwarding()) && "Node handle offset out of range!");
373 if (N == 0 || !N->isForwarding())
376 return HandleForwarding();
379 inline void DSNodeHandle::setNode(DSNode *n) const {
380 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
381 if (N) N->NumReferrers--;
385 if (Offset >= N->Size) {
386 assert((Offset == 0 || N->Size == 1) &&
387 "Pointer to non-collapsed node with invalid offset!");
391 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
392 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
393 N->isForwarding()) && "Node handle offset out of range!");
396 inline bool DSNodeHandle::hasLink(unsigned Num) const {
397 assert(N && "DSNodeHandle does not point to a node yet!");
398 return getNode()->hasLink(Num+Offset);
402 /// getLink - Treat this current node pointer as a pointer to a structure of
403 /// some sort. This method will return the pointer a mem[this+Num]
405 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
406 assert(N && "DSNodeHandle does not point to a node yet!");
407 return getNode()->getLink(Offset+Off);
409 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
410 assert(N && "DSNodeHandle does not point to a node yet!");
411 return getNode()->getLink(Off+Offset);
414 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
415 assert(N && "DSNodeHandle does not point to a node yet!");
416 getNode()->setLink(Off+Offset, NH);
419 /// addEdgeTo - Add an edge from the current node to the specified node. This
420 /// can cause merging of nodes in the graph.
422 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
423 assert(N && "DSNodeHandle does not point to a node yet!");
424 getNode()->addEdgeTo(Off+Offset, Node);
427 /// mergeWith - Merge the logical node pointed to by 'this' with the node
428 /// pointed to by 'N'.
430 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) const {
432 getNode()->mergeWith(Node, Offset);
433 else { // No node to merge with, so just point to Node
435 setNode(Node.getNode());
436 Offset = Node.getOffset();
440 } // End llvm namespace