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