* LoopInfo now keeps track of exit blocks from the loop
[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 "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
186   /// foldNodeCompletely - If we determine that this node has some funny
187   /// behavior happening to it that we cannot represent, we fold it down to a
188   /// single, completely pessimistic, node.  This node is represented as a
189   /// single byte with a single TypeEntry of "void" with isArray = true.
190   ///
191   void foldNodeCompletely();
192
193   /// isNodeCompletelyFolded - Return true if this node has been completely
194   /// folded down to something that can never be expanded, effectively losing
195   /// all of the field sensitivity that may be present in the node.
196   ///
197   bool isNodeCompletelyFolded() const;
198
199   /// setLink - Set the link at the specified offset to the specified
200   /// NodeHandle, replacing what was there.  It is uncommon to use this method,
201   /// instead one of the higher level methods should be used, below.
202   ///
203   void setLink(unsigned Offset, const DSNodeHandle &NH) {
204     assert((Offset & ((1 << DS::PointerShift)-1)) == 0 &&
205            "Pointer offset not aligned correctly!");
206     unsigned Index = Offset >> DS::PointerShift;
207     assert(Index < Links.size() && "Link index is out of range!");
208     Links[Index] = NH;
209   }
210
211   /// getPointerSize - Return the size of a pointer for the current target.
212   ///
213   unsigned getPointerSize() const { return DS::PointerSize; }
214
215   /// addEdgeTo - Add an edge from the current node to the specified node.  This
216   /// can cause merging of nodes in the graph.
217   ///
218   void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
219
220   /// mergeWith - Merge this node and the specified node, moving all links to
221   /// and from the argument node into the current node, deleting the node
222   /// argument.  Offset indicates what offset the specified node is to be merged
223   /// into the current node.
224   ///
225   /// The specified node may be a null pointer (in which case, nothing happens).
226   ///
227   void mergeWith(const DSNodeHandle &NH, unsigned Offset);
228
229   /// addGlobal - Add an entry for a global value to the Globals list.  This
230   /// also marks the node with the 'G' flag if it does not already have it.
231   ///
232   void addGlobal(GlobalValue *GV);
233   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
234   std::vector<GlobalValue*> &getGlobals() { return Globals; }
235
236   /// forwardNode - Mark this node as being obsolete, and all references to it
237   /// should be forwarded to the specified node and offset.
238   ///
239   void forwardNode(DSNode *To, unsigned Offset);
240
241   void print(std::ostream &O, const DSGraph *G) const;
242   void dump() const;
243
244   void assertOK() const;
245
246   void dropAllReferences() {
247     Links.clear();
248     if (!ForwardNH.isNull())
249       ForwardNH.setNode(0);
250   }
251
252   /// remapLinks - Change all of the Links in the current node according to the
253   /// specified mapping.
254   void remapLinks(hash_map<const DSNode*, DSNodeHandle> &OldNodeMap);
255
256   /// markReachableNodes - This method recursively traverses the specified
257   /// DSNodes, marking any nodes which are reachable.  All reachable nodes it
258   /// adds to the set, which allows it to only traverse visited nodes once.
259   ///
260   void markReachableNodes(hash_set<DSNode*> &ReachableNodes);
261
262 private:
263   friend class DSNodeHandle;
264
265   // static mergeNodes - Helper for mergeWith()
266   static void MergeNodes(DSNodeHandle& CurNodeH, DSNodeHandle& NH);
267 };
268
269
270 //===----------------------------------------------------------------------===//
271 // Define inline DSNodeHandle functions that depend on the definition of DSNode
272 //
273 inline DSNode *DSNodeHandle::getNode() const {
274   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
275           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
276   if (!N || N->ForwardNH.isNull())
277     return N;
278
279   return HandleForwarding();
280 }
281
282 inline void DSNodeHandle::setNode(DSNode *n) {
283   assert(!n || !n->getForwardNode() && "Cannot set node to a forwarded node!");
284   if (N) N->NumReferrers--;
285   N = n;
286   if (N) {
287     N->NumReferrers++;
288     if (Offset >= N->Size) {
289       assert((Offset == 0 || N->Size == 1) &&
290              "Pointer to non-collapsed node with invalid offset!");
291       Offset = 0;
292     }
293   }
294   assert(!N || ((N->NodeType & DSNode::DEAD) == 0));
295
296   assert((!N || Offset < N->Size || (N->Size == 0 && Offset == 0) ||
297           !N->ForwardNH.isNull()) && "Node handle offset out of range!");
298 }
299
300 inline bool DSNodeHandle::hasLink(unsigned Num) const {
301   assert(N && "DSNodeHandle does not point to a node yet!");
302   return getNode()->hasLink(Num+Offset);
303 }
304
305
306 /// getLink - Treat this current node pointer as a pointer to a structure of
307 /// some sort.  This method will return the pointer a mem[this+Num]
308 ///
309 inline const DSNodeHandle &DSNodeHandle::getLink(unsigned Off) const {
310   assert(N && "DSNodeHandle does not point to a node yet!");
311   return getNode()->getLink(Offset+Off);
312 }
313 inline DSNodeHandle &DSNodeHandle::getLink(unsigned Off) {
314   assert(N && "DSNodeHandle does not point to a node yet!");
315   return getNode()->getLink(Off+Offset);
316 }
317
318 inline void DSNodeHandle::setLink(unsigned Off, const DSNodeHandle &NH) {
319   assert(N && "DSNodeHandle does not point to a node yet!");
320   getNode()->setLink(Off+Offset, NH);
321 }
322
323 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
324 /// can cause merging of nodes in the graph.
325 ///
326 inline void DSNodeHandle::addEdgeTo(unsigned Off, const DSNodeHandle &Node) {
327   assert(N && "DSNodeHandle does not point to a node yet!");
328   getNode()->addEdgeTo(Off+Offset, Node);
329 }
330
331 /// mergeWith - Merge the logical node pointed to by 'this' with the node
332 /// pointed to by 'N'.
333 ///
334 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
335   if (N != 0)
336     getNode()->mergeWith(Node, Offset);
337   else     // No node to merge with, so just point to Node
338     *this = Node;
339 }
340
341 #endif