Added a first-class representation for each call site that can be
[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
12 class Type;
13 class DSGraph;
14 class DSNodeHandle;
15 class DSCallSite;
16 class LocalDataStructures;     // A collection of local graphs for a program
17 class BUDataStructures;        // A collection of bu graphs for a program
18 class TDDataStructures;        // A collection of td graphs for a program
19
20 // FIXME: move this stuff to a private header
21 namespace DataStructureAnalysis {
22   // isPointerType - Return true if this first class type is big enough to hold
23   // a pointer.
24   //
25   bool isPointerType(const Type *Ty);
26 }
27
28
29 // LocalDataStructures - The analysis that computes the local data structure
30 // graphs for all of the functions in the program.
31 //
32 // FIXME: This should be a Function pass that can be USED by a Pass, and would
33 // be automatically preserved.  Until we can do that, this is a Pass.
34 //
35 class LocalDataStructures : public Pass {
36   // DSInfo, one graph for each function
37   std::map<const Function*, DSGraph*> DSInfo;
38 public:
39   ~LocalDataStructures() { releaseMemory(); }
40
41   virtual bool run(Module &M);
42
43   // getDSGraph - Return the data structure graph for the specified function.
44   DSGraph &getDSGraph(const Function &F) const {
45     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
46     assert(I != DSInfo.end() && "Function not in module!");
47     return *I->second;
48   }
49
50   // print - Print out the analysis results...
51   void print(std::ostream &O, const Module *M) const;
52
53   // If the pass pipeline is done with this pass, we can release our memory...
54   virtual void releaseMemory();
55
56   // getAnalysisUsage - This obviously provides a data structure graph.
57   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58     AU.setPreservesAll();
59   }
60 };
61
62 // BUDataStructures - The analysis that computes the interprocedurally closed
63 // data structure graphs for all of the functions in the program.  This pass
64 // only performs a "Bottom Up" propogation (hence the name).
65 //
66 class BUDataStructures : public Pass {
67 private:
68   // DSInfo, one graph for each function
69   std::map<const Function*, DSGraph*> DSInfo;
70   std::map<const Function*, std::vector<DSCallSite> > CallSites;
71 public:
72   ~BUDataStructures() { releaseMemory(); }
73
74   virtual bool run(Module &M);
75
76   // getDSGraph - Return the data structure graph for the specified function.
77   DSGraph &getDSGraph(const Function &F) const {
78     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
79     assert(I != DSInfo.end() && "Function not in module!");
80     return *I->second;
81   }
82
83   const std::vector<DSCallSite> *getCallSites(const Function &F) const {
84     std::map<const Function*, std::vector<DSCallSite> >::const_iterator I
85       = CallSites.find(&F);
86     return I != CallSites.end() ? &I->second : 0;
87   }
88
89   // print - Print out the analysis results...
90   void print(std::ostream &O, const Module *M) const;
91
92   // If the pass pipeline is done with this pass, we can release our memory...
93   virtual void releaseMemory();
94
95   // getAnalysisUsage - This obviously provides a data structure graph.
96   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97     AU.setPreservesAll();
98     AU.addRequired<LocalDataStructures>();
99   }
100 private:
101   DSGraph &calculateGraph(Function &F);
102 };
103
104 // TDDataStructures - Analysis that computes new data structure graphs
105 // for each function using the closed graphs for the callers computed
106 // by the bottom-up pass.
107 //
108 class TDDataStructures : public Pass {
109   // DSInfo, one graph for each function
110   std::map<const Function*, DSGraph*> DSInfo;
111 public:
112   ~TDDataStructures() { releaseMemory(); }
113
114   virtual bool run(Module &M);
115
116   // getDSGraph - Return the data structure graph for the specified function.
117   DSGraph &getDSGraph(const Function &F) const {
118     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
119     assert(I != DSInfo.end() && "Function not in module!");
120     return *I->second;
121   }
122
123   // print - Print out the analysis results...
124   void print(std::ostream &O, const Module *M) const;
125
126   // If the pass pipeline is done with this pass, we can release our memory...
127   virtual void releaseMemory();
128
129   // getAnalysisUsage - This obviously provides a data structure graph.
130   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
131     AU.setPreservesAll();
132     AU.addRequired<BUDataStructures>();
133   }
134 private:
135   DSGraph &calculateGraph(Function &F);
136
137   void ResolveCallSite(DSGraph &Graph,
138                        const DSCallSite &CallSite);
139 };
140
141 #if 0
142 // GlobalDSGraph - A common graph for all the globals and their outgoing links
143 // to externally visible nodes.  This includes GlobalValues, New nodes,
144 // Cast nodes, and Calls.  This graph can only be used by one of the
145 // individual function graphs, and it goes away when they all go away.
146 // 
147 class GlobalDSGraph : public DSGraph {
148   hash_set<const DSGraph*> Referrers;
149   void addReference(const DSGraph* referrer);
150   void removeReference(const DSGraph* referrer);
151   friend class DSGraph;                           // give access to Referrers
152   
153   GlobalDSGraph(const GlobalDSGraph &GlobalDSG);  // Do not implement
154
155   // Helper function for cloneGlobals and cloneCalls
156   DSNode* cloneNodeInto(DSNode *OldNode,
157                         std::map<const DSNode*, DSNode*> &NodeCache,
158                         bool GlobalsAreFinal = false);
159
160 public:
161   GlobalDSGraph();                                // Create an empty DSGraph
162   virtual ~GlobalDSGraph();
163
164   void    cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
165   void    cloneCalls  (DSGraph& Graph);
166 };
167 #endif
168
169 #endif