Included assert.h so that the code compiles under newer versions of GCC.
[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     Modified    = 1 << 5,   // This node is modified in this context
73     Read        = 1 << 6,   // This node is read in this context
74     Array       = 1 << 7,   // This node is treated like an array
75 #if 1
76     DEAD        = 1 << 8,   // This node is dead and should not be pointed to
77 #endif
78
79     Composition = AllocaNode | HeapNode | GlobalNode | UnknownNode,
80   };
81   
82   /// NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
83   /// to the nodes in the data structure graph, so it is possible to have nodes
84   /// with a value of 0 for their NodeType.  Scalar and Alloca markers go away
85   /// when function graphs are inlined.
86   ///
87   unsigned short NodeType;
88
89   DSNode(unsigned NodeTy, 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   /// isModified - Return true if this node may be modified in this context
126   ///
127   bool isModified() const { return (NodeType & Modified) != 0; }
128
129   /// isRead - Return true if this node may be read in this context
130   ///
131   bool isRead() const { return (NodeType & Read) != 0; }
132
133   DSGraph *getParentGraph() const { return ParentGraph; }
134   void setParentGraph(DSGraph *G) { ParentGraph = G; }
135
136
137   /// getForwardNode - This method returns the node that this node is forwarded
138   /// to, if any.
139   DSNode *getForwardNode() const { return ForwardNH.getNode(); }
140   void stopForwarding() {
141     assert(!ForwardNH.isNull() &&
142            "Node isn't forwarding, cannot stopForwarding!");
143     ForwardNH.setNode(0);
144   }
145
146   /// hasLink - Return true if this memory object has a link in slot #LinkNo
147   ///
148   bool hasLink(unsigned Offset) const {
149     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
150            "Pointer offset not aligned correctly!");
151     unsigned Index = Offset >> DS::PointerShift;
152     assert(Index < Links.size() && "Link index is out of range!");
153     return Links[Index].getNode();
154   }
155
156   /// getLink - Return the link at the specified offset.
157   DSNodeHandle &getLink(unsigned Offset) {
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   const DSNodeHandle &getLink(unsigned Offset) const {
165     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
166            "Pointer offset not aligned correctly!");
167     unsigned Index = Offset >> DS::PointerShift;
168     assert(Index < Links.size() && "Link index is out of range!");
169     return Links[Index];
170   }
171
172   /// getNumLinks - Return the number of links in a node...
173   ///
174   unsigned getNumLinks() const { return Links.size(); }
175
176   /// mergeTypeInfo - This method merges the specified type into the current
177   /// node at the specified offset.  This may update the current node's type
178   /// record if this gives more information to the node, it may do nothing to
179   /// the node if this information is already known, or it may merge the node
180   /// completely (and return true) if the information is incompatible with what
181   /// is already known.
182   ///
183   /// This method returns true if the node is completely folded, otherwise
184   /// false.
185   ///
186   bool mergeTypeInfo(const Type *Ty, unsigned Offset,
187                      bool FoldIfIncompatible = true);
188
189   /// foldNodeCompletely - If we determine that this node has some funny
190   /// behavior happening to it that we cannot represent, we fold it down to a
191   /// single, completely pessimistic, node.  This node is represented as a
192   /// single byte with a single TypeEntry of "void" with isArray = true.
193   ///
194   void foldNodeCompletely();
195
196   /// isNodeCompletelyFolded - Return true if this node has been completely
197   /// folded down to something that can never be expanded, effectively losing
198   /// all of the field sensitivity that may be present in the node.
199   ///
200   bool isNodeCompletelyFolded() const;
201
202   /// setLink - Set the link at the specified offset to the specified
203   /// NodeHandle, replacing what was there.  It is uncommon to use this method,
204   /// instead one of the higher level methods should be used, below.
205   ///
206   void setLink(unsigned Offset, const DSNodeHandle &NH) {
207     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
208            "Pointer offset not aligned correctly!");
209     unsigned Index = Offset >> DS::PointerShift;
210     assert(Index < Links.size() && "Link index is out of range!");
211     Links[Index] = NH;
212   }
213
214   /// getPointerSize - Return the size of a pointer for the current target.
215   ///
216   unsigned getPointerSize() const { return DS::PointerSize; }
217
218   /// addEdgeTo - Add an edge from the current node to the specified node.  This
219   /// can cause merging of nodes in the graph.
220   ///
221   void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
222
223   /// mergeWith - Merge this node and the specified node, moving all links to
224   /// and from the argument node into the current node, deleting the node
225   /// argument.  Offset indicates what offset the specified node is to be merged
226   /// into the current node.
227   ///
228   /// The specified node may be a null pointer (in which case, nothing happens).
229   ///
230   void mergeWith(const DSNodeHandle &NH, unsigned Offset);
231
232   /// addGlobal - Add an entry for a global value to the Globals list.  This
233   /// also marks the node with the 'G' flag if it does not already have it.
234   ///
235   void addGlobal(GlobalValue *GV);
236   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
237   std::vector<GlobalValue*> &getGlobals() { return Globals; }
238
239   /// forwardNode - Mark this node as being obsolete, and all references to it
240   /// should be forwarded to the specified node and offset.
241   ///
242   void forwardNode(DSNode *To, unsigned Offset);
243
244   void print(std::ostream &O, const DSGraph *G) const;
245   void dump() const;
246
247   void assertOK() const;
248
249   void dropAllReferences() {
250     Links.clear();
251     if (!ForwardNH.isNull())
252       ForwardNH.setNode(0);
253   }
254
255   /// remapLinks - Change all of the Links in the current node according to the
256   /// specified mapping.
257   void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
258
259   /// markReachableNodes - This method recursively traverses the specified
260   /// DSNodes, marking any nodes which are reachable.  All reachable nodes it
261   /// adds to the set, which allows it to only traverse visited nodes once.
262   ///
263   void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
264
265 private:
266   friend class DSNodeHandle;
267
268   // static mergeNodes - Helper for mergeWith()
269   static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
270 };
271
272
273 //===----------------------------------------------------------------------===//
274 // Define inline DSNodeHandle functions that depend on the definition of DSNode
275 //
276 inline DSNode *DSNodeHandle::getNode() const {
277   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
278           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
279   if (!N || N->ForwardNH.isNull())
280     return N;
281
282   return HandleForwarding();
283 }
284
285 inline void DSNodeHandle::setNode(DSNode *n) {
286   assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
287   if (N) N->NumReferrers--;
288   N = n;
289   if (N) {
290     N->NumReferrers++;
291     if (Offset >= N->Size) {
292       assert((Offset == 0 || N->Size == 1) &&
293              "Pointer to non-collapsed node with invalid offset!");
294       Offset = 0;
295     }
296   }
297   assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
298
299   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
300           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
301 }
302
303 inline bool DSNodeHandle::hasLink(unsigned Num) const {
304   assert(N && "DSNodeHandle does not point to a node yet!");
305   return getNode()->hasLink(Num+Offset);
306 }
307
308
309 /// getLink - Treat this current node pointer as a pointer to a structure of
310 /// some sort.  This method will return the pointer a mem[this+Num]
311 ///
312 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
313   assert(N && "DSNodeHandle does not point to a node yet!");
314   return getNode()->getLink(Offset+Off);
315 }
316 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
317   assert(N && "DSNodeHandle does not point to a node yet!");
318   return getNode()->getLink(Off+Offset);
319 }
320
321 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
322   assert(N && "DSNodeHandle does not point to a node yet!");
323   getNode()->setLink(Off+Offset, NH);
324 }
325
326 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
327 /// can cause merging of nodes in the graph.
328 ///
329 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
330   assert(N && "DSNodeHandle does not point to a node yet!");
331   getNode()->addEdgeTo(Off+Offset, Node);
332 }
333
334 /// mergeWith - Merge the logical node pointed to by 'this' with the node
335 /// pointed to by 'N'.
336 ///
337 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
338   if (N != 0)
339     getNode()->mergeWith(Node, Offset);
340   else     // No node to merge with, so just point to Node
341     *this = Node;
342 }
343
344 #endif