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 ctor - Create a node of the specified type, inserting it into the
103 DSNode(const Type *T, DSGraph *G);
105 /// DSNode "copy ctor" - Copy the specified node, inserting it into the
106 /// specified graph. If NullLinks is true, then null out all of the links,
107 /// but keep the same number of them. This can be used for efficiency if the
108 /// links are just going to be clobbered anyway.
109 DSNode(const DSNode &, DSGraph *G, bool NullLinks = false);
113 assert(hasNoReferrers() && "Referrers to dead node exist!");
116 // Iterator for graph interface... Defined in DSGraphTraits.h
117 typedef DSNodeIterator<DSNode> iterator;
118 typedef DSNodeIterator<const DSNode> const_iterator;
119 inline iterator begin();
120 inline iterator end();
121 inline const_iterator begin() const;
122 inline const_iterator end() const;
124 //===--------------------------------------------------
127 /// getSize - Return the maximum number of bytes occupied by this object...
129 unsigned getSize() const { return Size; }
131 // getType - Return the node type of this object...
132 const Type *getType() const { return Ty; }
133 bool isArray() const { return NodeType & Array; }
135 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
137 bool hasNoReferrers() const { return getNumReferrers() == 0; }
139 /// getNumReferrers - This method returns the number of referrers to the
140 /// current node. Note that if this node is a forwarding node, this will
141 /// return the number of nodes forwarding over the node!
142 unsigned getNumReferrers() const { return NumReferrers; }
144 DSGraph *getParentGraph() const { return ParentGraph; }
145 void setParentGraph(DSGraph *G) { ParentGraph = G; }
148 /// getTargetData - Get the target data object used to construct this node.
150 const TargetData &getTargetData() const;
152 /// getForwardNode - This method returns the node that this node is forwarded
154 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
155 void stopForwarding() {
156 assert(!ForwardNH.isNull() &&
157 "Node isn't forwarding, cannot stopForwarding!");
158 ForwardNH.setNode(0);
161 /// hasLink - Return true if this memory object has a link in slot #LinkNo
163 bool hasLink(unsigned Offset) const {
164 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
165 "Pointer offset not aligned correctly!");
166 unsigned Index = Offset >> DS::PointerShift;
167 assert(Index < Links.size() && "Link index is out of range!");
168 return Links[Index].getNode();
171 /// getLink - Return the link at the specified offset.
172 DSNodeHandle &getLink(unsigned Offset) {
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!");
179 const DSNodeHandle &getLink(unsigned Offset) const {
180 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
181 "Pointer offset not aligned correctly!");
182 unsigned Index = Offset >> DS::PointerShift;
183 assert(Index < Links.size() && "Link index is out of range!");
187 /// getNumLinks - Return the number of links in a node...
189 unsigned getNumLinks() const { return Links.size(); }
191 /// mergeTypeInfo - This method merges the specified type into the current
192 /// node at the specified offset. This may update the current node's type
193 /// record if this gives more information to the node, it may do nothing to
194 /// the node if this information is already known, or it may merge the node
195 /// completely (and return true) if the information is incompatible with what
196 /// is already known.
198 /// This method returns true if the node is completely folded, otherwise
201 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
202 bool FoldIfIncompatible = true);
204 /// foldNodeCompletely - If we determine that this node has some funny
205 /// behavior happening to it that we cannot represent, we fold it down to a
206 /// single, completely pessimistic, node. This node is represented as a
207 /// single byte with a single TypeEntry of "void" with isArray = true.
209 void foldNodeCompletely();
211 /// isNodeCompletelyFolded - Return true if this node has been completely
212 /// folded down to something that can never be expanded, effectively losing
213 /// all of the field sensitivity that may be present in the node.
215 bool isNodeCompletelyFolded() const;
217 /// setLink - Set the link at the specified offset to the specified
218 /// NodeHandle, replacing what was there. It is uncommon to use this method,
219 /// instead one of the higher level methods should be used, below.
221 void setLink(unsigned Offset, const DSNodeHandle &NH) {
222 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
223 "Pointer offset not aligned correctly!");
224 unsigned Index = Offset >> DS::PointerShift;
225 assert(Index < Links.size() && "Link index is out of range!");
229 /// getPointerSize - Return the size of a pointer for the current target.
231 unsigned getPointerSize() const { return DS::PointerSize; }
233 /// addEdgeTo - Add an edge from the current node to the specified node. This
234 /// can cause merging of nodes in the graph.
236 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
238 /// mergeWith - Merge this node and the specified node, moving all links to
239 /// and from the argument node into the current node, deleting the node
240 /// argument. Offset indicates what offset the specified node is to be merged
241 /// into the current node.
243 /// The specified node may be a null pointer (in which case, nothing happens).
245 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
247 /// addGlobal - Add an entry for a global value to the Globals list. This
248 /// also marks the node with the 'G' flag if it does not already have it.
250 void addGlobal(GlobalValue *GV);
251 void mergeGlobals(const std::vector<GlobalValue*> &RHS);
252 const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
254 typedef std::vector<GlobalValue*>::const_iterator global_iterator;
255 global_iterator global_begin() const { return Globals.begin(); }
256 global_iterator global_end() const { return Globals.end(); }
259 /// maskNodeTypes - Apply a mask to the node types bitfield.
261 void maskNodeTypes(unsigned Mask) {
265 void mergeNodeFlags(unsigned RHS) {
269 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
270 /// is set, hide it from the caller.
271 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
273 bool isAllocaNode() const { return NodeType & AllocaNode; }
274 bool isHeapNode() const { return NodeType & HeapNode; }
275 bool isGlobalNode() const { return NodeType & GlobalNode; }
276 bool isUnknownNode() const { return NodeType & UnknownNode; }
278 bool isModified() const { return NodeType & Modified; }
279 bool isRead() const { return NodeType & Read; }
281 bool isIncomplete() const { return NodeType & Incomplete; }
282 bool isComplete() const { return !isIncomplete(); }
283 bool isDeadNode() const { return NodeType & DEAD; }
285 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
286 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
287 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
288 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
290 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
291 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
292 DSNode *setReadMarker() { NodeType |= Read; return this; }
294 void makeNodeDead() {
296 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
300 /// forwardNode - Mark this node as being obsolete, and all references to it
301 /// should be forwarded to the specified node and offset.
303 void forwardNode(DSNode *To, unsigned Offset);
305 void print(std::ostream &O, const DSGraph *G) const;
308 void assertOK() const;
310 void dropAllReferences() {
312 if (!ForwardNH.isNull())
313 ForwardNH.setNode(0);
316 /// remapLinks - Change all of the Links in the current node according to the
317 /// specified mapping.
318 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
320 /// markReachableNodes - This method recursively traverses the specified
321 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
322 /// adds to the set, which allows it to only traverse visited nodes once.
324 void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
327 friend class DSNodeHandle;
329 // static mergeNodes - Helper for mergeWith()
330 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
334 //===----------------------------------------------------------------------===//
335 // Define inline DSNodeHandle functions that depend on the definition of DSNode
337 inline DSNode *DSNodeHandle::getNode() const {
338 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
339 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
340 if (N == 0 || N->ForwardNH.isNull())
343 return HandleForwarding();
346 inline void DSNodeHandle::setNode(DSNode *n) const {
347 assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
348 if (N) N->NumReferrers--;
352 if (Offset >= N->Size) {
353 assert((Offset == 0 || N->Size == 1) &&
354 "Pointer to non-collapsed node with invalid offset!");
358 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
359 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
360 !N->ForwardNH.isNull()) && "Node handle offset out of range!");
363 inline bool DSNodeHandle::hasLink(unsigned Num) const {
364 assert(N && "DSNodeHandle does not point to a node yet!");
365 return getNode()->hasLink(Num+Offset);
369 /// getLink - Treat this current node pointer as a pointer to a structure of
370 /// some sort. This method will return the pointer a mem[this+Num]
372 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
373 assert(N && "DSNodeHandle does not point to a node yet!");
374 return getNode()->getLink(Offset+Off);
376 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
377 assert(N && "DSNodeHandle does not point to a node yet!");
378 return getNode()->getLink(Off+Offset);
381 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
382 assert(N && "DSNodeHandle does not point to a node yet!");
383 getNode()->setLink(Off+Offset, NH);
386 /// addEdgeTo - Add an edge from the current node to the specified node. This
387 /// can cause merging of nodes in the graph.
389 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
390 assert(N && "DSNodeHandle does not point to a node yet!");
391 getNode()->addEdgeTo(Off+Offset, Node);
394 /// mergeWith - Merge the logical node pointed to by 'this' with the node
395 /// pointed to by 'N'.
397 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) const {
399 getNode()->mergeWith(Node, Offset);
400 else { // No node to merge with, so just point to Node
402 setNode(Node.getNode());
403 Offset = Node.getOffset();
407 } // End llvm namespace