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