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