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