blob: 7da1746125beb605b9fbad813da5f405a06a6344 [file] [log] [blame]
Vikram S. Adveaaeee752002-07-30 22:06:40 +00001//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
2//
3// This file implements the TDDataStructures class, which represents the
4// Top-down Interprocedural closure of the data structure graph over the
5// program. This is useful (but not strictly necessary?) for applications
6// like pointer analysis.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
11#include "llvm/Module.h"
12#include "llvm/DerivedTypes.h"
Chris Lattner6806f562003-08-01 22:15:03 +000013#include "Support/Debug.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include "Support/Statistic.h"
Chris Lattner18f07a12003-07-01 16:28:11 +000015#include "DSCallSiteIterator.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000016
Chris Lattner4923d1b2003-02-03 22:51:28 +000017namespace {
18 RegisterAnalysis<TDDataStructures> // Register the pass
Chris Lattner312edd32003-06-28 22:14:55 +000019 Y("tddatastructure", "Top-down Data Structure Analysis");
Chris Lattnera8da51b2003-07-02 04:39:44 +000020
21 Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
22}
23
Chris Lattner3b0a9be2003-09-20 22:24:04 +000024void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
25 hash_set<DSNode*> &Visited) {
Chris Lattner11fc9302003-09-20 23:58:33 +000026 if (!N || Visited.count(N)) return;
Chris Lattner3b0a9be2003-09-20 22:24:04 +000027 Visited.insert(N);
28
29 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) {
30 DSNodeHandle &NH = N->getLink(i*N->getPointerSize());
31 if (DSNode *NN = NH.getNode()) {
32 const std::vector<GlobalValue*> &Globals = NN->getGlobals();
33 for (unsigned G = 0, e = Globals.size(); G != e; ++G)
34 if (Function *F = dyn_cast<Function>(Globals[G]))
35 ArgsRemainIncomplete.insert(F);
36
37 markReachableFunctionsExternallyAccessible(NN, Visited);
38 }
39 }
Chris Lattner4923d1b2003-02-03 22:51:28 +000040}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000041
Chris Lattner3b0a9be2003-09-20 22:24:04 +000042
Chris Lattneraa0b4682002-11-09 21:12:07 +000043// run - Calculate the top down data structure graphs for each function in the
44// program.
45//
46bool TDDataStructures::run(Module &M) {
47 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattner312edd32003-06-28 22:14:55 +000048 GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
Chris Lattner11fc9302003-09-20 23:58:33 +000049 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000050
Chris Lattnera8da51b2003-07-02 04:39:44 +000051 // Figure out which functions must not mark their arguments complete because
Chris Lattner3b0a9be2003-09-20 22:24:04 +000052 // they are accessible outside this compilation unit. Currently, these
53 // arguments are functions which are reachable by global variables in the
54 // globals graph.
55 const DSGraph::ScalarMapTy &GGSM = GlobalsGraph->getScalarMap();
56 hash_set<DSNode*> Visited;
57 for (DSGraph::ScalarMapTy::const_iterator I = GGSM.begin(), E = GGSM.end();
58 I != E; ++I)
59 if (isa<GlobalValue>(I->first))
60 markReachableFunctionsExternallyAccessible(I->second.getNode(), Visited);
Chris Lattner11fc9302003-09-20 23:58:33 +000061
62 // Loop over unresolved call nodes. Any functions passed into (but not
63 // returned!?) from unresolvable call nodes may be invoked outside of the
64 // current module.
65 const std::vector<DSCallSite> &Calls = GlobalsGraph->getAuxFunctionCalls();
66 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
67 const DSCallSite &CS = Calls[i];
68 for (unsigned arg = 0, e = CS.getNumPtrArgs(); arg != e; ++arg)
69 markReachableFunctionsExternallyAccessible(CS.getPtrArg(arg).getNode(),
70 Visited);
71 }
Chris Lattner3b0a9be2003-09-20 22:24:04 +000072 Visited.clear();
73
74 // Functions without internal linkage also have unknown incoming arguments!
Chris Lattnera8da51b2003-07-02 04:39:44 +000075 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner3b0a9be2003-09-20 22:24:04 +000076 if (!I->isExternal() && !I->hasInternalLinkage())
Chris Lattnera8da51b2003-07-02 04:39:44 +000077 ArgsRemainIncomplete.insert(I);
78
Chris Lattner6c874612003-07-02 23:42:48 +000079 // We want to traverse the call graph in reverse post-order. To do this, we
80 // calculate a post-order traversal, then reverse it.
81 hash_set<DSGraph*> VisitedGraph;
82 std::vector<DSGraph*> PostOrder;
83 const BUDataStructures::ActualCalleesTy &ActualCallees =
84 getAnalysis<BUDataStructures>().getActualCallees();
85
Chris Lattneraa0b4682002-11-09 21:12:07 +000086 // Calculate top-down from main...
87 if (Function *F = M.getMainFunction())
Chris Lattner61691c52003-07-02 23:44:15 +000088 ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
Chris Lattneraa0b4682002-11-09 21:12:07 +000089
Vikram S. Adve03e19dd2003-07-16 21:40:28 +000090 // Next calculate the graphs for each unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +000091 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner6c874612003-07-02 23:42:48 +000092 ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
93
94 VisitedGraph.clear(); // Release memory!
95
96 // Visit each of the graphs in reverse post-order now!
97 while (!PostOrder.empty()) {
98 inlineGraphIntoCallees(*PostOrder.back());
99 PostOrder.pop_back();
100 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000101
Chris Lattnera8da51b2003-07-02 04:39:44 +0000102 ArgsRemainIncomplete.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000103 return false;
104}
105
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000106
Chris Lattner7a211632002-11-08 21:28:37 +0000107DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
108 DSGraph *&G = DSInfo[&F];
109 if (G == 0) { // Not created yet? Clone BU graph...
110 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
111 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +0000112 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000113 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +0000114 }
115 return *G;
116}
Vikram S. Adve26b98262002-10-20 21:41:02 +0000117
Chris Lattnerdea81462003-06-29 22:37:07 +0000118
Chris Lattner18f07a12003-07-01 16:28:11 +0000119void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
120 std::vector<DSGraph*> &PostOrder,
121 const BUDataStructures::ActualCalleesTy &ActualCallees) {
122 if (F.isExternal()) return;
123 DSGraph &G = getOrCreateDSGraph(F);
124 if (Visited.count(&G)) return;
125 Visited.insert(&G);
126
127 // Recursively traverse all of the callee graphs.
128 const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls();
Chris Lattnerdea81462003-06-29 22:37:07 +0000129
Chris Lattner18f07a12003-07-01 16:28:11 +0000130 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000131 Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
Chris Lattner18f07a12003-07-01 16:28:11 +0000132 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
133 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000134 IP = ActualCallees.equal_range(CallI);
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000135
Chris Lattner18f07a12003-07-01 16:28:11 +0000136 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
137 I != IP.second; ++I)
138 ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
139 }
Chris Lattner198be222002-10-21 19:47:18 +0000140
Chris Lattner18f07a12003-07-01 16:28:11 +0000141 PostOrder.push_back(&G);
142}
143
144
145
Chris Lattner18f07a12003-07-01 16:28:11 +0000146
Chris Lattner6c874612003-07-02 23:42:48 +0000147
148// releaseMemory - If the pass pipeline is done with this pass, we can release
149// our memory... here...
150//
151// FIXME: This should be releaseMemory and will work fine, except that LoadVN
152// has no way to extend the lifetime of the pass, which screws up ds-aa.
153//
154void TDDataStructures::releaseMyMemory() {
155 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
156 E = DSInfo.end(); I != E; ++I) {
157 I->second->getReturnNodes().erase(I->first);
158 if (I->second->getReturnNodes().empty())
159 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000160 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000161
Chris Lattner6c874612003-07-02 23:42:48 +0000162 // Empty map so next time memory is released, data structures are not
163 // re-deleted.
164 DSInfo.clear();
165 delete GlobalsGraph;
166 GlobalsGraph = 0;
167}
Chris Lattner18f07a12003-07-01 16:28:11 +0000168
169void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000170 // Recompute the Incomplete markers and eliminate unreachable nodes.
Chris Lattner47030f82003-07-02 19:49:11 +0000171 Graph.removeTriviallyDeadNodes();
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000172 Graph.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000173
174 // If any of the functions has incomplete incoming arguments, don't mark any
175 // of them as complete.
176 bool HasIncompleteArgs = false;
177 const DSGraph::ReturnNodesTy &GraphReturnNodes = Graph.getReturnNodes();
178 for (DSGraph::ReturnNodesTy::const_iterator I = GraphReturnNodes.begin(),
179 E = GraphReturnNodes.end(); I != E; ++I)
180 if (ArgsRemainIncomplete.count(I->first)) {
181 HasIncompleteArgs = true;
182 break;
183 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000184
185 // Now fold in the necessary globals from the GlobalsGraph. A global G
186 // must be folded in if it exists in the current graph (i.e., is not dead)
187 // and it was not inlined from any of my callers. If it was inlined from
188 // a caller, it would have been fully consistent with the GlobalsGraph
189 // in the caller so folding in is not necessary. Otherwise, this node came
190 // solely from this function's BU graph and so has to be made consistent.
191 //
192 Graph.updateFromGlobalGraph();
193
194 // Recompute the Incomplete markers. Depends on whether args are complete
Chris Lattnera8da51b2003-07-02 04:39:44 +0000195 unsigned Flags
196 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000197 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000198
199 // Delete dead nodes. Treat globals that are unreachable as dead also.
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000200 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
201
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000202 // We are done with computing the current TD Graph! Now move on to
203 // inlining the current graph into the graphs for its callees, if any.
204 //
Chris Lattnera8da51b2003-07-02 04:39:44 +0000205 const std::vector<DSCallSite> &FunctionCalls = Graph.getFunctionCalls();
206 if (FunctionCalls.empty()) {
Chris Lattner18f07a12003-07-01 16:28:11 +0000207 DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
208 << "\n");
209 return;
210 }
211
Chris Lattner18f07a12003-07-01 16:28:11 +0000212 // Now that we have information about all of the callees, propagate the
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000213 // current graph into the callees. Clone only the reachable subgraph at
214 // each call-site, not the entire graph (even though the entire graph
215 // would be cloned only once, this should still be better on average).
Chris Lattner18f07a12003-07-01 16:28:11 +0000216 //
217 DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
Chris Lattnera8da51b2003-07-02 04:39:44 +0000218 << FunctionCalls.size() << " call nodes.\n");
Chris Lattner18f07a12003-07-01 16:28:11 +0000219
Chris Lattnera8da51b2003-07-02 04:39:44 +0000220 const BUDataStructures::ActualCalleesTy &ActualCallees =
221 getAnalysis<BUDataStructures>().getActualCallees();
Chris Lattner18f07a12003-07-01 16:28:11 +0000222
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000223 // Loop over all the call sites and all the callees at each call site.
224 // Clone and merge the reachable subgraph from the call into callee's graph.
225 //
Chris Lattnera8da51b2003-07-02 04:39:44 +0000226 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000227 Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000228 // For each function in the invoked function list at this call site...
Chris Lattnera8da51b2003-07-02 04:39:44 +0000229 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
230 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000231 IP = ActualCallees.equal_range(CallI);
Chris Lattnera8da51b2003-07-02 04:39:44 +0000232
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000233 // Multiple callees may have the same graph, so try to inline and merge
234 // only once for each <callSite,calleeGraph> pair, not once for each
235 // <callSite,calleeFunction> pair; the latter will be correct but slower.
236 hash_set<DSGraph*> GraphsSeen;
237
238 // Loop over each actual callee at this call site
239 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
240 I != IP.second; ++I) {
241 DSGraph& CalleeGraph = getDSGraph(*I->second);
242 assert(&CalleeGraph != &Graph && "TD need not inline graph into self!");
243
244 // if this callee graph is already done at this site, skip this callee
245 if (GraphsSeen.find(&CalleeGraph) != GraphsSeen.end())
246 continue;
247 GraphsSeen.insert(&CalleeGraph);
248
249 // Get the root nodes for cloning the reachable subgraph into each callee:
250 // -- all global nodes that appear in both the caller and the callee
251 // -- return value at this call site, if any
252 // -- actual arguments passed at this call site
253 // -- callee node at this call site, if this is an indirect call (this may
254 // not be needed for merging, but allows us to create CS and therefore
255 // simplify the merging below).
256 hash_set<const DSNode*> RootNodeSet;
257 for (DSGraph::ScalarMapTy::const_iterator
258 SI = CalleeGraph.getScalarMap().begin(),
259 SE = CalleeGraph.getScalarMap().end(); SI != SE; ++SI)
260 if (GlobalValue* GV = dyn_cast<GlobalValue>(SI->first)) {
261 DSGraph::ScalarMapTy::const_iterator GI=Graph.getScalarMap().find(GV);
262 if (GI != Graph.getScalarMap().end())
263 RootNodeSet.insert(GI->second.getNode());
Chris Lattnera8da51b2003-07-02 04:39:44 +0000264 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000265
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000266 if (const DSNode* RetNode = FunctionCalls[i].getRetVal().getNode())
267 RootNodeSet.insert(RetNode);
Chris Lattner0e744122002-10-17 04:26:54 +0000268
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000269 for (unsigned j=0, N=FunctionCalls[i].getNumPtrArgs(); j < N; ++j)
270 if (const DSNode* ArgTarget = FunctionCalls[i].getPtrArg(j).getNode())
271 RootNodeSet.insert(ArgTarget);
272
273 if (FunctionCalls[i].isIndirectCall())
274 RootNodeSet.insert(FunctionCalls[i].getCalleeNode());
275
Chris Lattnera8da51b2003-07-02 04:39:44 +0000276 DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000277 << CalleeGraph.getFunctionNames()
278 << "': " << I->second->getFunctionType()->getNumParams()
279 << " args\n at call site (DSCallSite*) 0x"
280 << &FunctionCalls[i] << "\n");
281
282 DSGraph::NodeMapTy NodeMapInCallee; // map from nodes to clones in callee
283 DSGraph::NodeMapTy CompletedMap; // unused map for nodes not to do
284 CalleeGraph.cloneReachableSubgraph(Graph, RootNodeSet,
285 NodeMapInCallee, CompletedMap,
286 DSGraph::StripModRefBits |
287 DSGraph::KeepAllocaBit);
Chris Lattner7a211632002-11-08 21:28:37 +0000288
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000289 // Transform our call site info into the cloned version for CalleeGraph
290 DSCallSite CS(FunctionCalls[i], NodeMapInCallee);
Chris Lattnera8da51b2003-07-02 04:39:44 +0000291
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000292 // Get the formal argument and return nodes for the called function
293 // and merge them with the cloned subgraph. Global nodes were merged
294 // already by cloneReachableSubgraph() above.
295 CalleeGraph.getCallSiteForArguments(*I->second).mergeWith(CS);
296
297 ++NumTDInlines;
Chris Lattnera8da51b2003-07-02 04:39:44 +0000298 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000299 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000300
301 DEBUG(std::cerr << " [TD] Done inlining into callees for: "
302 << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
303 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000304}
Chris Lattner4923d1b2003-02-03 22:51:28 +0000305