blob: 6912af437b700e53dbdec35d50e6619716c82308 [file] [log] [blame]
Vikram S. Adveaaeee752002-07-30 22:06:40 +00001//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Vikram S. Adveaaeee752002-07-30 22:06:40 +00009//
10// This file implements the TDDataStructures class, which represents the
11// Top-down Interprocedural closure of the data structure graph over the
12// program. This is useful (but not strictly necessary?) for applications
13// like pointer analysis.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/DataStructure.h"
18#include "llvm/Module.h"
19#include "llvm/DerivedTypes.h"
Chris Lattner492dda92003-11-08 21:17:37 +000020#include "llvm/Analysis/DSGraph.h"
Chris Lattner6806f562003-08-01 22:15:03 +000021#include "Support/Debug.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000022#include "Support/Statistic.h"
Chris Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner4923d1b2003-02-03 22:51:28 +000025namespace {
26 RegisterAnalysis<TDDataStructures> // Register the pass
Chris Lattner312edd32003-06-28 22:14:55 +000027 Y("tddatastructure", "Top-down Data Structure Analysis");
Chris Lattnera8da51b2003-07-02 04:39:44 +000028
29 Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
30}
31
Chris Lattner3b0a9be2003-09-20 22:24:04 +000032void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
33 hash_set<DSNode*> &Visited) {
Chris Lattner11fc9302003-09-20 23:58:33 +000034 if (!N || Visited.count(N)) return;
Chris Lattner3b0a9be2003-09-20 22:24:04 +000035 Visited.insert(N);
36
37 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) {
38 DSNodeHandle &NH = N->getLink(i*N->getPointerSize());
39 if (DSNode *NN = NH.getNode()) {
40 const std::vector<GlobalValue*> &Globals = NN->getGlobals();
41 for (unsigned G = 0, e = Globals.size(); G != e; ++G)
42 if (Function *F = dyn_cast<Function>(Globals[G]))
43 ArgsRemainIncomplete.insert(F);
44
45 markReachableFunctionsExternallyAccessible(NN, Visited);
46 }
47 }
Chris Lattner4923d1b2003-02-03 22:51:28 +000048}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000049
Chris Lattner3b0a9be2003-09-20 22:24:04 +000050
Chris Lattneraa0b4682002-11-09 21:12:07 +000051// run - Calculate the top down data structure graphs for each function in the
52// program.
53//
54bool TDDataStructures::run(Module &M) {
55 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattner312edd32003-06-28 22:14:55 +000056 GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
Chris Lattner11fc9302003-09-20 23:58:33 +000057 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000058
Chris Lattnera8da51b2003-07-02 04:39:44 +000059 // Figure out which functions must not mark their arguments complete because
Chris Lattner3b0a9be2003-09-20 22:24:04 +000060 // they are accessible outside this compilation unit. Currently, these
61 // arguments are functions which are reachable by global variables in the
62 // globals graph.
63 const DSGraph::ScalarMapTy &GGSM = GlobalsGraph->getScalarMap();
64 hash_set<DSNode*> Visited;
65 for (DSGraph::ScalarMapTy::const_iterator I = GGSM.begin(), E = GGSM.end();
66 I != E; ++I)
67 if (isa<GlobalValue>(I->first))
68 markReachableFunctionsExternallyAccessible(I->second.getNode(), Visited);
Chris Lattner11fc9302003-09-20 23:58:33 +000069
70 // Loop over unresolved call nodes. Any functions passed into (but not
71 // returned!?) from unresolvable call nodes may be invoked outside of the
72 // current module.
73 const std::vector<DSCallSite> &Calls = GlobalsGraph->getAuxFunctionCalls();
74 for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
75 const DSCallSite &CS = Calls[i];
76 for (unsigned arg = 0, e = CS.getNumPtrArgs(); arg != e; ++arg)
77 markReachableFunctionsExternallyAccessible(CS.getPtrArg(arg).getNode(),
78 Visited);
79 }
Chris Lattner3b0a9be2003-09-20 22:24:04 +000080 Visited.clear();
81
82 // Functions without internal linkage also have unknown incoming arguments!
Chris Lattnera8da51b2003-07-02 04:39:44 +000083 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner3b0a9be2003-09-20 22:24:04 +000084 if (!I->isExternal() && !I->hasInternalLinkage())
Chris Lattnera8da51b2003-07-02 04:39:44 +000085 ArgsRemainIncomplete.insert(I);
86
Chris Lattner6c874612003-07-02 23:42:48 +000087 // We want to traverse the call graph in reverse post-order. To do this, we
88 // calculate a post-order traversal, then reverse it.
89 hash_set<DSGraph*> VisitedGraph;
90 std::vector<DSGraph*> PostOrder;
91 const BUDataStructures::ActualCalleesTy &ActualCallees =
92 getAnalysis<BUDataStructures>().getActualCallees();
93
Chris Lattneraa0b4682002-11-09 21:12:07 +000094 // Calculate top-down from main...
95 if (Function *F = M.getMainFunction())
Chris Lattner61691c52003-07-02 23:44:15 +000096 ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
Chris Lattneraa0b4682002-11-09 21:12:07 +000097
Vikram S. Adve03e19dd2003-07-16 21:40:28 +000098 // Next calculate the graphs for each unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +000099 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner6c874612003-07-02 23:42:48 +0000100 ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
101
102 VisitedGraph.clear(); // Release memory!
103
104 // Visit each of the graphs in reverse post-order now!
105 while (!PostOrder.empty()) {
106 inlineGraphIntoCallees(*PostOrder.back());
107 PostOrder.pop_back();
108 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000109
Chris Lattnera8da51b2003-07-02 04:39:44 +0000110 ArgsRemainIncomplete.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000111 return false;
112}
113
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000114
Chris Lattner7a211632002-11-08 21:28:37 +0000115DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
116 DSGraph *&G = DSInfo[&F];
117 if (G == 0) { // Not created yet? Clone BU graph...
118 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
119 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +0000120 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000121 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +0000122 }
123 return *G;
124}
Vikram S. Adve26b98262002-10-20 21:41:02 +0000125
Chris Lattnerdea81462003-06-29 22:37:07 +0000126
Chris Lattner18f07a12003-07-01 16:28:11 +0000127void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
128 std::vector<DSGraph*> &PostOrder,
129 const BUDataStructures::ActualCalleesTy &ActualCallees) {
130 if (F.isExternal()) return;
131 DSGraph &G = getOrCreateDSGraph(F);
132 if (Visited.count(&G)) return;
133 Visited.insert(&G);
134
135 // Recursively traverse all of the callee graphs.
136 const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls();
Chris Lattnerdea81462003-06-29 22:37:07 +0000137
Chris Lattner18f07a12003-07-01 16:28:11 +0000138 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000139 Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
Chris Lattner18f07a12003-07-01 16:28:11 +0000140 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
141 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000142 IP = ActualCallees.equal_range(CallI);
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000143
Chris Lattner18f07a12003-07-01 16:28:11 +0000144 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
145 I != IP.second; ++I)
146 ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
147 }
Chris Lattner198be222002-10-21 19:47:18 +0000148
Chris Lattner18f07a12003-07-01 16:28:11 +0000149 PostOrder.push_back(&G);
150}
151
152
153
Chris Lattner18f07a12003-07-01 16:28:11 +0000154
Chris Lattner6c874612003-07-02 23:42:48 +0000155
156// releaseMemory - If the pass pipeline is done with this pass, we can release
157// our memory... here...
158//
159// FIXME: This should be releaseMemory and will work fine, except that LoadVN
160// has no way to extend the lifetime of the pass, which screws up ds-aa.
161//
162void TDDataStructures::releaseMyMemory() {
163 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
164 E = DSInfo.end(); I != E; ++I) {
165 I->second->getReturnNodes().erase(I->first);
166 if (I->second->getReturnNodes().empty())
167 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000168 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000169
Chris Lattner6c874612003-07-02 23:42:48 +0000170 // Empty map so next time memory is released, data structures are not
171 // re-deleted.
172 DSInfo.clear();
173 delete GlobalsGraph;
174 GlobalsGraph = 0;
175}
Chris Lattner18f07a12003-07-01 16:28:11 +0000176
177void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000178 // Recompute the Incomplete markers and eliminate unreachable nodes.
Chris Lattner47030f82003-07-02 19:49:11 +0000179 Graph.removeTriviallyDeadNodes();
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000180 Graph.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000181
182 // If any of the functions has incomplete incoming arguments, don't mark any
183 // of them as complete.
184 bool HasIncompleteArgs = false;
185 const DSGraph::ReturnNodesTy &GraphReturnNodes = Graph.getReturnNodes();
186 for (DSGraph::ReturnNodesTy::const_iterator I = GraphReturnNodes.begin(),
187 E = GraphReturnNodes.end(); I != E; ++I)
188 if (ArgsRemainIncomplete.count(I->first)) {
189 HasIncompleteArgs = true;
190 break;
191 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000192
193 // Now fold in the necessary globals from the GlobalsGraph. A global G
194 // must be folded in if it exists in the current graph (i.e., is not dead)
195 // and it was not inlined from any of my callers. If it was inlined from
196 // a caller, it would have been fully consistent with the GlobalsGraph
197 // in the caller so folding in is not necessary. Otherwise, this node came
198 // solely from this function's BU graph and so has to be made consistent.
199 //
200 Graph.updateFromGlobalGraph();
201
202 // Recompute the Incomplete markers. Depends on whether args are complete
Chris Lattnera8da51b2003-07-02 04:39:44 +0000203 unsigned Flags
204 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000205 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000206
207 // Delete dead nodes. Treat globals that are unreachable as dead also.
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000208 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
209
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000210 // We are done with computing the current TD Graph! Now move on to
211 // inlining the current graph into the graphs for its callees, if any.
212 //
Chris Lattnera8da51b2003-07-02 04:39:44 +0000213 const std::vector<DSCallSite> &FunctionCalls = Graph.getFunctionCalls();
214 if (FunctionCalls.empty()) {
Chris Lattner18f07a12003-07-01 16:28:11 +0000215 DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
216 << "\n");
217 return;
218 }
219
Chris Lattner18f07a12003-07-01 16:28:11 +0000220 // Now that we have information about all of the callees, propagate the
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000221 // current graph into the callees. Clone only the reachable subgraph at
222 // each call-site, not the entire graph (even though the entire graph
223 // would be cloned only once, this should still be better on average).
Chris Lattner18f07a12003-07-01 16:28:11 +0000224 //
225 DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
Chris Lattnera8da51b2003-07-02 04:39:44 +0000226 << FunctionCalls.size() << " call nodes.\n");
Chris Lattner18f07a12003-07-01 16:28:11 +0000227
Chris Lattnera8da51b2003-07-02 04:39:44 +0000228 const BUDataStructures::ActualCalleesTy &ActualCallees =
229 getAnalysis<BUDataStructures>().getActualCallees();
Chris Lattner18f07a12003-07-01 16:28:11 +0000230
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000231 // Loop over all the call sites and all the callees at each call site.
232 // Clone and merge the reachable subgraph from the call into callee's graph.
233 //
Chris Lattnera8da51b2003-07-02 04:39:44 +0000234 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000235 Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000236 // For each function in the invoked function list at this call site...
Chris Lattnera8da51b2003-07-02 04:39:44 +0000237 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
238 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000239 IP = ActualCallees.equal_range(CallI);
Chris Lattnera8da51b2003-07-02 04:39:44 +0000240
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000241 // Multiple callees may have the same graph, so try to inline and merge
242 // only once for each <callSite,calleeGraph> pair, not once for each
243 // <callSite,calleeFunction> pair; the latter will be correct but slower.
244 hash_set<DSGraph*> GraphsSeen;
245
246 // Loop over each actual callee at this call site
247 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
248 I != IP.second; ++I) {
249 DSGraph& CalleeGraph = getDSGraph(*I->second);
250 assert(&CalleeGraph != &Graph && "TD need not inline graph into self!");
251
252 // if this callee graph is already done at this site, skip this callee
253 if (GraphsSeen.find(&CalleeGraph) != GraphsSeen.end())
254 continue;
255 GraphsSeen.insert(&CalleeGraph);
256
257 // Get the root nodes for cloning the reachable subgraph into each callee:
258 // -- all global nodes that appear in both the caller and the callee
259 // -- return value at this call site, if any
260 // -- actual arguments passed at this call site
261 // -- callee node at this call site, if this is an indirect call (this may
262 // not be needed for merging, but allows us to create CS and therefore
263 // simplify the merging below).
264 hash_set<const DSNode*> RootNodeSet;
265 for (DSGraph::ScalarMapTy::const_iterator
266 SI = CalleeGraph.getScalarMap().begin(),
267 SE = CalleeGraph.getScalarMap().end(); SI != SE; ++SI)
268 if (GlobalValue* GV = dyn_cast<GlobalValue>(SI->first)) {
269 DSGraph::ScalarMapTy::const_iterator GI=Graph.getScalarMap().find(GV);
270 if (GI != Graph.getScalarMap().end())
271 RootNodeSet.insert(GI->second.getNode());
Chris Lattnera8da51b2003-07-02 04:39:44 +0000272 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000273
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000274 if (const DSNode* RetNode = FunctionCalls[i].getRetVal().getNode())
275 RootNodeSet.insert(RetNode);
Chris Lattner0e744122002-10-17 04:26:54 +0000276
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000277 for (unsigned j=0, N=FunctionCalls[i].getNumPtrArgs(); j < N; ++j)
278 if (const DSNode* ArgTarget = FunctionCalls[i].getPtrArg(j).getNode())
279 RootNodeSet.insert(ArgTarget);
280
281 if (FunctionCalls[i].isIndirectCall())
282 RootNodeSet.insert(FunctionCalls[i].getCalleeNode());
283
Chris Lattnera8da51b2003-07-02 04:39:44 +0000284 DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000285 << CalleeGraph.getFunctionNames()
286 << "': " << I->second->getFunctionType()->getNumParams()
287 << " args\n at call site (DSCallSite*) 0x"
288 << &FunctionCalls[i] << "\n");
289
290 DSGraph::NodeMapTy NodeMapInCallee; // map from nodes to clones in callee
291 DSGraph::NodeMapTy CompletedMap; // unused map for nodes not to do
292 CalleeGraph.cloneReachableSubgraph(Graph, RootNodeSet,
293 NodeMapInCallee, CompletedMap,
294 DSGraph::StripModRefBits |
295 DSGraph::KeepAllocaBit);
Chris Lattner7a211632002-11-08 21:28:37 +0000296
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000297 // Transform our call site info into the cloned version for CalleeGraph
298 DSCallSite CS(FunctionCalls[i], NodeMapInCallee);
Chris Lattnera8da51b2003-07-02 04:39:44 +0000299
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000300 // Get the formal argument and return nodes for the called function
301 // and merge them with the cloned subgraph. Global nodes were merged
302 // already by cloneReachableSubgraph() above.
303 CalleeGraph.getCallSiteForArguments(*I->second).mergeWith(CS);
304
305 ++NumTDInlines;
Chris Lattnera8da51b2003-07-02 04:39:44 +0000306 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000307 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000308
309 DEBUG(std::cerr << " [TD] Done inlining into callees for: "
310 << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
311 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000312}