Add a bunch of assertions
[oota-llvm.git] / include / llvm / Analysis / DataStructure / 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 //===----------------------------------------------------------------------===//
13 /// DSNode - Data structure node class
14 ///
15 /// This class represents an untyped memory object of Size bytes.  It keeps
16 /// track of any pointers that have been stored into the object as well as the
17 /// different types represented in this object.
18 ///
19 class DSNode {
20   /// Links - Contains one entry for every _distinct_ pointer field in the
21   /// memory block.  These are demand allocated and indexed by the MergeMap
22   /// vector.
23   ///
24   std::vector<DSNodeHandle> Links;
25
26   /// MergeMap - Maps from every byte in the object to a signed byte number.
27   /// This map is neccesary due to the merging that is possible as part of the
28   /// unification algorithm.  To merge two distinct bytes of the object together
29   /// into a single logical byte, the indexes for the two bytes are set to the
30   /// same value.  This fully general merging is capable of representing all
31   /// manners of array merging if neccesary.
32   ///
33   /// This map is also used to map outgoing pointers to various byte offsets in
34   /// this data structure node.  If this value is >= 0, then it indicates that
35   /// the numbered entry in the Links vector contains the outgoing edge for this
36   /// byte offset.  In this way, the Links vector can be demand allocated and
37   /// byte elements of the node may be merged without needing a Link allocated
38   /// for it.
39   ///
40   /// Initially, each each element of the MergeMap is assigned a unique negative
41   /// number, which are then merged as the unification occurs.
42   ///
43   std::vector<signed char> MergeMap;
44
45   /// Referrers - Keep track of all of the node handles that point to this
46   /// DSNode.  These pointers may need to be updated to point to a different
47   /// node if this node gets merged with it.
48   ///
49   std::vector<DSNodeHandle*> Referrers;
50
51   /// TypeEntries - As part of the merging process of this algorithm, nodes of
52   /// different types can be represented by this single DSNode.  This vector is
53   /// kept sorted.
54   ///
55   std::vector<DSTypeRec> TypeEntries;
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 public:
63   enum NodeTy {
64     ShadowNode  = 0,        // Nothing is known about this node...
65     AllocaNode  = 1 << 0,   // This node was allocated with alloca
66     HeapNode    = 1 << 1,   // This node was allocated with malloc
67     GlobalNode  = 1 << 2,   // This node was allocated by a global var decl
68     UnknownNode = 1 << 3,   // This node points to unknown allocated memory 
69     Incomplete  = 1 << 4,   // This node may not be complete
70     Modified    = 1 << 5,   // This node is modified in this context
71     Read        = 1 << 6,   // This node is read in this context
72   };
73   
74   /// NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
75   /// to the nodes in the data structure graph, so it is possible to have nodes
76   /// with a value of 0 for their NodeType.  Scalar and Alloca markers go away
77   /// when function graphs are inlined.
78   ///
79   unsigned char NodeType;
80
81   DSNode(enum NodeTy NT, const Type *T);
82   DSNode(const DSNode &);
83
84   ~DSNode() {
85 #ifndef NDEBUG
86     dropAllReferences();  // Only needed to satisfy assertion checks...
87     assert(Referrers.empty() && "Referrers to dead node exist!");
88 #endif
89   }
90
91   // Iterator for graph interface...
92   typedef DSNodeIterator iterator;
93   typedef DSNodeIterator const_iterator;
94   inline iterator begin() const;   // Defined in DSGraphTraits.h
95   inline iterator end() const;
96
97   //===--------------------------------------------------
98   // Accessors
99
100   /// getSize - Return the maximum number of bytes occupied by this object...
101   ///
102   unsigned getSize() const { return MergeMap.size(); }
103
104   // getTypeEntries - Return the possible types and their offsets in this object
105   const std::vector<DSTypeRec> &getTypeEntries() const { return TypeEntries; }
106
107   /// getReferrers - Return a list of the pointers to this node...
108   ///
109   const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
110
111   /// isModified - Return true if this node may be modified in this context
112   ///
113   bool isModified() const { return (NodeType & Modified) != 0; }
114
115   /// isRead - Return true if this node may be read in this context
116   ///
117   bool isRead() const { return (NodeType & Read) != 0; }
118
119
120   /// hasLink - Return true if this memory object has a link at the specified
121   /// location.
122   ///
123   bool hasLink(unsigned i) const {
124     assert(i < getSize() && "Field Link index is out of range!");
125     return MergeMap[i] >= 0;
126   }
127
128   DSNodeHandle *getLink(unsigned i) {
129     if (hasLink(i)) {
130       assert((unsigned)MergeMap[i] < Links.size() &&
131              "MergeMap references Link that doesn't exist!");
132       return &Links[MergeMap[i]];
133     }
134     return 0;
135   }
136   const DSNodeHandle *getLink(unsigned i) const {
137     if (hasLink(i)) {
138       assert((unsigned)MergeMap[i] < Links.size() &&
139              "MergeMap references Link that doesn't exist!");
140       return &Links[MergeMap[i]];
141     }
142     return 0;
143   }
144
145   /// getMergeMapLabel - Return the merge map entry specified, to allow printing
146   /// out of DSNodes nicely for DOT graphs.
147   ///
148   int getMergeMapLabel(unsigned i) const {
149     assert(i < MergeMap.size() && "MergeMap index out of range!");
150     return MergeMap[i];
151   }
152
153   /// getTypeRec - This method returns the specified type record if it exists.
154   /// If it does not yet exist, the method checks to see whether or not the
155   /// request would result in an untrackable state.  If adding it would cause
156   /// untrackable state, we foldNodeCompletely the node and return the void
157   /// record, otherwise we add an new TypeEntry and return it.
158   ///
159   DSTypeRec &getTypeRec(const Type *Ty, unsigned Offset);
160
161   /// foldNodeCompletely - If we determine that this node has some funny
162   /// behavior happening to it that we cannot represent, we fold it down to a
163   /// single, completely pessimistic, node.  This node is represented as a
164   /// single byte with a single TypeEntry of "void".
165   ///
166   void foldNodeCompletely();
167
168   /// isNodeCompletelyFolded - Return true if this node has been completely
169   /// folded down to something that can never be expanded, effectively losing
170   /// all of the field sensitivity that may be present in the node.
171   ///
172   bool isNodeCompletelyFolded() const;
173
174   /// setLink - Set the link at the specified offset to the specified
175   /// NodeHandle, replacing what was there.  It is uncommon to use this method,
176   /// instead one of the higher level methods should be used, below.
177   ///
178   void setLink(unsigned i, const DSNodeHandle &NH);
179
180   /// addEdgeTo - Add an edge from the current node to the specified node.  This
181   /// can cause merging of nodes in the graph.
182   ///
183   void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
184
185   /// mergeWith - Merge this node and the specified node, moving all links to
186   /// and from the argument node into the current node, deleting the node
187   /// argument.  Offset indicates what offset the specified node is to be merged
188   /// into the current node.
189   ///
190   /// The specified node may be a null pointer (in which case, nothing happens).
191   ///
192   void mergeWith(const DSNodeHandle &NH, unsigned Offset);
193
194   /// mergeIndexes - If we discover that two indexes are equivalent and must be
195   /// merged, this function is used to do the dirty work.
196   ///
197   void mergeIndexes(unsigned idx1, unsigned idx2) {
198     assert(idx1 < getSize() && idx2 < getSize() && "Indexes out of range!");
199     signed char MV1 = MergeMap[idx1];
200     signed char MV2 = MergeMap[idx2];
201     if (MV1 != MV2)
202       mergeMappedValues(MV1, MV2);
203   }
204
205
206   /// addGlobal - Add an entry for a global value to the Globals list.  This
207   /// also marks the node with the 'G' flag if it does not already have it.
208   ///
209   void addGlobal(GlobalValue *GV);
210   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
211   std::vector<GlobalValue*> &getGlobals() { return Globals; }
212
213   void print(std::ostream &O, const DSGraph *G) const;
214   void dump() const;
215
216   void dropAllReferences() {
217     Links.clear();
218   }
219
220   /// remapLinks - Change all of the Links in the current node according to the
221   /// specified mapping.
222   void remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap);
223
224 private:
225   friend class DSNodeHandle;
226   // addReferrer - Keep the referrer set up to date...
227   void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
228   void removeReferrer(DSNodeHandle *H);
229
230   /// rewriteMergeMap - Loop over the mergemap, replacing any references to the
231   /// index From to be references to the index To.
232   ///
233   void rewriteMergeMap(signed char From, signed char To) {
234     assert(From != To && "Cannot change something into itself!");
235     assert(To < (int)Links.size() &&
236            "Changing MergeMap entry to an illegal entry!");
237     for (unsigned i = 0, e = MergeMap.size(); i != e; ++i)
238       if (MergeMap[i] == From)
239         MergeMap[i] = To;
240   }
241
242   /// mergeMappedValues - This is the higher level form of rewriteMergeMap.  It
243   /// is fully capable of merging links together if neccesary as well as simply
244   /// rewriting the map entries.
245   ///
246   void mergeMappedValues(signed char V1, signed char V2);
247
248   /// growNode - Attempt to grow the node to the specified size.  This may do
249   /// one of three things:
250   ///   1. Grow the node, return false
251   ///   2. Refuse to grow the node, but maintain a trackable situation, return
252   ///      false.
253   ///   3. Be unable to track if node was that size, so collapse the node and
254   ///      return true.
255   ///
256   bool growNode(unsigned RequestedSize);
257 };
258
259
260 //===----------------------------------------------------------------------===//
261 // Define inline DSNodeHandle functions that depend on the definition of DSNode
262 //
263
264 inline void DSNodeHandle::setNode(DSNode *n) {
265   if (N) N->removeReferrer(this);
266   N = n;
267   if (N) N->addReferrer(this);
268 }
269
270 inline bool DSNodeHandle::hasLink(unsigned Num) const {
271   assert(N && "DSNodeHandle does not point to a node yet!");
272   return N->hasLink(Num+Offset);
273 }
274
275
276 /// getLink - Treat this current node pointer as a pointer to a structure of
277 /// some sort.  This method will return the pointer a mem[this+Num]
278 ///
279 inline const DSNodeHandle *DSNodeHandle::getLink(unsigned Num) const {
280   assert(N && "DSNodeHandle does not point to a node yet!");
281   return N->getLink(Num+Offset);
282 }
283 inline DSNodeHandle *DSNodeHandle::getLink(unsigned Num) {
284   assert(N && "DSNodeHandle does not point to a node yet!");
285   return N->getLink(Num+Offset);
286 }
287
288 inline void DSNodeHandle::setLink(unsigned Num, const DSNodeHandle &NH) {
289   assert(N && "DSNodeHandle does not point to a node yet!");
290   N->setLink(Num+Offset, NH);
291 }
292
293 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
294 /// can cause merging of nodes in the graph.
295 ///
296 inline void DSNodeHandle::addEdgeTo(unsigned LinkNo, const DSNodeHandle &Node) {
297   assert(N && "DSNodeHandle does not point to a node yet!");
298   N->addEdgeTo(LinkNo+Offset, Node);
299 }
300
301 /// mergeWith - Merge the logical node pointed to by 'this' with the node
302 /// pointed to by 'N'.
303 ///
304 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
305   if (N != 0)
306     N->mergeWith(Node, Offset);
307   else {   // No node to merge with, so just point to Node
308     *this = Node;
309   }
310 }
311
312 #endif