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/Analysis/DSSupport.h"
11 #include "llvm/Pass.h"
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
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
25 bool isPointerType(const Type *Ty);
29 // LocalDataStructures - The analysis that computes the local data structure
30 // graphs for all of the functions in the program.
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.
35 class LocalDataStructures : public Pass {
36 // DSInfo, one graph for each function
37 std::map<const Function*, DSGraph*> DSInfo;
39 ~LocalDataStructures() { releaseMemory(); }
41 virtual bool run(Module &M);
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!");
50 // print - Print out the analysis results...
51 void print(std::ostream &O, const Module *M) const;
53 // If the pass pipeline is done with this pass, we can release our memory...
54 virtual void releaseMemory();
56 // getAnalysisUsage - This obviously provides a data structure graph.
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
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" propagation (hence the name).
66 class BUDataStructures : public Pass {
67 // DSInfo, one graph for each function
68 std::map<const Function*, DSGraph*> DSInfo;
69 std::map<const Function*, std::vector<DSCallSite> > CallSites;
71 ~BUDataStructures() { releaseMemory(); }
73 virtual bool run(Module &M);
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!");
82 /// getCallSites - Return all of the call sites for the specified function
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 // CallSitesForFunction - This is a temporary map that is only kept around
123 // when building the top-down closures for a program. It traverses all of the
124 // call sites in the BU graph and holds all of the call sites that each
125 // function is the "resolving caller" for.
127 std::map<const Function*,
128 std::vector<const DSCallSite*> > CallSitesForFunction;
131 ~TDDataStructures() { releaseMemory(); }
133 virtual bool run(Module &M);
135 // getDSGraph - Return the data structure graph for the specified function.
136 DSGraph &getDSGraph(const Function &F) const {
137 std::map<const Function*, DSGraph*>::const_iterator I = DSInfo.find(&F);
138 assert(I != DSInfo.end() && "Function not in module!");
142 // print - Print out the analysis results...
143 void print(std::ostream &O, const Module *M) const;
145 // If the pass pipeline is done with this pass, we can release our memory...
146 virtual void releaseMemory();
148 // getAnalysisUsage - This obviously provides a data structure graph.
149 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
151 AU.addRequired<BUDataStructures>();
154 DSGraph &calculateGraph(Function &F);
156 void ResolveCallSite(DSGraph &Graph, const DSCallSite &CallSite);
160 // GlobalDSGraph - A common graph for all the globals and their outgoing links
161 // to externally visible nodes. This includes GlobalValues, New nodes,
162 // Cast nodes, and Calls. This graph can only be used by one of the
163 // individual function graphs, and it goes away when they all go away.
165 class GlobalDSGraph : public DSGraph {
166 hash_set<const DSGraph*> Referrers;
167 void addReference(const DSGraph* referrer);
168 void removeReference(const DSGraph* referrer);
169 friend class DSGraph; // give access to Referrers
171 GlobalDSGraph(const GlobalDSGraph &GlobalDSG); // Do not implement
173 // Helper function for cloneGlobals and cloneCalls
174 DSNode* cloneNodeInto(DSNode *OldNode,
175 std::map<const DSNode*, DSNode*> &NodeCache,
176 bool GlobalsAreFinal = false);
179 GlobalDSGraph(); // Create an empty DSGraph
180 virtual ~GlobalDSGraph();
182 void cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
183 void cloneCalls (DSGraph& Graph);