Fix comments
[oota-llvm.git] / include / llvm / Analysis / DataStructure.h
1 //===- DataStructure.h - Build data structure graphs -------------*- C++ -*--=//
2 //
3 // Implement the LLVM data structure analysis library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
8 #define LLVM_ANALYSIS_DATA_STRUCTURE_H
9
10 #include "llvm/Pass.h"
11 #include "llvm/GlobalValue.h"
12 #include "Support/HashExtras.h"
13 #include "Support/hash_set"
14 #include <set>
15 #include <string>
16
17 class Type;
18 class GlobalValue;
19 class DSNode;                  // Each node in the graph
20 class DSGraph;                 // A graph for a function
21 class GlobalDSGraph;           // A common graph for globals in a program 
22 class DSNodeIterator;          // Data structure graph traversal iterator
23 class LocalDataStructures;     // A collection of local graphs for a program
24 class BUDataStructures;        // A collection of bu graphs for a program
25 class TDDataStructures;        // A collection of td graphs for a program
26
27 //===----------------------------------------------------------------------===//
28 // DSNodeHandle - Implement a "handle" to a data structure node that takes care
29 // of all of the add/un'refing of the node to prevent the backpointers in the
30 // graph from getting out of date.
31 //
32 class DSNodeHandle {
33   DSNode *N;
34 public:
35   // Allow construction, destruction, and assignment...
36   DSNodeHandle(DSNode *n = 0) : N(0) { operator=(n); }
37   DSNodeHandle(const DSNodeHandle &H) : N(0) { operator=(H.N); }
38   ~DSNodeHandle() { operator=(0); }
39   DSNodeHandle &operator=(const DSNodeHandle &H) {operator=(H.N); return *this;}
40
41   // Assignment of DSNode*, implement all of the add/un'refing (defined later)
42   inline DSNodeHandle &operator=(DSNode *n);
43
44   // Allow automatic, implicit, conversion to DSNode*
45   operator       DSNode*()       { return N; }
46   operator const DSNode*() const { return N; }
47   operator const DSNode*()       { return N; }
48   operator bool() const          { return N != 0; }
49   operator bool()                { return N != 0; }
50
51   bool operator<(const DSNodeHandle &H) const {  // Allow sorting
52     return N < H.N;
53   }
54   bool operator==(const DSNodeHandle &H) const { return N == H.N; }
55   bool operator!=(const DSNodeHandle &H) const { return N != H.N; }
56   bool operator==(const DSNode *Node) const { return N == Node; }
57   bool operator!=(const DSNode *Node) const { return N != Node; }
58   bool operator==(DSNode *Node) const { return N == Node; }
59   bool operator!=(DSNode *Node) const { return N != Node; }
60
61   // Avoid having comparisons to null cause errors...
62   bool operator==(int X) const {
63     assert(X == 0 && "Bad comparison!");
64     return operator==((DSNode*)0);
65   }
66   bool operator!=(int X) const { return !operator==(X); }
67
68   // Allow explicit conversion to DSNode...
69   DSNode *get() { return N; }
70   const DSNode *get() const { return N; }
71
72   // Allow this to be treated like a pointer...
73   DSNode *operator->() { return N; }
74   const DSNode *operator->() const { return N; }
75 };
76
77
78 //===----------------------------------------------------------------------===//
79 // DSNode - Data structure node class
80 //
81 // This class keeps track of a node's type, and the fields in the data
82 // structure.
83 //
84 //
85 class DSNode {
86   const Type *Ty;
87   std::vector<DSNodeHandle> Links;
88   std::vector<DSNodeHandle*> Referrers;
89
90   // Globals - The list of global values that are merged into this node.
91   std::vector<GlobalValue*> Globals;
92
93   void operator=(const DSNode &); // DO NOT IMPLEMENT
94 public:
95   enum NodeTy {
96     ShadowNode = 0,        // Nothing is known about this node...
97     ScalarNode = 1 << 0,   // Scalar of the current function contains this value
98     AllocaNode = 1 << 1,   // This node was allocated with alloca
99     NewNode    = 1 << 2,   // This node was allocated with malloc
100     GlobalNode = 1 << 3,   // This node was allocated by a global var decl
101     SubElement = 1 << 4,   // This node is a part of some other node
102     CastNode   = 1 << 5,   // This node is accessed in unsafe ways
103     Incomplete = 1 << 6,   // This node may not be complete
104   };
105
106   // NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
107   // to the nodes in the data structure graph, so it is possible to have nodes
108   // with a value of 0 for their NodeType.  Scalar and Alloca markers go away
109   // when function graphs are inlined.
110   //
111   unsigned char NodeType;
112
113   DSNode(enum NodeTy NT, const Type *T);
114   DSNode(const DSNode &);
115
116   ~DSNode() {
117 #ifndef NDEBUG
118     dropAllReferences();  // Only needed to satisfy assertion checks...
119 #endif
120     assert(Referrers.empty() && "Referrers to dead node exist!");
121   }
122
123   // Iterator for graph interface...
124   typedef DSNodeIterator iterator;
125   inline iterator begin();   // Defined in DataStructureGraph.h
126   inline iterator end();
127
128   // Accessors
129   const Type *getType() const { return Ty; }
130
131   unsigned getNumLinks() const { return Links.size(); }
132   DSNode *getLink(unsigned i) {
133     assert(i < getNumLinks() && "Field links access out of range...");
134     return Links[i];
135   }
136   const DSNode *getLink(unsigned i) const {
137     assert(i < getNumLinks() && "Field links access out of range...");
138     return Links[i];
139   }
140
141   void setLink(unsigned i, DSNode *N) {
142     assert(i < getNumLinks() && "Field links access out of range...");
143     Links[i] = N;
144   }
145
146   // addGlobal - Add an entry for a global value to the Globals list.  This also
147   // marks the node with the 'G' flag if it does not already have it.
148   //
149   void addGlobal(GlobalValue *GV);
150   const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
151   std::vector<GlobalValue*> &getGlobals() { return Globals; }
152
153   // addEdgeTo - Add an edge from the current node to the specified node.  This
154   // can cause merging of nodes in the graph.
155   //
156   void addEdgeTo(unsigned LinkNo, DSNode *N);
157   void addEdgeTo(DSNode *N) {
158     assert(getNumLinks() == 1 && "Must specify a field number to add edge if "
159            " more than one field exists!");
160     addEdgeTo(0, N);
161   }
162
163   // mergeWith - Merge this node into the specified node, moving all links to
164   // and from the argument node into the current node.  The specified node may
165   // be a null pointer (in which case, nothing happens).
166   //
167   void mergeWith(DSNode *N);
168
169   // addReferrer - Keep the referrer set up to date...
170   void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
171   void removeReferrer(DSNodeHandle *H);
172   const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }
173
174   void print(std::ostream &O, const DSGraph *G) const;
175   void dump() const;
176
177   std::string getCaption(const DSGraph *G) const;
178
179   void dropAllReferences() {
180     Links.clear();
181   }
182 };
183
184
185 inline DSNodeHandle &DSNodeHandle::operator=(DSNode *n) {
186   if (N) N->removeReferrer(this);
187   N = n;
188   if (N) N->addReferrer(this);
189   return *this;
190 }
191
192
193 // DSGraph - The graph that represents a function.
194 //
195 class DSGraph {
196   friend class GlobalDSGraph;
197 protected:
198   Function &Func;
199   std::vector<DSNode*> Nodes;
200   DSNodeHandle RetNode;                          // Node that gets returned...
201   std::map<Value*, DSNodeHandle> ValueMap;
202
203   // GlobalsGraph -- Reference to the common graph of globally visible objects.
204   // This includes GlobalValues, New nodes, Cast nodes, and Calls.
205   // 
206   GlobalDSGraph* GlobalsGraph;
207
208   // FunctionCalls - This vector maintains a single entry for each call
209   // instruction in the current graph.  Each call entry contains DSNodeHandles
210   // that refer to the arguments that are passed into the function call.  The
211   // first entry in the vector is the scalar that holds the return value for the
212   // call, the second is the function scalar being invoked, and the rest are
213   // pointer arguments to the function.
214   //
215   std::vector<std::vector<DSNodeHandle> > FunctionCalls;
216
217   // OrigFunctionCalls - This vector retains a copy of the original function
218   // calls of the current graph.  This is needed to support top-down inlining
219   // after bottom-up inlining is complete, since the latter deletes call nodes.
220   // 
221   std::vector<std::vector<DSNodeHandle> > OrigFunctionCalls;
222
223   // PendingCallers - This vector records all unresolved callers of the
224   // current function, i.e., ones whose graphs have not been inlined into
225   // the current graph.  As long as there are unresolved callers, the nodes
226   // for formal arguments in the current graph cannot be eliminated, and
227   // nodes in the graph reachable from the formal argument nodes or
228   // global variable nodes must be considered incomplete. 
229   std::set<Function*> PendingCallers;
230   
231 protected:
232   // Define the interface only accessable to DataStructure
233   friend class LocalDataStructures;
234   friend class BUDataStructures;
235   friend class TDDataStructures;
236   DSGraph(Function &F, GlobalDSGraph* GlobalsG); // Compute the local DSGraph
237   DSGraph(const DSGraph &DSG);     // Copy ctor
238   virtual ~DSGraph();
239
240   // clone all the call nodes and save the copies in OrigFunctionCalls
241   void saveOrigFunctionCalls() {
242     assert(OrigFunctionCalls.size() == 0 && "Do this only once!");
243     OrigFunctionCalls = FunctionCalls;
244   }
245   
246   // get the saved copies of the original function call nodes
247   std::vector<std::vector<DSNodeHandle> > &getOrigFunctionCalls() {
248     return OrigFunctionCalls;
249   }
250
251   void operator=(const DSGraph &); // DO NOT IMPLEMENT
252 public:
253
254   Function &getFunction() const { return Func; }
255
256   // getNodes - Get a vector of all the nodes in the graph
257   // 
258   const std::vector<DSNode*>& getNodes() const { return Nodes; }
259         std::vector<DSNode*>& getNodes()       { return Nodes; }
260
261   // getValueMap - Get a map that describes what the nodes the scalars in this
262   // function point to...
263   //
264   std::map<Value*, DSNodeHandle> &getValueMap() { return ValueMap; }
265   const std::map<Value*, DSNodeHandle> &getValueMap() const { return ValueMap;}
266
267   std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() {
268     return FunctionCalls;
269   }
270   const std::vector<std::vector<DSNodeHandle> > &getFunctionCalls() const {
271     return FunctionCalls;
272   }
273
274   const DSNode *getRetNode() const { return RetNode; }
275         DSNode *getRetNode()       { return RetNode; }
276
277   unsigned getGraphSize() const {
278     return Nodes.size();
279   }
280
281   void print(std::ostream &O) const;
282   void dump() const;
283
284   // maskNodeTypes - Apply a mask to all of the node types in the graph.  This
285   // is useful for clearing out markers like Scalar or Incomplete.
286   //
287   void maskNodeTypes(unsigned char Mask);
288   void maskIncompleteMarkers() { maskNodeTypes(~DSNode::Incomplete); }
289
290   // markIncompleteNodes - Traverse the graph, identifying nodes that may be
291   // modified by other functions that have not been resolved yet.  This marks
292   // nodes that are reachable through three sources of "unknownness":
293   //   Global Variables, Function Calls, and Incoming Arguments
294   //
295   // For any node that may have unknown components (because something outside
296   // the scope of current analysis may have modified it), the 'Incomplete' flag
297   // is added to the NodeType.
298   //
299   void markIncompleteNodes(bool markFormalArgs = true);
300
301   // removeTriviallyDeadNodes - After the graph has been constructed, this
302   // method removes all unreachable nodes that are created because they got
303   // merged with other nodes in the graph.
304   //
305   void removeTriviallyDeadNodes(bool KeepAllGlobals = false);
306
307   // removeDeadNodes - Use a more powerful reachability analysis to eliminate
308   // subgraphs that are unreachable.  This often occurs because the data
309   // structure doesn't "escape" into it's caller, and thus should be eliminated
310   // from the caller's graph entirely.  This is only appropriate to use when
311   // inlining graphs.
312   //
313   void removeDeadNodes(bool KeepAllGlobals = false, bool KeepCalls = true);
314
315   // AddCaller - add a known caller node into the graph and mark it pending.
316   // getCallers - get a vector of the functions that call this one
317   // getCallersPending - get a matching vector of bools indicating if each
318   //                     caller's DSGraph has been resolved into this one.
319   // 
320   void addCaller(Function& caller) {
321     PendingCallers.insert(&caller);
322   }
323   std::set<Function*>& getPendingCallers() {
324     return PendingCallers;
325   }
326
327   // cloneInto - Clone the specified DSGraph into the current graph, returning
328   // the Return node of the graph.  The translated ValueMap for the old function
329   // is filled into the OldValMap member.
330   // If StripScalars (StripAllocas) is set to true, Scalar (Alloca) markers
331   // are removed from the graph as the graph is being cloned.
332   // If CopyCallers is set to true, the PendingCallers list is copied.
333   // If CopyOrigCalls is set to true, the OrigFunctionCalls list is copied.
334   //
335   DSNode *cloneInto(const DSGraph &G, std::map<Value*, DSNodeHandle> &OldValMap,
336                     std::map<const DSNode*, DSNode*>& OldNodeMap,
337                     bool StripScalars = false, bool StripAllocas = false,
338                     bool CopyCallers = true, bool CopyOrigCalls = true);
339
340   // cloneGlobalInto - Clone the given global node (or the node for the given
341   // GlobalValue) from the GlobalsGraph and all its target links (recursively).
342   // 
343   DSNode* cloneGlobalInto(const DSNode* GNode);
344   DSNode* cloneGlobalInto(GlobalValue* GV) {
345     assert(!GV || (((DSGraph*) GlobalsGraph)->ValueMap[GV] != 0));
346     return GV? cloneGlobalInto(((DSGraph*) GlobalsGraph)->ValueMap[GV]) : 0;
347   }
348
349 private:
350   bool isNodeDead(DSNode *N);
351 };
352
353
354 // GlobalDSGraph - A common graph for all the globals and their outgoing links
355 // to externally visible nodes.  This includes GlobalValues, New nodes,
356 // Cast nodes, and Calls.  This graph can only be used by one of the
357 // individual function graphs, and it goes away when they all go away.
358 // 
359 class GlobalDSGraph : public DSGraph {
360   hash_set<const DSGraph*> Referrers;
361   void addReference(const DSGraph* referrer);
362   void removeReference(const DSGraph* referrer);
363   friend class DSGraph;                           // give access to Referrers
364   
365   GlobalDSGraph(const GlobalDSGraph &GlobalDSG);  // Do not implement
366
367   // Helper function for cloneGlobals and cloneCalls
368   DSNode* cloneNodeInto(DSNode *OldNode,
369                         std::map<const DSNode*, DSNode*> &NodeCache,
370                         bool GlobalsAreFinal = false);
371
372 public:
373   GlobalDSGraph();                                // Create an empty DSGraph
374   virtual ~GlobalDSGraph();
375
376   void    cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
377   void    cloneCalls  (DSGraph& Graph);
378 };
379
380
381 // LocalDataStructures - The analysis that computes the local data structure
382 // graphs for all of the functions in the program.
383 //
384 // FIXME: This should be a Function pass that can be USED by a Pass, and would
385 // be automatically preserved.  Until we can do that, this is a Pass.
386 //
387 class LocalDataStructures : public Pass {
388   // DSInfo, one graph for each function
389   std::map<const Function*, DSGraph*> DSInfo;
390 public:
391   ~LocalDataStructures() { releaseMemory(); }
392
393   virtual bool run(Module &M);
394
395   // getDSGraph - Return the data structure graph for the specified function.
396   DSGraph &getDSGraph(const Function &F) const {
397     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
398     assert(I != DSInfo.end() && "Function not in module!");
399     return *I->second;
400   }
401
402   // print - Print out the analysis results...
403   void print(std::ostream &O, const Module *M) const;
404
405   // If the pass pipeline is done with this pass, we can release our memory...
406   virtual void releaseMemory();
407
408   // getAnalysisUsage - This obviously provides a data structure graph.
409   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
410     AU.setPreservesAll();
411   }
412 };
413
414
415 // BUDataStructures - The analysis that computes the interprocedurally closed
416 // data structure graphs for all of the functions in the program.  This pass
417 // only performs a "Bottom Up" propogation (hence the name).
418 //
419 class BUDataStructures : public Pass {
420   // DSInfo, one graph for each function
421   std::map<const Function*, DSGraph*> DSInfo;
422 public:
423   ~BUDataStructures() { releaseMemory(); }
424
425   virtual bool run(Module &M);
426
427   // getDSGraph - Return the data structure graph for the specified function.
428   DSGraph &getDSGraph(const Function &F) const {
429     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
430     assert(I != DSInfo.end() && "Function not in module!");
431     return *I->second;
432   }
433
434   // print - Print out the analysis results...
435   void print(std::ostream &O, const Module *M) const;
436
437   // If the pass pipeline is done with this pass, we can release our memory...
438   virtual void releaseMemory();
439
440   // getAnalysisUsage - This obviously provides a data structure graph.
441   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
442     AU.setPreservesAll();
443     AU.addRequired<LocalDataStructures>();
444   }
445 private:
446   DSGraph &calculateGraph(Function &F);
447 };
448
449
450 // TDDataStructures - Analysis that computes new data structure graphs
451 // for each function using the closed graphs for the callers computed
452 // by the bottom-up pass.
453 //
454 class TDDataStructures : public Pass {
455   // DSInfo, one graph for each function
456   std::map<const Function*, DSGraph*> DSInfo;
457 public:
458   ~TDDataStructures() { releaseMemory(); }
459
460   virtual bool run(Module &M);
461
462   // getDSGraph - Return the data structure graph for the specified function.
463   DSGraph &getDSGraph(const Function &F) const {
464     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
465     assert(I != DSInfo.end() && "Function not in module!");
466     return *I->second;
467   }
468
469   // print - Print out the analysis results...
470   void print(std::ostream &O, const Module *M) const;
471
472   // If the pass pipeline is done with this pass, we can release our memory...
473   virtual void releaseMemory();
474
475   // getAnalysisUsage - This obviously provides a data structure graph.
476   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
477     AU.setPreservesAll();
478     AU.addRequired<BUDataStructures>();
479   }
480 private:
481   DSGraph &calculateGraph(Function &F);
482   void pushGraphIntoCallee(DSGraph &callerGraph, DSGraph &calleeGraph,
483                            std::map<Value*, DSNodeHandle> &OldValMap,
484                            std::map<const DSNode*, DSNode*> &OldNodeMap);
485 };
486 #endif