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