An interprocedural analysis pass that computes flow-insensitive
[oota-llvm.git] / lib / Analysis / DataStructure / IPModRef.cpp
1 //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===//
2 //
3 // See high-level comments in include/llvm/Analysis/IPModRef.h
4 // 
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Analysis/DataStructure.h"
8 #include "llvm/Analysis/DSGraph.h"
9 #include "llvm/Analysis/IPModRef.h"
10 #include "llvm/Module.h"
11 #include "llvm/Function.h"
12 #include "llvm/iOther.h"
13 #include "llvm/Pass.h"
14 #include "Support/Statistic.h"
15 #include "Support/STLExtras.h"
16 #include "Support/StringExtras.h"
17
18 #include <algorithm>
19 #include <utility>
20 #include <vector>
21
22
23 //----------------------------------------------------------------------------
24 // Private constants and data
25 //----------------------------------------------------------------------------
26
27 static RegisterAnalysis<IPModRef>
28 Z("ipmodref", "Interprocedural mod/ref analysis");
29
30
31 //----------------------------------------------------------------------------
32 // class ModRefInfo
33 //----------------------------------------------------------------------------
34
35 void ModRefInfo::print(std::ostream &O) const
36 {
37   O << std::endl << "Modified   nodes = " << modNodeSet;
38   O              << "Referenced nodes = " << refNodeSet << std::endl;
39 }
40
41 void ModRefInfo::dump() const
42 {
43   print(std::cerr);
44 }
45
46 //----------------------------------------------------------------------------
47 // class FunctionModRefInfo
48 //----------------------------------------------------------------------------
49
50
51 // This constructor computes a node numbering for the TD graph.
52 // 
53 FunctionModRefInfo::FunctionModRefInfo(const Function& func,
54                                        const DSGraph& tdg,
55                                        const DSGraph& ldg)
56   : F(func),
57     funcTDGraph(tdg),
58     funcLocalGraph(ldg),
59     funcModRefInfo(tdg.getGraphSize())
60 {
61   for (unsigned i=0, N = funcTDGraph.getGraphSize(); i < N; ++i)
62     NodeIds[funcTDGraph.getNodes()[i]] = i;
63 }
64
65
66 FunctionModRefInfo::~FunctionModRefInfo()
67 {
68   for(std::map<const CallInst*, ModRefInfo*>::iterator
69         I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I)
70     delete(I->second);
71
72   // Empty map just to make problems easier to track down
73   callSiteModRefInfo.clear();
74 }
75
76
77 // Dummy function that will be replaced with one that inlines
78 // the callee's BU graph into the caller's TD graph.
79 // 
80 const DSGraph* ResolveGraphForCallSite(const DSGraph& funcTDGraph,
81                                        const CallInst& callInst)
82 {
83   return &funcTDGraph;                    // TEMPORARY
84 }
85
86
87 // Compute Mod/Ref bit vectors for the entire function.
88 // These are simply copies of the Read/Write flags from the nodes of
89 // the top-down DS graph.
90 // 
91 void FunctionModRefInfo::computeModRef(const Function &func)
92 {
93   // Mark all nodes in the graph that are marked MOD as being mod
94   // and all those marked REF as being ref.
95   for (unsigned i = 0, N = funcTDGraph.getGraphSize(); i < N; ++i)
96     {
97       if (funcTDGraph.getNodes()[i]->isModified())
98         funcModRefInfo.setNodeIsMod(i);
99       if (funcTDGraph.getNodes()[i]->isRead())
100         funcModRefInfo.setNodeIsRef(i);
101     }
102
103   // Compute the Mod/Ref info for all call sites within the function
104   // Use the Local DSgraph, which includes all the call sites in the
105   // original program.
106   const std::vector<DSCallSite>& callSites = funcLocalGraph.getFunctionCalls();
107   for (unsigned i = 0, N = callSites.size(); i < N; ++i)
108     computeModRef(callSites[i].getCallInst());
109 }
110
111
112 // Compute Mod/Ref bit vectors for a single call site.
113 // These are copies of the Read/Write flags from the nodes of
114 // the graph produced by clearing all flags in teh caller's TD graph
115 // and then inlining the callee's BU graph into the caller's TD graph.
116 // 
117 void
118 FunctionModRefInfo::computeModRef(const CallInst& callInst)
119 {
120   // Allocate the mod/ref info for the call site.  Bits automatically cleared.
121   ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
122   callSiteModRefInfo[&callInst] = callModRefInfo;
123
124   // Get a copy of the graph for the callee with the callee inlined
125   const DSGraph* csgp = ResolveGraphForCallSite(funcTDGraph, callInst);
126   assert(csgp && "Unable to compute callee mod/ref information");
127
128   // For all nodes in the graph, extract the mod/ref information
129   const std::vector<DSNode*>& csgNodes = csgp->getNodes();
130   const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes();
131   assert(csgNodes.size() == origNodes.size());
132   for (unsigned i=0, N = csgNodes.size(); i < N; ++i)
133     { 
134       if (csgNodes[i]->isModified())
135         callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
136       if (csgNodes[i]->isRead())
137         callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
138     }
139 }
140
141
142 // Print the results of the pass.
143 // Currently this just prints bit-vectors and is not very readable.
144 // 
145 void FunctionModRefInfo::print(std::ostream &O) const
146 {
147   O << "---------- Mod/ref information for function "
148     << F.getName() << "---------- \n\n";
149
150   O << "Mod/ref info for function body:\n";
151   funcModRefInfo.print(O);
152
153   for (std::map<const CallInst*, ModRefInfo*>::const_iterator
154          CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
155        CI != CE; ++CI)
156     {
157       O << "Mod/ref info for call site " << CI->first << ":\n";
158       CI->second->print(O);
159     }
160
161   O << "\n";
162 }
163
164 void FunctionModRefInfo::dump() const
165 {
166   print(std::cerr);
167 }
168
169
170 //----------------------------------------------------------------------------
171 // class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
172 //----------------------------------------------------------------------------
173
174
175 // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
176 // 
177 void IPModRef::releaseMemory()
178 {
179   for(std::map<const Function*, FunctionModRefInfo*>::iterator
180         I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
181     delete(I->second);
182
183   // Clear map so memory is not re-released if we are called again
184   funcToModRefInfoMap.clear();
185 }
186
187
188 // Run the "interprocedural" pass on each function.  This needs to do
189 // NO real interprocedural work because all that has been done the
190 // data structure analysis.
191 // 
192 bool IPModRef::run(Module &theModule)
193 {
194   M = &theModule;
195   for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
196     if (! FI->isExternal())
197       getFuncInfo(*FI, /*computeIfMissing*/ true);
198   return true;
199 }
200
201
202 void IPModRef::print(std::ostream &O) const
203 {
204   O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
205   
206   for (std::map<const Function*, FunctionModRefInfo*>::const_iterator
207          mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end();
208        mapI != mapE; ++mapI)
209     mapI->second->print(O);
210
211   O << "\n";
212 }
213
214
215 void IPModRef::dump() const
216 {
217   print(std::cerr);
218 }