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