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" propagation (hence the name).
67 class BUDataStructures : public Pass {
68 // DSInfo, one graph for each function
69 std::map<const Function*, DSGraph*> DSInfo;
70 std::map<const Function*, std::vector<DSCallSite> > CallSites;
72 ~BUDataStructures() { releaseMemory(); }
74 virtual bool run(Module &M);
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!");
83 const std::vector<DSCallSite> *getCallSites(const Function &F) const {
84 std::map<const Function*, std::vector<DSCallSite> >::const_iterator I
86 return I != CallSites.end() ? &I->second : 0;
89 // print - Print out the analysis results...
90 void print(std::ostream &O, const Module *M) const;
92 // If the pass pipeline is done with this pass, we can release our memory...
93 virtual void releaseMemory();
95 // getAnalysisUsage - This obviously provides a data structure graph.
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.addRequired<LocalDataStructures>();
101 DSGraph &calculateGraph(Function &F);
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.
108 class TDDataStructures : public Pass {
109 // DSInfo, one graph for each function
110 std::map<const Function*, DSGraph*> DSInfo;
112 // Each graph in DSInfo is based on a graph in the BUDS object. The BUMaps
113 // member keeps the mappings from the BU graphs to the TD graphs as they are
114 // calculated by calculateGraph. This information is used to properly
115 // implement resolving of call sites, where the call sites in the BUGraph are
116 // in terms of the caller function's graph in the BUGraph.
118 typedef std::map<const DSNode*, DSNodeHandle> BUNodeMapTy;
119 std::map<const Function*, BUNodeMapTy> BUMaps;
121 // CallSitesForFunction - This is a temporary map that is only kept around
122 // when building the top-down closures for a program. It traverses all of the
123 // call sites in the BU graph and holds all of the call sites that each
124 // function is the "resolving caller" for.
126 std::map<const Function*,
127 std::vector<const DSCallSite*> > CallSitesForFunction;
130 ~TDDataStructures() { releaseMemory(); }
132 virtual bool run(Module &M);
134 // getDSGraph - Return the data structure graph for the specified function.
135 DSGraph &getDSGraph(const Function &F) const {
136 std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
137 assert(I != DSInfo.end() && "Function not in module!");
141 // print - Print out the analysis results...
142 void print(std::ostream &O, const Module *M) const;
144 // If the pass pipeline is done with this pass, we can release our memory...
145 virtual void releaseMemory();
147 // getAnalysisUsage - This obviously provides a data structure graph.
148 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
149 AU.setPreservesAll();
150 AU.addRequired<BUDataStructures>();
153 DSGraph &calculateGraph(Function &F);
155 void ResolveCallSite(DSGraph &Graph, const DSCallSite &CallSite);
159 // GlobalDSGraph - A common graph for all the globals and their outgoing links
160 // to externally visible nodes. This includes GlobalValues, New nodes,
161 // Cast nodes, and Calls. This graph can only be used by one of the
162 // individual function graphs, and it goes away when they all go away.
164 class GlobalDSGraph : public DSGraph {
165 hash_set<const DSGraph*> Referrers;
166 void addReference(const DSGraph* referrer);
167 void removeReference(const DSGraph* referrer);
168 friend class DSGraph; // give access to Referrers
170 GlobalDSGraph(const GlobalDSGraph &GlobalDSG); // Do not implement
172 // Helper function for cloneGlobals and cloneCalls
173 DSNode* cloneNodeInto(DSNode *OldNode,
174 std::map<const DSNode*, DSNode*> &NodeCache,
175 bool GlobalsAreFinal = false);
178 GlobalDSGraph(); // Create an empty DSGraph
179 virtual ~GlobalDSGraph();
181 void cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
182 void cloneCalls (DSGraph& Graph);