1 //===- DataStructure.h - Build data structure graphs ------------*- C++ -*-===//
3 // Implement the LLVM data structure analysis library.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
8 #define LLVM_ANALYSIS_DATA_STRUCTURE_H
10 #include "llvm/Pass.h"
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
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
26 bool isPointerType(const Type *Ty);
30 // LocalDataStructures - The analysis that computes the local data structure
31 // graphs for all of the functions in the program.
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.
36 class LocalDataStructures : public Pass {
37 // DSInfo, one graph for each function
38 std::map<const Function*, DSGraph*> DSInfo;
40 ~LocalDataStructures() { releaseMemory(); }
42 virtual bool run(Module &M);
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!");
51 // print - Print out the analysis results...
52 void print(std::ostream &O, const Module *M) const;
54 // If the pass pipeline is done with this pass, we can release our memory...
55 virtual void releaseMemory();
57 // getAnalysisUsage - This obviously provides a data structure graph.
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
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).
67 class BUDataStructures : public Pass {
69 // DSInfo, one graph for each function
70 std::map<const Function*, DSGraph*> DSInfo;
71 std::map<const Function*, std::vector<DSCallSite> > CallSites;
73 ~BUDataStructures() { releaseMemory(); }
75 virtual bool run(Module &M);
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!");
84 const std::vector<DSCallSite> *getCallSites(const Function &F) const {
85 std::map<const Function*, std::vector<DSCallSite> >::const_iterator I
87 return I != CallSites.end() ? &I->second : 0;
90 // print - Print out the analysis results...
91 void print(std::ostream &O, const Module *M) const;
93 // If the pass pipeline is done with this pass, we can release our memory...
94 virtual void releaseMemory();
96 // getAnalysisUsage - This obviously provides a data structure graph.
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.addRequired<LocalDataStructures>();
102 DSGraph &calculateGraph(Function &F);
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.
109 class TDDataStructures : public Pass {
110 // DSInfo, one graph for each function
111 std::map<const Function*, DSGraph*> DSInfo;
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.
119 typedef std::map<const DSNode*, DSNodeHandle> BUNodeMapTy;
120 std::map<const Function*, BUNodeMapTy> BUMaps;
122 ~TDDataStructures() { releaseMemory(); }
124 virtual bool run(Module &M);
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!");
133 // print - Print out the analysis results...
134 void print(std::ostream &O, const Module *M) const;
136 // If the pass pipeline is done with this pass, we can release our memory...
137 virtual void releaseMemory();
139 // getAnalysisUsage - This obviously provides a data structure graph.
140 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesAll();
142 AU.addRequired<BUDataStructures>();
145 DSGraph &calculateGraph(Function &F);
147 void ResolveCallSite(DSGraph &Graph, const DSCallSite &CallSite);
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.
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
162 GlobalDSGraph(const GlobalDSGraph &GlobalDSG); // Do not implement
164 // Helper function for cloneGlobals and cloneCalls
165 DSNode* cloneNodeInto(DSNode *OldNode,
166 std::map<const DSNode*, DSNode*> &NodeCache,
167 bool GlobalsAreFinal = false);
170 GlobalDSGraph(); // Create an empty DSGraph
171 virtual ~GlobalDSGraph();
173 void cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
174 void cloneCalls (DSGraph& Graph);