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/DataStructure/DSSupport.h"
18 #include "llvm/ADT/hash_map"
22 template<typename BaseType>
23 class DSNodeIterator; // Data structure graph traversal iterator
26 //===----------------------------------------------------------------------===//
27 /// DSNode - Data structure node class
29 /// This class represents an untyped memory object of Size bytes. It keeps
30 /// track of any pointers that have been stored into the object as well as the
31 /// different types represented in this object.
34 /// NumReferrers - The number of DSNodeHandles pointing to this node... if
35 /// this is a forwarding node, then this is the number of node handles which
36 /// are still forwarding over us.
38 unsigned NumReferrers;
40 /// ForwardNH - This NodeHandle contain the node (and offset into the node)
41 /// that this node really is. When nodes get folded together, the node to be
42 /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
45 DSNodeHandle ForwardNH;
47 /// Next, Prev - These instance variables are used to keep the node on a
48 /// doubly-linked ilist in the DSGraph.
51 friend struct ilist_traits<DSNode>;
53 /// Size - The current size of the node. This should be equal to the size of
54 /// the current type record.
58 /// ParentGraph - The graph this node is currently embedded into.
62 /// Ty - Keep track of the current outer most type of this object, in addition
63 /// to whether or not it has been indexed like an array or not. If the
64 /// isArray bit is set, the node cannot grow.
66 const Type *Ty; // The type itself...
68 /// Links - Contains one entry for every sizeof(void*) bytes in this memory
69 /// object. Note that if the node is not a multiple of size(void*) bytes
70 /// large, that there is an extra entry for the "remainder" of the node as
71 /// well. For this reason, nodes of 1 byte in size do have one link.
73 std::vector<DSNodeHandle> Links;
75 /// Globals - The list of global values that are merged into this node.
77 std::vector<GlobalValue*> Globals;
79 void operator=(const DSNode &); // DO NOT IMPLEMENT
80 DSNode(const DSNode &); // DO NOT IMPLEMENT
83 ShadowNode = 0, // Nothing is known about this node...
84 AllocaNode = 1 << 0, // This node was allocated with alloca
85 HeapNode = 1 << 1, // This node was allocated with malloc
86 GlobalNode = 1 << 2, // This node was allocated by a global var decl
87 UnknownNode = 1 << 3, // This node points to unknown allocated memory
88 Incomplete = 1 << 4, // This node may not be complete
90 Modified = 1 << 5, // This node is modified in this context
91 Read = 1 << 6, // This node is read in this context
93 Array = 1 << 7, // This node is treated like an array
95 DEAD = 1 << 8, // This node is dead and should not be pointed to
98 Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode
101 /// NodeType - A union of the above bits. "Shadow" nodes do not add any flags
102 /// to the nodes in the data structure graph, so it is possible to have nodes
103 /// with a value of 0 for their NodeType.
106 unsigned short NodeType;
109 /// DSNode ctor - Create a node of the specified type, inserting it into the
112 DSNode(const Type *T, DSGraph *G);
114 /// DSNode "copy ctor" - Copy the specified node, inserting it into the
115 /// specified graph. If NullLinks is true, then null out all of the links,
116 /// but keep the same number of them. This can be used for efficiency if the
117 /// links are just going to be clobbered anyway.
119 DSNode(const DSNode &, DSGraph *G, bool NullLinks = false);
123 assert(hasNoReferrers() && "Referrers to dead node exist!");
126 // Iterator for graph interface... Defined in DSGraphTraits.h
127 typedef DSNodeIterator<DSNode> iterator;
128 typedef DSNodeIterator<const DSNode> const_iterator;
129 inline iterator begin();
130 inline iterator end();
131 inline const_iterator begin() const;
132 inline const_iterator end() const;
134 //===--------------------------------------------------
137 /// getSize - Return the maximum number of bytes occupied by this object...
139 unsigned getSize() const { return Size; }
141 /// getType - Return the node type of this object...
143 const Type *getType() const { return Ty; }
145 bool isArray() const { return NodeType & Array; }
147 /// hasNoReferrers - Return true if nothing is pointing to this node at all.
149 bool hasNoReferrers() const { return getNumReferrers() == 0; }
151 /// getNumReferrers - This method returns the number of referrers to the
152 /// current node. Note that if this node is a forwarding node, this will
153 /// return the number of nodes forwarding over the node!
154 unsigned getNumReferrers() const { return NumReferrers; }
156 DSGraph *getParentGraph() const { return ParentGraph; }
157 void setParentGraph(DSGraph *G) { ParentGraph = G; }
160 /// getTargetData - Get the target data object used to construct this node.
162 const TargetData &getTargetData() const;
164 /// getForwardNode - This method returns the node that this node is forwarded
167 DSNode *getForwardNode() const { return ForwardNH.getNode(); }
169 /// isForwarding - Return true if this node is forwarding to another.
171 bool isForwarding() const { return !ForwardNH.isNull(); }
173 /// stopForwarding - When the last reference to this forwarding node has been
174 /// dropped, delete the node.
176 void stopForwarding() {
177 assert(isForwarding() &&
178 "Node isn't forwarding, cannot stopForwarding()!");
179 ForwardNH.setTo(0, 0);
180 assert(ParentGraph == 0 &&
181 "Forwarding nodes must have been removed from graph!");
185 /// hasLink - Return true if this memory object has a link in slot LinkNo
187 bool hasLink(unsigned Offset) const {
188 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
189 "Pointer offset not aligned correctly!");
190 unsigned Index = Offset >> DS::PointerShift;
191 assert(Index < Links.size() && "Link index is out of range!");
192 return Links[Index].getNode();
195 /// getLink - Return the link at the specified offset.
197 DSNodeHandle &getLink(unsigned Offset) {
198 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
199 "Pointer offset not aligned correctly!");
200 unsigned Index = Offset >> DS::PointerShift;
201 assert(Index < Links.size() && "Link index is out of range!");
204 const DSNodeHandle &getLink(unsigned Offset) const {
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 /// getNumLinks - Return the number of links in a node...
214 unsigned getNumLinks() const { return Links.size(); }
216 /// edge_* - Provide iterators for accessing outgoing edges. Some outgoing
217 /// edges may be null.
218 typedef std::vector<DSNodeHandle>::iterator edge_iterator;
219 typedef std::vector<DSNodeHandle>::const_iterator const_edge_iterator;
220 edge_iterator edge_begin() { return Links.begin(); }
221 edge_iterator edge_end() { return Links.end(); }
222 const_edge_iterator edge_begin() const { return Links.begin(); }
223 const_edge_iterator edge_end() const { return Links.end(); }
226 /// mergeTypeInfo - This method merges the specified type into the current
227 /// node at the specified offset. This may update the current node's type
228 /// record if this gives more information to the node, it may do nothing to
229 /// the node if this information is already known, or it may merge the node
230 /// completely (and return true) if the information is incompatible with what
231 /// is already known.
233 /// This method returns true if the node is completely folded, otherwise
236 bool mergeTypeInfo(const Type *Ty, unsigned Offset,
237 bool FoldIfIncompatible = true);
239 /// foldNodeCompletely - If we determine that this node has some funny
240 /// behavior happening to it that we cannot represent, we fold it down to a
241 /// single, completely pessimistic, node. This node is represented as a
242 /// single byte with a single TypeEntry of "void" with isArray = true.
244 void foldNodeCompletely();
246 /// isNodeCompletelyFolded - Return true if this node has been completely
247 /// folded down to something that can never be expanded, effectively losing
248 /// all of the field sensitivity that may be present in the node.
250 bool isNodeCompletelyFolded() const;
252 /// setLink - Set the link at the specified offset to the specified
253 /// NodeHandle, replacing what was there. It is uncommon to use this method,
254 /// instead one of the higher level methods should be used, below.
256 void setLink(unsigned Offset, const DSNodeHandle &NH) {
257 assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
258 "Pointer offset not aligned correctly!");
259 unsigned Index = Offset >> DS::PointerShift;
260 assert(Index < Links.size() && "Link index is out of range!");
264 /// getPointerSize - Return the size of a pointer for the current target.
266 unsigned getPointerSize() const { return DS::PointerSize; }
268 /// addEdgeTo - Add an edge from the current node to the specified node. This
269 /// can cause merging of nodes in the graph.
271 void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
273 /// mergeWith - Merge this node and the specified node, moving all links to
274 /// and from the argument node into the current node, deleting the node
275 /// argument. Offset indicates what offset the specified node is to be merged
276 /// into the current node.
278 /// The specified node may be a null pointer (in which case, nothing happens).
280 void mergeWith(const DSNodeHandle &NH, unsigned Offset);
282 /// addGlobal - Add an entry for a global value to the Globals list. This
283 /// also marks the node with the 'G' flag if it does not already have it.
285 void addGlobal(GlobalValue *GV);
287 /// removeGlobal - Remove the specified global that is explicitly in the
289 void removeGlobal(GlobalValue *GV);
291 void mergeGlobals(const std::vector<GlobalValue*> &RHS);
292 void clearGlobals() { std::vector<GlobalValue*>().swap(Globals); }
294 /// getGlobalsList - Return the set of global leaders that are represented by
295 /// this node. Note that globals that are in this equivalence class but are
296 /// not leaders are not returned: for that, use addFullGlobalsList().
297 const std::vector<GlobalValue*> &getGlobalsList() const { return Globals; }
299 /// addFullGlobalsList - Compute the full set of global values that are
300 /// represented by this node. Unlike getGlobalsList(), this requires fair
301 /// amount of work to compute, so don't treat this method call as free.
302 void addFullGlobalsList(std::vector<GlobalValue*> &List) const;
304 /// addFullFunctionList - Identical to addFullGlobalsList, but only return the
305 /// functions in the full list.
306 void addFullFunctionList(std::vector<Function*> &List) const;
308 /// globals_iterator/begin/end - Provide iteration methods over the global
309 /// value leaders set that is merged into this node. Like the getGlobalsList
310 /// method, these iterators do not return globals that are part of the
311 /// equivalence classes for globals in this node, but aren't leaders.
312 typedef std::vector<GlobalValue*>::const_iterator globals_iterator;
313 globals_iterator globals_begin() const { return Globals.begin(); }
314 globals_iterator globals_end() const { return Globals.end(); }
317 /// maskNodeTypes - Apply a mask to the node types bitfield.
319 void maskNodeTypes(unsigned Mask) {
323 void mergeNodeFlags(unsigned RHS) {
327 /// getNodeFlags - Return all of the flags set on the node. If the DEAD flag
328 /// is set, hide it from the caller.
330 unsigned getNodeFlags() const { return NodeType & ~DEAD; }
332 bool isAllocaNode() const { return NodeType & AllocaNode; }
333 bool isHeapNode() const { return NodeType & HeapNode; }
334 bool isGlobalNode() const { return NodeType & GlobalNode; }
335 bool isUnknownNode() const { return NodeType & UnknownNode; }
337 bool isModified() const { return NodeType & Modified; }
338 bool isRead() const { return NodeType & Read; }
340 bool isIncomplete() const { return NodeType & Incomplete; }
341 bool isComplete() const { return !isIncomplete(); }
342 bool isDeadNode() const { return NodeType & DEAD; }
344 DSNode *setAllocaNodeMarker() { NodeType |= AllocaNode; return this; }
345 DSNode *setHeapNodeMarker() { NodeType |= HeapNode; return this; }
346 DSNode *setGlobalNodeMarker() { NodeType |= GlobalNode; return this; }
347 DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
349 DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
350 DSNode *setModifiedMarker() { NodeType |= Modified; return this; }
351 DSNode *setReadMarker() { NodeType |= Read; return this; }
352 DSNode *setArrayMarker() { NodeType |= Array; return this; }
354 void makeNodeDead() {
356 assert(hasNoReferrers() && "Dead node shouldn't have refs!");
360 /// forwardNode - Mark this node as being obsolete, and all references to it
361 /// should be forwarded to the specified node and offset.
363 void forwardNode(DSNode *To, unsigned Offset);
365 void print(llvm_ostream &O, const DSGraph *G) const {
366 if (O.stream()) print(*O.stream(), G);
368 void print(std::ostream &O, const DSGraph *G) const;
371 void assertOK() const;
373 void dropAllReferences() {
376 ForwardNH.setTo(0, 0);
379 /// remapLinks - Change all of the Links in the current node according to the
380 /// specified mapping.
382 void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
384 /// markReachableNodes - This method recursively traverses the specified
385 /// DSNodes, marking any nodes which are reachable. All reachable nodes it
386 /// adds to the set, which allows it to only traverse visited nodes once.
388 void markReachableNodes(hash_set<const DSNode*> &ReachableNodes) const;
391 friend class DSNodeHandle;
393 // static mergeNodes - Helper for mergeWith()
394 static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
397 //===----------------------------------------------------------------------===//
398 // Define the ilist_traits specialization for the DSGraph ilist.
401 struct ilist_traits<DSNode> {
402 static DSNode *getPrev(const DSNode *N) { return N->Prev; }
403 static DSNode *getNext(const DSNode *N) { return N->Next; }
405 static void setPrev(DSNode *N, DSNode *Prev) { N->Prev = Prev; }
406 static void setNext(DSNode *N, DSNode *Next) { N->Next = Next; }
408 static DSNode *createSentinel() { return new DSNode(0,0); }
409 static void destroySentinel(DSNode *N) { delete N; }
410 //static DSNode *createNode(const DSNode &V) { return new DSNode(V); }
413 void addNodeToList(DSNode *NTy) {}
414 void removeNodeFromList(DSNode *NTy) {}
415 void transferNodesFromList(iplist<DSNode, ilist_traits> &L2,
416 ilist_iterator<DSNode> first,
417 ilist_iterator<DSNode> last) {}
421 struct ilist_traits<const DSNode> : public ilist_traits<DSNode> {};
423 //===----------------------------------------------------------------------===//
424 // Define inline DSNodeHandle functions that depend on the definition of DSNode
426 inline DSNode *DSNodeHandle::getNode() const {
427 // Disabling this assertion because it is failing on a "magic" struct
428 // in named (from bind). The fourth field is an array of length 0,
429 // presumably used to create struct instances of different sizes.
430 // In a variable length struct, Offset could exceed Size when getNode()
431 // is called before such a node is folded. In this case, the DS Analysis now
432 // correctly folds this node after calling getNode.
434 N->isNodeCompletelyFolded() ||
435 (N->Size == 0 && Offset == 0) ||
436 (int(Offset) >= 0 && Offset < N->Size) ||
437 (int(Offset) < 0 && -int(Offset) < int(N->Size)) ||
438 N->isForwarding()) && "Node handle offset out of range!");
440 if (N == 0 || !N->isForwarding())
443 return HandleForwarding();
446 inline void DSNodeHandle::setTo(DSNode *n, unsigned NewOffset) const {
447 assert(!n || !n->isForwarding() && "Cannot set node to a forwarded node!");
448 if (N) getNode()->NumReferrers--;
453 if (Offset >= N->Size) {
454 assert((Offset == 0 || N->Size == 1) &&
455 "Pointer to non-collapsed node with invalid offset!");
459 assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
460 assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
461 N->isForwarding()) && "Node handle offset out of range!");
464 inline bool DSNodeHandle::hasLink(unsigned Num) const {
465 assert(N && "DSNodeHandle does not point to a node yet!");
466 return getNode()->hasLink(Num+Offset);
470 /// getLink - Treat this current node pointer as a pointer to a structure of
471 /// some sort. This method will return the pointer a mem[this+Num]
473 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
474 assert(N && "DSNodeHandle does not point to a node yet!");
475 return getNode()->getLink(Offset+Off);
477 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
478 assert(N && "DSNodeHandle does not point to a node yet!");
479 return getNode()->getLink(Off+Offset);
482 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
483 assert(N && "DSNodeHandle does not point to a node yet!");
484 getNode()->setLink(Off+Offset, NH);
487 /// addEdgeTo - Add an edge from the current node to the specified node. This
488 /// can cause merging of nodes in the graph.
490 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
491 assert(N && "DSNodeHandle does not point to a node yet!");
492 getNode()->addEdgeTo(Off+Offset, Node);
495 /// mergeWith - Merge the logical node pointed to by 'this' with the node
496 /// pointed to by 'N'.
498 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) const {
500 getNode()->mergeWith(Node, Offset);
501 else { // No node to merge with, so just point to Node
503 DSNode *NN = Node.getNode();
504 setTo(NN, Node.getOffset());
508 } // End llvm namespace