Allow direct access to mergemap for printing
[oota-llvm.git] / include / llvm / Analysis / DataStructure / DSGraph.h
1 //===- DSGraph.h - Represent a collection of data structures ----*- C++ -*-===//
2 //
3 // This header defines the primative classes that make up a data structure
4 // graph.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_ANALYSIS_DSGRAPH_H
9 #define LLVM_ANALYSIS_DSGRAPH_H
10
11 #include <vector>
12 #include <map>
13
14 class Function;
15 class Value;
16 class GlobalValue;
17 class Type;
18
19 class DSNode;                  // Each node in the graph
20 class DSGraph;                 // A graph for a function
21 class DSNodeIterator;          // Data structure graph traversal iterator
22
23 //===----------------------------------------------------------------------===//
24 /// DSNodeHandle - Implement a "handle" to a data structure node that takes care
25 /// of all of the add/un'refing of the node to prevent the backpointers in the
26 /// graph from getting out of date.  This class represents a "pointer" in the
27 /// graph, whose destination is an indexed offset into a node.
28 ///
29 class DSNodeHandle {
30   DSNode *N;
31   unsigned Offset;
32 public:
33   // Allow construction, destruction, and assignment...
34   DSNodeHandle(DSNode *n = 0, unsigned offs = 0) : N(0), Offset(offs) {
35     setNode(n);
36   }
37   DSNodeHandle(const DSNodeHandle &H) : N(0), Offset(H.Offset) { setNode(H.N); }
38   ~DSNodeHandle() { setNode((DSNode*)0); }
39   DSNodeHandle &operator=(const DSNodeHandle &H) {
40     setNode(H.N); Offset = H.Offset;
41     return *this;
42   }
43
44   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
45     return N < H.N || (N == H.N && Offset < H.Offset);
46   }
47   bool operator==(const DSNodeHandle &H) const { // Allow comparison
48     return N == H.N && Offset == H.Offset;
49   }
50   bool operator!=(const DSNodeHandle &H) const { return !operator==(H); }
51
52   // Allow explicit conversion to DSNode...
53   DSNode *getNode() const { return N; }
54   unsigned getOffset() const { return Offset; }
55
56   inline void setNode(DSNode *N);  // Defined inline later...
57   void setOffset(unsigned O) { Offset = O; }
58
59   void addEdgeTo(unsigned LinkNo, const DSNodeHandle &N);
60   void addEdgeTo(const DSNodeHandle &N) { addEdgeTo(0, N); }
61
62   /// mergeWith - Merge the logical node pointed to by 'this' with the node
63   /// pointed to by 'N'.
64   ///
65   void mergeWith(const DSNodeHandle &N);
66
67   // hasLink - Return true if there is a link at the specified offset...
68   inline bool hasLink(unsigned Num) const;
69
70   /// getLink - Treat this current node pointer as a pointer to a structure of
71   /// some sort.  This method will return the pointer a mem[this+Num]
72   ///
73   inline const DSNodeHandle *getLink(unsigned Num) const;
74   inline DSNodeHandle *getLink(unsigned Num);
75
76   inline void setLink(unsigned Num, const DSNodeHandle &NH);
77 };
78
79
80 //===----------------------------------------------------------------------===//
81 /// DSNode - Data structure node class
82 ///
83 /// This class represents an untyped memory object of Size bytes.  It keeps
84 /// track of any pointers that have been stored into the object as well as the
85 /// different types represented in this object.
86 ///
87 class DSNode {
88   /// Links - Contains one entry for every _distinct_ pointer field in the
89   /// memory block.  These are demand allocated and indexed by the MergeMap
90   /// vector.
91   ///
92   std::vector<DSNodeHandle> Links;
93
94   /// MergeMap - Maps from every byte in the object to a signed byte number.
95   /// This map is neccesary due to the merging that is possible as part of the
96   /// unification algorithm.  To merge two distinct bytes of the object together
97   /// into a single logical byte, the indexes for the two bytes are set to the
98   /// same value.  This fully general merging is capable of representing all
99   /// manners of array merging if neccesary.
100   ///
101   /// This map is also used to map outgoing pointers to various byte offsets in
102   /// this data structure node.  If this value is >= 0, then it indicates that
103   /// the numbered entry in the Links vector contains the outgoing edge for this
104   /// byte offset.  In this way, the Links vector can be demand allocated and
105   /// byte elements of the node may be merged without needing a Link allocated
106   /// for it.
107   ///
108   /// Initially, each each element of the MergeMap is assigned a unique negative
109   /// number, which are then merged as the unification occurs.
110   ///
111   std::vector<signed char> MergeMap;
112
113   /// Referrers - Keep track of all of the node handles that point to this
114   /// DSNode.  These pointers may need to be updated to point to a different
115   /// node if this node gets merged with it.
116   ///
117   std::vector<DSNodeHandle*> Referrers;
118
119   /// TypeEntries - As part of the merging process of this algorithm, nodes of
120   /// different types can be represented by this single DSNode.  This vector is
121   /// kept sorted.
122   ///
123   typedef std::pair<const Type *, unsigned> TypeRec;
124   std::vector<TypeRec> TypeEntries;
125
126   /// Globals - The list of global values that are merged into this node.
127   ///
128   std::vector<GlobalValue*> Globals;
129
130   void operator=(const DSNode &); // DO NOT IMPLEMENT
131 public:
132   enum NodeTy {
133     ShadowNode = 0,        // Nothing is known about this node...
134     ScalarNode = 1 << 0,   // Scalar of the current function contains this value
135     AllocaNode = 1 << 1,   // This node was allocated with alloca
136     NewNode    = 1 << 2,   // This node was allocated with malloc
137     GlobalNode = 1 << 3,   // This node was allocated by a global var decl
138     Incomplete = 1 << 4,   // This node may not be complete
139   };
140   
141   /// NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
142   /// to the nodes in the data structure graph, so it is possible to have nodes
143   /// with a value of 0 for their NodeType.  Scalar and Alloca markers go away
144   /// when function graphs are inlined.
145   ///
146   unsigned char NodeType;
147
148   DSNode(enum NodeTy NT, const Type *T);
149   DSNode(const DSNode &);
150
151   ~DSNode() {
152 #ifndef NDEBUG
153     dropAllReferences();  // Only needed to satisfy assertion checks...
154     assert(Referrers.empty() && "Referrers to dead node exist!");
155 #endif
156   }
157
158   // Iterator for graph interface...
159   typedef DSNodeIterator iterator;
160   inline iterator begin();   // Defined in DSGraphTraits.h
161   inline iterator end();
162
163   //===--------------------------------------------------
164   // Accessors
165
166   // getSize - Return the maximum number of bytes occupied by this object...
167   unsigned getSize() const { return MergeMap.size(); }
168
169   // getTypeEntries - Return the possible types and their offsets in this object
170   const std::vector<TypeRec> &getTypeEntries() const { return TypeEntries; }
171
172   // getReferrers - Return a list of the pointers to this node...
173   const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
174
175
176   /// hasLink - Return true if this memory object has a link at the specified
177   /// location.
178   ///
179   bool hasLink(unsigned i) const {
180     assert(i < getSize() && "Field Link index is out of range!");
181     return MergeMap[i] >= 0;
182   }
183
184   DSNodeHandle *getLink(unsigned i) {
185     if (hasLink(i))
186       return &Links[MergeMap[i]];
187     return 0;
188   }
189   const DSNodeHandle *getLink(unsigned i) const {
190     if (hasLink(i))
191       return &Links[MergeMap[i]];
192     return 0;
193   }
194
195   int getMergeMapLabel(unsigned i) {
196     assert(i < MergeMap.size() && "MergeMap index out of range!");
197     return MergeMap[i];
198   }
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 i, const DSNodeHandle &NH);
205
206   /// addEdgeTo - Add an edge from the current node to the specified node.  This
207   /// can cause merging of nodes in the graph.
208   ///
209   void addEdgeTo(unsigned Offset, const DSNodeHandle &NH);
210
211   /// mergeWith - Merge this node and the specified node, moving all links to
212   /// and from the argument node into the current node, deleting the node
213   /// argument.  Offset indicates what offset the specified node is to be merged
214   /// into the current node.
215   ///
216   /// The specified node may be a null pointer (in which case, nothing happens).
217   ///
218   void mergeWith(const DSNodeHandle &NH, unsigned Offset);
219
220   /// mergeIndexes - If we discover that two indexes are equivalent and must be
221   /// merged, this function is used to do the dirty work.
222   ///
223   void mergeIndexes(unsigned idx1, unsigned idx2) {
224     assert(idx1 < getSize() && idx2 < getSize() && "Indexes out of range!");
225     signed char MV1 = MergeMap[idx1];
226     signed char MV2 = MergeMap[idx2];
227     if (MV1 != MV2)
228       mergeMappedValues(MV1, MV2);
229   }
230
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   void print(std::ostream &O, const DSGraph *G) const;
240   void dump() const;
241
242   void dropAllReferences() {
243     Links.clear();
244   }
245
246   /// remapLinks - Change all of the Links in the current node according to the
247   /// specified mapping.
248   void remapLinks(std::map<const DSNode*, DSNode*> &OldNodeMap);
249
250 private:
251   friend class DSNodeHandle;
252   // addReferrer - Keep the referrer set up to date...
253   void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
254   void removeReferrer(DSNodeHandle *H);
255
256   /// rewriteMergeMap - Loop over the mergemap, replacing any references to the
257   /// index From to be references to the index To.
258   ///
259   void rewriteMergeMap(signed char From, signed char To) {
260     assert(From != To && "Cannot change something into itself!");
261     for (unsigned i = 0, e = MergeMap.size(); i != e; ++i)
262       if (MergeMap[i] == From)
263         MergeMap[i] = To;
264   }
265
266   /// mergeMappedValues - This is the higher level form of rewriteMergeMap.  It
267   /// is fully capable of merging links together if neccesary as well as simply
268   /// rewriting the map entries.
269   ///
270   void mergeMappedValues(signed char V1, signed char V2);
271 };
272
273
274 //===----------------------------------------------------------------------===//
275 // Define inline DSNodeHandle functions that depend on the definition of DSNode
276 //
277
278 inline void DSNodeHandle::setNode(DSNode *n) {
279   if (N) N->removeReferrer(this);
280   N = n;
281   if (N) N->addReferrer(this);
282 }
283
284 inline bool DSNodeHandle::hasLink(unsigned Num) const {
285   assert(N && "DSNodeHandle does not point to a node yet!");
286   return N->hasLink(Num+Offset);
287 }
288
289
290 /// getLink - Treat this current node pointer as a pointer to a structure of
291 /// some sort.  This method will return the pointer a mem[this+Num]
292 ///
293 inline const DSNodeHandle *DSNodeHandle::getLink(unsigned Num) const {
294   assert(N && "DSNodeHandle does not point to a node yet!");
295   return N->getLink(Num+Offset);
296 }
297 inline DSNodeHandle *DSNodeHandle::getLink(unsigned Num) {
298   assert(N && "DSNodeHandle does not point to a node yet!");
299   return N->getLink(Num+Offset);
300 }
301
302 inline void DSNodeHandle::setLink(unsigned Num, const DSNodeHandle &NH) {
303   assert(N && "DSNodeHandle does not point to a node yet!");
304   N->setLink(Num+Offset, NH);
305 }
306
307 ///  addEdgeTo - Add an edge from the current node to the specified node.  This
308 /// can cause merging of nodes in the graph.
309 ///
310 inline void DSNodeHandle::addEdgeTo(unsigned LinkNo, const DSNodeHandle &Node) {
311   assert(N && "DSNodeHandle does not point to a node yet!");
312   N->addEdgeTo(LinkNo+Offset, Node);
313 }
314
315 /// mergeWith - Merge the logical node pointed to by 'this' with the node
316 /// pointed to by 'N'.
317 ///
318 inline void DSNodeHandle::mergeWith(const DSNodeHandle &Node) {
319   assert(N && "DSNodeHandle does not point to a node yet!");
320   N->mergeWith(Node, Offset);
321 }
322
323
324 //===----------------------------------------------------------------------===//
325 /// DSGraph - The graph that represents a function.
326 ///
327 class DSGraph {
328   Function *Func;
329   std::vector<DSNode*> Nodes;
330   DSNodeHandle RetNode;                          // Node that gets returned...
331   std::map<Value*, DSNodeHandle> ValueMap;
332
333 #if 0
334   // GlobalsGraph -- Reference to the common graph of globally visible objects.
335   // This includes GlobalValues, New nodes, Cast nodes, and Calls.
336   // 
337   GlobalDSGraph* GlobalsGraph;
338 #endif
339
340   // FunctionCalls - This vector maintains a single entry for each call
341   // instruction in the current graph.  Each call entry contains DSNodeHandles
342   // that refer to the arguments that are passed into the function call.  The
343   // first entry in the vector is the scalar that holds the return value for the
344   // call, the second is the function scalar being invoked, and the rest are
345   // pointer arguments to the function.
346   //
347   std::vector<std::vector<DSNodeHandle> > FunctionCalls;
348
349 #if 0
350   // OrigFunctionCalls - This vector retains a copy of the original function
351   // calls of the current graph.  This is needed to support top-down inlining
352   // after bottom-up inlining is complete, since the latter deletes call nodes.
353   // 
354   std::vector<std::vector<DSNodeHandle> > OrigFunctionCalls;
355
356   // PendingCallers - This vector records all unresolved callers of the
357   // current function, i.e., ones whose graphs have not been inlined into
358   // the current graph.  As long as there are unresolved callers, the nodes
359   // for formal arguments in the current graph cannot be eliminated, and
360   // nodes in the graph reachable from the formal argument nodes or
361   // global variable nodes must be considered incomplete. 
362   std::set<Function*> PendingCallers;
363 #endif
364   
365 protected:
366
367 #if 0
368   // clone all the call nodes and save the copies in OrigFunctionCalls
369   void saveOrigFunctionCalls() {
370     assert(OrigFunctionCalls.size() == 0 && "Do this only once!");
371     OrigFunctionCalls = FunctionCalls;
372   }
373
374   // get the saved copies of the original function call nodes
375   std::vector<std::vector<DSNodeHandle> > &getOrigFunctionCalls() {
376     return OrigFunctionCalls;
377   }
378 #endif
379
380   void operator=(const DSGraph &); // DO NOT IMPLEMENT
381 public:
382   DSGraph() : Func(0) {}           // Create a new, empty, DSGraph.
383   DSGraph(Function &F);            // Compute the local DSGraph
384   DSGraph(const DSGraph &DSG);     // Copy ctor
385   ~DSGraph();
386
387   bool hasFunction() const { return Func != 0; }
388   Function &getFunction() const { return *Func; }
389
390   /// getNodes - Get a vector of all the nodes in the graph
391   /// 
392   const std::vector<DSNode*> &getNodes() const { return Nodes; }
393         std::vector<DSNode*> &getNodes()       { return Nodes; }
394
395   /// addNode - Add a new node to the graph.
396   ///
397   void addNode(DSNode *N) { Nodes.push_back(N); }
398
399   /// getValueMap - Get a map that describes what the nodes the scalars in this
400   /// function point to...
401   ///
402   std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
403   const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
404
405   std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() {
406     return FunctionCalls;
407   }
408   const std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() const {
409     return FunctionCalls;
410   }
411
412   const DSNodeHandle &getRetNode() const { return RetNode; }
413         DSNodeHandle &getRetNode()       { return RetNode; }
414
415   unsigned getGraphSize() const {
416     return Nodes.size();
417   }
418
419   void print(std::ostream &O) const;
420   void dump() const;
421   void writeGraphToFile(std::ostream &O, const std::string &GraphName);
422
423   // maskNodeTypes - Apply a mask to all of the node types in the graph.  This
424   // is useful for clearing out markers like Scalar or Incomplete.
425   //
426   void maskNodeTypes(unsigned char Mask);
427   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
428
429   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
430   // modified by other functions that have not been resolved yet.  This marks
431   // nodes that are reachable through three sources of "unknownness":
432   //   Global Variables, Function Calls, and Incoming Arguments
433   //
434   // For any node that may have unknown components (because something outside
435   // the scope of current analysis may have modified it), the 'Incomplete' flag
436   // is added to the NodeType.
437   //
438   void markIncompleteNodes(bool markFormalArgs = true);
439
440   // removeTriviallyDeadNodes - After the graph has been constructed, this
441   // method removes all unreachable nodes that are created because they got
442   // merged with other nodes in the graph.
443   //
444   void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
445
446   // removeDeadNodes - Use a more powerful reachability analysis to eliminate
447   // subgraphs that are unreachable.  This often occurs because the data
448   // structure doesn't "escape" into it's caller, and thus should be eliminated
449   // from the caller's graph entirely.  This is only appropriate to use when
450   // inlining graphs.
451   //
452   void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
453
454 #if 0
455   // AddCaller - add a known caller node into the graph and mark it pending.
456   // getCallers - get a vector of the functions that call this one
457   // getCallersPending - get a matching vector of bools indicating if each
458   //                     caller's DSGraph has been resolved into this one.
459   // 
460   void addCaller(Function &caller) {
461     PendingCallers.insert(&caller);
462   }
463   std::set<Function*> &getPendingCallers() {
464     return PendingCallers;
465   }
466 #endif
467
468   // cloneInto - Clone the specified DSGraph into the current graph, returning
469   // the Return node of the graph.  The translated ValueMap for the old function
470   // is filled into the OldValMap member.
471   // If StripScalars (StripAllocas) is set to true, Scalar (Alloca) markers
472   // are removed from the graph as the graph is being cloned.
473   // If CopyCallers is set to true, the PendingCallers list is copied.
474   // If CopyOrigCalls is set to true, the OrigFunctionCalls list is copied.
475   //
476   DSNodeHandle cloneInto(const DSGraph &G,
477                          std::map<Value*, DSNodeHandle> &OldValMap,
478                          std::map<const DSNode*, DSNode*> &OldNodeMap,
479                          bool StripScalars = false, bool StripAllocas = false,
480                          bool CopyCallers = true, bool CopyOrigCalls = true);
481
482 #if 0
483   // cloneGlobalInto - Clone the given global node (or the node for the given
484   // GlobalValue) from the GlobalsGraph and all its target links (recursively).
485   // 
486   DSNode* cloneGlobalInto(const DSNode* GNode);
487   DSNode* cloneGlobalInto(GlobalValue* GV) {
488     assert(!GV || (((DSGraph*) GlobalsGraph)->ValueMap[GV] != 0));
489     return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ValueMap[GV]) : 0;
490   }
491 #endif
492
493 private:
494   bool isNodeDead(DSNode *N);
495 };
496
497 #endif