ff1d7d614ea8c979e886cfe00e0d1554d518c658
[oota-llvm.git] / include / llvm / Analysis / DSNode.h
1 //===- DSNode.h - Node definition for datastructure graphs ------*- C++ -*-===//
2 //
3 // Data structure graph nodes and some implementation of DSNodeHandle.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_ANALYSIS_DSNODE_H
8 #define LLVM_ANALYSIS_DSNODE_H
9
10 #include "llvm/Analysis/DSSupport.h"
11
12 template<typename BaseType>
13 class DSNodeIterator;          // Data structure graph traversal iterator
14
15 //===----------------------------------------------------------------------===//
16 /// DSNode - Data structure node class
17 ///
18 /// This class represents an untyped memory object of Size bytes.  It keeps
19 /// track of any pointers that have been stored into the object as well as the
20 /// different types represented in this object.
21 ///
22 class DSNode {
23   /// NumReferrers - The number of DSNodeHandles pointing to this node... if
24   /// this is a forwarding node, then this is the number of node handles which
25   /// are still forwarding over us.
26   ///
27   unsigned NumReferrers;
28
29   /// ForwardNH - This NodeHandle contain the node (and offset into the node)
30   /// that this node really is.  When nodes get folded together, the node to be
31   /// eliminated has these fields filled in, otherwise ForwardNH.getNode() is
32   /// null.
33   DSNodeHandle ForwardNH;
34
35   /// Size - The current size of the node.  This should be equal to the size of
36   /// the current type record.
37   ///
38   unsigned Size;
39
40   /// ParentGraph - The graph this node is currently embedded into.
41   ///
42   DSGraph *ParentGraph;
43
44   /// Ty - Keep track of the current outer most type of this object, in addition
45   /// to whether or not it has been indexed like an array or not.  If the
46   /// isArray bit is set, the node cannot grow.
47   ///
48   const Type *Ty;                 // The type itself...
49
50   /// Links - Contains one entry for every sizeof(void*) bytes in this memory
51   /// object.  Note that if the node is not a multiple of size(void*) bytes
52   /// large, that there is an extra entry for the "remainder" of the node as
53   /// well.  For this reason, nodes of 1 byte in size do have one link.
54   ///
55   std::vector<DSNodeHandle> Links;
56
57   /// Globals - The list of global values that are merged into this node.
58   ///
59   std::vector<GlobalValue*> Globals;
60
61   void operator=(const DSNode &); // DO NOT IMPLEMENT
62   DSNode(const DSNode &);         // DO NOT IMPLEMENT
63 public:
64   enum NodeTy {
65     ShadowNode  = 0,        // Nothing is known about this node...
66     AllocaNode  = 1 << 0,   // This node was allocated with alloca
67     HeapNode    = 1 << 1,   // This node was allocated with malloc
68     GlobalNode  = 1 << 2,   // This node was allocated by a global var decl
69     UnknownNode = 1 << 3,   // This node points to unknown allocated memory 
70     Incomplete  = 1 << 4,   // This node may not be complete
71
72     Modified    = 1 << 5,   // This node is modified in this context
73     Read        = 1 << 6,   // This node is read in this context
74
75     Array       = 1 << 7,   // This node is treated like an array
76     //#ifndef NDEBUG
77     DEAD        = 1 << 8,   // This node is dead and should not be pointed to
78     //#endif
79
80     Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
81   };
82   
83   /// NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
84   /// to the nodes in the data structure graph, so it is possible to have nodes
85   /// with a value of 0 for their NodeType.
86   ///
87 private:
88   unsigned short NodeType;
89 public:
90
91   DSNode(const Type *T, DSGraph *G);
92   DSNode(const DSNode &, DSGraph *G);
93
94   ~DSNode() {
95     dropAllReferences();
96     assert(hasNoReferrers() && "Referrers to dead node exist!");
97   }
98
99   // Iterator for graph interface... Defined in DSGraphTraits.h
100   typedef DSNodeIterator<DSNode> iterator;
101   typedef DSNodeIterator<const DSNode> const_iterator;
102   inline iterator begin();
103   inline iterator end();
104   inline const_iterator begin() const;
105   inline const_iterator end() const;
106
107   //===--------------------------------------------------
108   // Accessors
109
110   /// getSize - Return the maximum number of bytes occupied by this object...
111   ///
112   unsigned getSize() const { return Size; }
113
114   // getType - Return the node type of this object...
115   const Type *getType() const { return Ty; }
116   bool isArray() const { return NodeType & Array; }
117
118   /// hasNoReferrers - Return true if nothing is pointing to this node at all.
119   ///
120   bool hasNoReferrers() const { return getNumReferrers() == 0; }
121
122   /// getNumReferrers - This method returns the number of referrers to the
123   /// current node.  Note that if this node is a forwarding node, this will
124   /// return the number of nodes forwarding over the node!
125   unsigned getNumReferrers() const { return NumReferrers; }
126
127   DSGraph *getParentGraph() const { return ParentGraph; }
128   void setParentGraph(DSGraph *G) { ParentGraph = G; }
129
130   /// getForwardNode - This method returns the node that this node is forwarded
131   /// to, if any.
132   DSNode *getForwardNode() const { return ForwardNH.getNode(); }
133   void stopForwarding() {
134     assert(!ForwardNH.isNull() &&
135            "Node isn't forwarding, cannot stopForwarding!");
136     ForwardNH.setNode(0);
137   }
138
139   /// hasLink - Return true if this memory object has a link in slot #LinkNo
140   ///
141   bool hasLink(unsigned Offset) const {
142     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
143            "Pointer offset not aligned correctly!");
144     unsigned Index = Offset >> DS::PointerShift;
145     assert(Index < Links.size() && "Link index is out of range!");
146     return Links[Index].getNode();
147   }
148
149   /// getLink - Return the link at the specified offset.
150   DSNodeHandle &getLink(unsigned Offset) {
151     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
152            "Pointer offset not aligned correctly!");
153     unsigned Index = Offset >> DS::PointerShift;
154     assert(Index < Links.size() && "Link index is out of range!");
155     return Links[Index];
156   }
157   const DSNodeHandle &getLink(unsigned Offset) const {
158     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
159            "Pointer offset not aligned correctly!");
160     unsigned Index = Offset >> DS::PointerShift;
161     assert(Index < Links.size() && "Link index is out of range!");
162     return Links[Index];
163   }
164
165   /// getNumLinks - Return the number of links in a node...
166   ///
167   unsigned getNumLinks() const { return Links.size(); }
168
169   /// mergeTypeInfo - This method merges the specified type into the current
170   /// node at the specified offset.  This may update the current node's type
171   /// record if this gives more information to the node, it may do nothing to
172   /// the node if this information is already known, or it may merge the node
173   /// completely (and return true) if the information is incompatible with what
174   /// is already known.
175   ///
176   /// This method returns true if the node is completely folded, otherwise
177   /// false.
178   ///
179   bool mergeTypeInfo(const Type *Ty, unsigned Offset,
180                      bool FoldIfIncompatible = true);
181
182   /// foldNodeCompletely - If we determine that this node has some funny
183   /// behavior happening to it that we cannot represent, we fold it down to a
184   /// single, completely pessimistic, node.  This node is represented as a
185   /// single byte with a single TypeEntry of "void" with isArray = true.
186   ///
187   void foldNodeCompletely();
188
189   /// isNodeCompletelyFolded - Return true if this node has been completely
190   /// folded down to something that can never be expanded, effectively losing
191   /// all of the field sensitivity that may be present in the node.
192   ///
193   bool isNodeCompletelyFolded() const;
194
195   /// setLink - Set the link at the specified offset to the specified
196   /// NodeHandle, replacing what was there.  It is uncommon to use this method,
197   /// instead one of the higher level methods should be used, below.
198   ///
199   void setLink(unsigned Offset, const DSNodeHandle &NH) {
200     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
201            "Pointer offset not aligned correctly!");
202     unsigned Index = Offset >> DS::PointerShift;
203     assert(Index < Links.size() && "Link index is out of range!");
204     Links[Index] = NH;
205   }
206
207   /// getPointerSize - Return the size of a pointer for the current target.
208   ///
209   unsigned getPointerSize() const { return DS::PointerSize; }
210
211   /// addEdgeTo - Add an edge from the current node to the specified node.  This
212   /// can cause merging of nodes in the graph.
213   ///
214   void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
215
216   /// mergeWith - Merge this node and the specified node, moving all links to
217   /// and from the argument node into the current node, deleting the node
218   /// argument.  Offset indicates what offset the specified node is to be merged
219   /// into the current node.
220   ///
221   /// The specified node may be a null pointer (in which case, nothing happens).
222   ///
223   void mergeWith(const DSNodeHandle &NH, unsigned Offset);
224
225   /// addGlobal - Add an entry for a global value to the Globals list.  This
226   /// also marks the node with the 'G' flag if it does not already have it.
227   ///
228   void addGlobal(GlobalValue *GV);
229   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
230
231   /// maskNodeTypes - Apply a mask to the node types bitfield.
232   ///
233   void maskNodeTypes(unsigned Mask) {
234     NodeType &= Mask;
235   }
236
237   /// getNodeFlags - Return all of the flags set on the node.  If the DEAD flag
238   /// is set, hide it from the caller.
239   unsigned getNodeFlags() const { return NodeType & ~DEAD; }
240
241   bool isAllocaNode()  const { return NodeType & AllocaNode; }
242   bool isHeapNode()    const { return NodeType & HeapNode; }
243   bool isGlobalNode()  const { return NodeType & GlobalNode; }
244   bool isUnknownNode() const { return NodeType & UnknownNode; }
245
246   bool isModified() const   { return NodeType & Modified; }
247   bool isRead() const       { return NodeType & Read; }
248
249   bool isIncomplete() const { return NodeType & Incomplete; }
250   bool isComplete() const   { return !isIncomplete(); }
251   bool isDeadNode() const   { return NodeType & DEAD; }
252
253   DSNode *setAllocaNodeMarker()  { NodeType |= AllocaNode;  return this; }
254   DSNode *setHeapNodeMarker()    { NodeType |= HeapNode;    return this; }
255   DSNode *setGlobalNodeMarker()  { NodeType |= GlobalNode;  return this; }
256   DSNode *setUnknownNodeMarker() { NodeType |= UnknownNode; return this; }
257
258   DSNode *setIncompleteMarker() { NodeType |= Incomplete; return this; }
259   DSNode *setModifiedMarker()   { NodeType |= Modified;   return this; }
260   DSNode *setReadMarker()       { NodeType |= Read;       return this; }
261
262   void makeNodeDead() {
263     Globals.clear();
264     assert(hasNoReferrers() && "Dead node shouldn't have refs!");
265     NodeType = DEAD;
266   }
267
268   /// forwardNode - Mark this node as being obsolete, and all references to it
269   /// should be forwarded to the specified node and offset.
270   ///
271   void forwardNode(DSNode *To, unsigned Offset);
272
273   void print(std::ostream &O, const DSGraph *G) const;
274   void dump() const;
275
276   void assertOK() const;
277
278   void dropAllReferences() {
279     Links.clear();
280     if (!ForwardNH.isNull())
281       ForwardNH.setNode(0);
282   }
283
284   /// remapLinks - Change all of the Links in the current node according to the
285   /// specified mapping.
286   void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
287
288   /// markReachableNodes - This method recursively traverses the specified
289   /// DSNodes, marking any nodes which are reachable.  All reachable nodes it
290   /// adds to the set, which allows it to only traverse visited nodes once.
291   ///
292   void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
293
294 private:
295   friend class DSNodeHandle;
296
297   // static mergeNodes - Helper for mergeWith()
298   static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
299 };
300
301
302 //===----------------------------------------------------------------------===//
303 // Define inline DSNodeHandle functions that depend on the definition of DSNode
304 //
305 inline DSNode *DSNodeHandle::getNode() const {
306   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
307           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
308   if (!N || N->ForwardNH.isNull())
309     return N;
310
311   return HandleForwarding();
312 }
313
314 inline void DSNodeHandle::setNode(DSNode *n) {
315   assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
316   if (N) N->NumReferrers--;
317   N = n;
318   if (N) {
319     N->NumReferrers++;
320     if (Offset >= N->Size) {
321       assert((Offset == 0 || N->Size == 1) &&
322              "Pointer to non-collapsed node with invalid offset!");
323       Offset = 0;
324     }
325   }
326   assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
327   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
328           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
329 }
330
331 inline bool DSNodeHandle::hasLink(unsigned Num) const {
332   assert(N && "DSNodeHandle does not point to a node yet!");
333   return getNode()->hasLink(Num+Offset);
334 }
335
336
337 /// getLink - Treat this current node pointer as a pointer to a structure of
338 /// some sort.  This method will return the pointer a mem[this+Num]
339 ///
340 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
341   assert(N && "DSNodeHandle does not point to a node yet!");
342   return getNode()->getLink(Offset+Off);
343 }
344 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
345   assert(N && "DSNodeHandle does not point to a node yet!");
346   return getNode()->getLink(Off+Offset);
347 }
348
349 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
350   assert(N && "DSNodeHandle does not point to a node yet!");
351   getNode()->setLink(Off+Offset, NH);
352 }
353
354 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
355 /// can cause merging of nodes in the graph.
356 ///
357 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
358   assert(N && "DSNodeHandle does not point to a node yet!");
359   getNode()->addEdgeTo(Off+Offset, Node);
360 }
361
362 /// mergeWith - Merge the logical node pointed to by 'this' with the node
363 /// pointed to by 'N'.
364 ///
365 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
366   if (N != 0)
367     getNode()->mergeWith(Node, Offset);
368   else     // No node to merge with, so just point to Node
369     *this = Node;
370 }
371
372 #endif