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"
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;
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 // print - Print out the analysis results...
83 void print(std::ostream &O, const Module *M) const;
85 // If the pass pipeline is done with this pass, we can release our memory...
86 virtual void releaseMemory();
88 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
90 AU.addRequired<LocalDataStructures>();
93 DSGraph &calculateGraph(Function &F);
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.
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;
105 ~TDDataStructures() { releaseMemory(); }
107 virtual bool run(Module &M);
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!");
116 // print - Print out the analysis results...
117 void print(std::ostream &O, const Module *M) const;
119 // If the pass pipeline is done with this pass, we can release our memory...
120 virtual void releaseMemory();
122 // getAnalysisUsage - This obviously provides a data structure graph.
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesAll();
125 AU.addRequired<BUDataStructures>();
128 void calculateGraph(Function &F);
129 DSGraph &getOrCreateDSGraph(Function &F);
131 void ResolveCallSite(DSGraph &Graph, const DSCallSite &CallSite);
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.
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
146 GlobalDSGraph(const GlobalDSGraph &GlobalDSG); // Do not implement
148 // Helper function for cloneGlobals and cloneCalls
149 DSNode* cloneNodeInto(DSNode *OldNode,
150 std::map<const DSNode*, DSNode*> &NodeCache,
151 bool GlobalsAreFinal = false);
154 GlobalDSGraph(); // Create an empty DSGraph
155 virtual ~GlobalDSGraph();
157 void cloneGlobals(DSGraph& Graph, bool CloneCalls = false);
158 void cloneCalls (DSGraph& Graph);