Add another option to cloneGraph
[oota-llvm.git] / include / llvm / Analysis / DataStructure / 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/Analysis/DSSupport.h"
11 #include "llvm/Pass.h"
12 #include <set>
13
14 class Type;
15 class DSGraph;
16 class DSNode;
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" propagation (hence the name).
66 //
67 class BUDataStructures : public Pass {
68   // DSInfo, one graph for each function
69   std::map<const Function*, DSGraph*> DSInfo;
70 public:
71   ~BUDataStructures() { releaseMemory(); }
72
73   virtual bool run(Module &M);
74
75   // getDSGraph - Return the data structure graph for the specified function.
76   DSGraph &getDSGraph(const Function &F) const {
77     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
78     assert(I != DSInfo.end() && "Function not in module!");
79     return *I->second;
80   }
81
82    // print - Print out the analysis results...
83   void print(std::ostream &O, const Module *M) const;
84
85   // If the pass pipeline is done with this pass, we can release our memory...
86   virtual void releaseMemory();
87
88   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
89     AU.setPreservesAll();
90     AU.addRequired<LocalDataStructures>();
91   }
92 private:
93   DSGraph &calculateGraph(Function &F);
94 };
95
96 // TDDataStructures - Analysis that computes new data structure graphs
97 // for each function using the closed graphs for the callers computed
98 // by the bottom-up pass.
99 //
100 class TDDataStructures : public Pass {
101   // DSInfo, one graph for each function
102   std::map<const Function*, DSGraph*> DSInfo;
103   std::set<const Function*> GraphDone;
104 public:
105   ~TDDataStructures() { releaseMemory(); }
106
107   virtual bool run(Module &M);
108
109   // getDSGraph - Return the data structure graph for the specified function.
110   DSGraph &getDSGraph(const Function &F) const {
111     std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
112     assert(I != DSInfo.end() && "Function not in module!");
113     return *I->second;
114   }
115
116   // print - Print out the analysis results...
117   void print(std::ostream &O, const Module *M) const;
118
119   // If the pass pipeline is done with this pass, we can release our memory...
120   virtual void releaseMemory();
121
122   // getAnalysisUsage - This obviously provides a data structure graph.
123   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124     AU.setPreservesAll();
125     AU.addRequired<BUDataStructures>();
126   }
127 private:
128   void calculateGraph(Function &F);
129   DSGraph &getOrCreateDSGraph(Function &F);
130
131   void ResolveCallSite(DSGraph &Graph, const DSCallSite &CallSite);
132 };
133
134 #if 0
135 // GlobalDSGraph - A common graph for all the globals and their outgoing links
136 // to externally visible nodes.  This includes GlobalValues, New nodes,
137 // Cast nodes, and Calls.  This graph can only be used by one of the
138 // individual function graphs, and it goes away when they all go away.
139 // 
140 class GlobalDSGraph : public DSGraph {
141   hash_set<const DSGraph*> Referrers;
142   void addReference(const DSGraph* referrer);
143   void removeReference(const DSGraph* referrer);
144   friend class DSGraph;                           // give access to Referrers
145   
146   GlobalDSGraph(const GlobalDSGraph &GlobalDSG);  // Do not implement
147
148   // Helper function for cloneGlobals and cloneCalls
149   DSNode* cloneNodeInto(DSNode *OldNode,
150                         std::map<const DSNode*, DSNode*> &NodeCache,
151                         bool GlobalsAreFinal = false);
152
153 public:
154   GlobalDSGraph();                                // Create an empty DSGraph
155   virtual ~GlobalDSGraph();
156
157   void    cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
158   void    cloneCalls  (DSGraph& Graph);
159 };
160 #endif
161
162 #endif