blob: 8190532176fabdb4a83d494d25b458b21b7d5e04 [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
Chris Lattner4dabb2c2004-07-07 06:32:21 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000018#include "llvm/Module.h"
19#include "llvm/DerivedTypes.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000020#include "llvm/Analysis/DataStructure/DSGraph.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/Debug.h"
22#include "llvm/ADT/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()) {
Chris Lattner9454dda2005-03-20 02:39:49 +000040 std::vector<Function*> Functions;
41 NN->addFullFunctionList(Functions);
42 ArgsRemainIncomplete.insert(Functions.begin(), Functions.end());
Chris Lattner3b0a9be2003-09-20 22:24:04 +000043 markReachableFunctionsExternallyAccessible(NN, Visited);
44 }
45 }
Chris Lattner4923d1b2003-02-03 22:51:28 +000046}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000047
Chris Lattner3b0a9be2003-09-20 22:24:04 +000048
Chris Lattneraa0b4682002-11-09 21:12:07 +000049// run - Calculate the top down data structure graphs for each function in the
50// program.
51//
Chris Lattnerb12914b2004-09-20 04:48:05 +000052bool TDDataStructures::runOnModule(Module &M) {
Chris Lattneraa0b4682002-11-09 21:12:07 +000053 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000054 GlobalECs = BU.getGlobalECs();
55 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
Chris Lattner11fc9302003-09-20 23:58:33 +000056 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000057
Chris Lattnera8da51b2003-07-02 04:39:44 +000058 // Figure out which functions must not mark their arguments complete because
Chris Lattner3b0a9be2003-09-20 22:24:04 +000059 // they are accessible outside this compilation unit. Currently, these
60 // arguments are functions which are reachable by global variables in the
61 // globals graph.
Chris Lattner6d8f3dc2004-01-28 03:12:48 +000062 const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
Chris Lattner3b0a9be2003-09-20 22:24:04 +000063 hash_set<DSNode*> Visited;
Misha Brukman96a8bd72004-04-29 04:05:30 +000064 for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end();
Chris Lattner3b0a9be2003-09-20 22:24:04 +000065 I != E; ++I)
Misha Brukman96a8bd72004-04-29 04:05:30 +000066 markReachableFunctionsExternallyAccessible(GGSM.find(*I)->second.getNode(),
67 Visited);
Chris Lattner11fc9302003-09-20 23:58:33 +000068
69 // Loop over unresolved call nodes. Any functions passed into (but not
Chris Lattnerf325e392004-01-27 21:53:14 +000070 // returned!) from unresolvable call nodes may be invoked outside of the
Chris Lattner11fc9302003-09-20 23:58:33 +000071 // current module.
Chris Lattnera9548d92005-01-30 23:51:02 +000072 for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(),
73 E = GlobalsGraph->afc_end(); I != E; ++I)
74 for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg)
75 markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(),
Chris Lattner11fc9302003-09-20 23:58:33 +000076 Visited);
Chris Lattner3b0a9be2003-09-20 22:24:04 +000077 Visited.clear();
78
79 // Functions without internal linkage also have unknown incoming arguments!
Chris Lattnera8da51b2003-07-02 04:39:44 +000080 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner3b0a9be2003-09-20 22:24:04 +000081 if (!I->isExternal() && !I->hasInternalLinkage())
Chris Lattnera8da51b2003-07-02 04:39:44 +000082 ArgsRemainIncomplete.insert(I);
83
Chris Lattner6c874612003-07-02 23:42:48 +000084 // We want to traverse the call graph in reverse post-order. To do this, we
85 // calculate a post-order traversal, then reverse it.
86 hash_set<DSGraph*> VisitedGraph;
87 std::vector<DSGraph*> PostOrder;
88 const BUDataStructures::ActualCalleesTy &ActualCallees =
89 getAnalysis<BUDataStructures>().getActualCallees();
90
Chris Lattneraa0b4682002-11-09 21:12:07 +000091 // Calculate top-down from main...
92 if (Function *F = M.getMainFunction())
Chris Lattner61691c52003-07-02 23:44:15 +000093 ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
Chris Lattneraa0b4682002-11-09 21:12:07 +000094
Vikram S. Adve03e19dd2003-07-16 21:40:28 +000095 // Next calculate the graphs for each unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +000096 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner6c874612003-07-02 23:42:48 +000097 ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
98
99 VisitedGraph.clear(); // Release memory!
100
101 // Visit each of the graphs in reverse post-order now!
102 while (!PostOrder.empty()) {
103 inlineGraphIntoCallees(*PostOrder.back());
104 PostOrder.pop_back();
105 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000106
Chris Lattnera8da51b2003-07-02 04:39:44 +0000107 ArgsRemainIncomplete.clear();
Chris Lattnerc3f5f772004-02-08 01:51:48 +0000108 GlobalsGraph->removeTriviallyDeadNodes();
109
Chris Lattneraa0b4682002-11-09 21:12:07 +0000110 return false;
111}
112
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000113
Chris Lattner7a211632002-11-08 21:28:37 +0000114DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
115 DSGraph *&G = DSInfo[&F];
116 if (G == 0) { // Not created yet? Clone BU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000117 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner7a211632002-11-08 21:28:37 +0000118 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +0000119 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000120 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +0000121 }
122 return *G;
123}
Vikram S. Adve26b98262002-10-20 21:41:02 +0000124
Chris Lattnerdea81462003-06-29 22:37:07 +0000125
Chris Lattner18f07a12003-07-01 16:28:11 +0000126void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
127 std::vector<DSGraph*> &PostOrder,
128 const BUDataStructures::ActualCalleesTy &ActualCallees) {
129 if (F.isExternal()) return;
130 DSGraph &G = getOrCreateDSGraph(F);
131 if (Visited.count(&G)) return;
132 Visited.insert(&G);
133
134 // Recursively traverse all of the callee graphs.
Chris Lattnera9548d92005-01-30 23:51:02 +0000135 for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E; ++CI) {
136 Instruction *CallI = CI->getCallSite().getInstruction();
Chris Lattner18f07a12003-07-01 16:28:11 +0000137 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
138 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000139 IP = ActualCallees.equal_range(CallI);
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000140
Chris Lattner18f07a12003-07-01 16:28:11 +0000141 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
142 I != IP.second; ++I)
143 ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
144 }
Chris Lattner198be222002-10-21 19:47:18 +0000145
Chris Lattner18f07a12003-07-01 16:28:11 +0000146 PostOrder.push_back(&G);
147}
148
149
150
Chris Lattner18f07a12003-07-01 16:28:11 +0000151
Chris Lattner6c874612003-07-02 23:42:48 +0000152
153// releaseMemory - If the pass pipeline is done with this pass, we can release
154// our memory... here...
155//
156// FIXME: This should be releaseMemory and will work fine, except that LoadVN
157// has no way to extend the lifetime of the pass, which screws up ds-aa.
158//
159void TDDataStructures::releaseMyMemory() {
160 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
161 E = DSInfo.end(); I != E; ++I) {
162 I->second->getReturnNodes().erase(I->first);
163 if (I->second->getReturnNodes().empty())
164 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000165 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000166
Chris Lattner6c874612003-07-02 23:42:48 +0000167 // Empty map so next time memory is released, data structures are not
168 // re-deleted.
169 DSInfo.clear();
170 delete GlobalsGraph;
171 GlobalsGraph = 0;
172}
Chris Lattner18f07a12003-07-01 16:28:11 +0000173
174void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000175 // Recompute the Incomplete markers and eliminate unreachable nodes.
176 Graph.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000177
178 // If any of the functions has incomplete incoming arguments, don't mark any
179 // of them as complete.
180 bool HasIncompleteArgs = false;
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000181 for (DSGraph::retnodes_iterator I = Graph.retnodes_begin(),
182 E = Graph.retnodes_end(); I != E; ++I)
Chris Lattnera8da51b2003-07-02 04:39:44 +0000183 if (ArgsRemainIncomplete.count(I->first)) {
184 HasIncompleteArgs = true;
185 break;
186 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000187
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000188 // Recompute the Incomplete markers. Depends on whether args are complete
Chris Lattnera8da51b2003-07-02 04:39:44 +0000189 unsigned Flags
190 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000191 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000192
193 // Delete dead nodes. Treat globals that are unreachable as dead also.
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000194 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
195
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000196 // We are done with computing the current TD Graph! Now move on to
197 // inlining the current graph into the graphs for its callees, if any.
198 //
Chris Lattnera9548d92005-01-30 23:51:02 +0000199 if (Graph.fc_begin() == Graph.fc_end()) {
Chris Lattner18f07a12003-07-01 16:28:11 +0000200 DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
201 << "\n");
202 return;
203 }
204
Chris Lattner18f07a12003-07-01 16:28:11 +0000205 // Now that we have information about all of the callees, propagate the
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000206 // current graph into the callees. Clone only the reachable subgraph at
207 // each call-site, not the entire graph (even though the entire graph
208 // would be cloned only once, this should still be better on average).
Chris Lattner18f07a12003-07-01 16:28:11 +0000209 //
210 DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
Chris Lattnera9548d92005-01-30 23:51:02 +0000211 << Graph.getFunctionCalls().size() << " call nodes.\n");
Chris Lattner18f07a12003-07-01 16:28:11 +0000212
Chris Lattnera8da51b2003-07-02 04:39:44 +0000213 const BUDataStructures::ActualCalleesTy &ActualCallees =
214 getAnalysis<BUDataStructures>().getActualCallees();
Chris Lattner18f07a12003-07-01 16:28:11 +0000215
Chris Lattnerf325e392004-01-27 21:53:14 +0000216 // Loop over all the call sites and all the callees at each call site. Build
217 // a mapping from called DSGraph's to the call sites in this function that
218 // invoke them. This is useful because we can be more efficient if there are
219 // multiple call sites to the callees in the graph from this caller.
220 std::multimap<DSGraph*, std::pair<Function*, const DSCallSite*> > CallSites;
221
Chris Lattnera9548d92005-01-30 23:51:02 +0000222 for (DSGraph::fc_iterator CI = Graph.fc_begin(), E = Graph.fc_end();
223 CI != E; ++CI) {
224 Instruction *CallI = CI->getCallSite().getInstruction();
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000225 // For each function in the invoked function list at this call site...
Chris Lattnera8da51b2003-07-02 04:39:44 +0000226 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
Chris Lattnera9548d92005-01-30 23:51:02 +0000227 BUDataStructures::ActualCalleesTy::const_iterator>
228 IP = ActualCallees.equal_range(CallI);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000229 // Loop over each actual callee at this call site
230 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
231 I != IP.second; ++I) {
232 DSGraph& CalleeGraph = getDSGraph(*I->second);
Chris Lattnerd7be1882005-02-04 18:58:04 +0000233 if (&CalleeGraph != &Graph)
234 CallSites.insert(std::make_pair(&CalleeGraph,
235 std::make_pair(I->second, &*CI)));
Chris Lattnerf325e392004-01-27 21:53:14 +0000236 }
237 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000238
Chris Lattnerf325e392004-01-27 21:53:14 +0000239 // Now that we built the mapping, actually perform the inlining a callee graph
240 // at a time.
241 std::multimap<DSGraph*,std::pair<Function*,const DSCallSite*> >::iterator CSI;
242 for (CSI = CallSites.begin(); CSI != CallSites.end(); ) {
243 DSGraph &CalleeGraph = *CSI->first;
244 // Iterate through all of the call sites of this graph, cloning and merging
245 // any nodes required by the call.
Chris Lattner53491b32005-03-20 18:02:56 +0000246 ReachabilityCloner RC(CalleeGraph, Graph, 0);
Chris Lattner18f07a12003-07-01 16:28:11 +0000247
Chris Lattnerf325e392004-01-27 21:53:14 +0000248 // Clone over any global nodes that appear in both graphs.
Chris Lattner6d8f3dc2004-01-28 03:12:48 +0000249 for (DSScalarMap::global_iterator
250 SI = CalleeGraph.getScalarMap().global_begin(),
251 SE = CalleeGraph.getScalarMap().global_end(); SI != SE; ++SI) {
252 DSScalarMap::const_iterator GI = Graph.getScalarMap().find(*SI);
253 if (GI != Graph.getScalarMap().end())
254 RC.merge(CalleeGraph.getNodeForValue(*SI), GI->second);
255 }
Chris Lattner0e744122002-10-17 04:26:54 +0000256
Chris Lattnerf325e392004-01-27 21:53:14 +0000257 // Loop over all of the distinct call sites in the caller of the callee.
258 for (; CSI != CallSites.end() && CSI->first == &CalleeGraph; ++CSI) {
259 Function &CF = *CSI->second.first;
260 const DSCallSite &CS = *CSI->second.second;
Chris Lattnera8da51b2003-07-02 04:39:44 +0000261 DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000262 << CalleeGraph.getFunctionNames()
Chris Lattnerf325e392004-01-27 21:53:14 +0000263 << "': " << CF.getFunctionType()->getNumParams()
264 << " args\n at call site (DSCallSite*) 0x" << &CS << "\n");
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000265
Chris Lattnerf325e392004-01-27 21:53:14 +0000266 // Get the formal argument and return nodes for the called function and
267 // merge them with the cloned subgraph.
268 RC.mergeCallSite(CalleeGraph.getCallSiteForArguments(CF), CS);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000269 ++NumTDInlines;
Chris Lattnera8da51b2003-07-02 04:39:44 +0000270 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000271 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000272
273 DEBUG(std::cerr << " [TD] Done inlining into callees for: "
274 << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
275 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000276}
Chris Lattner851b5342005-01-24 20:00:14 +0000277
278static const Function *getFnForValue(const Value *V) {
279 if (const Instruction *I = dyn_cast<Instruction>(V))
280 return I->getParent()->getParent();
281 else if (const Argument *A = dyn_cast<Argument>(V))
282 return A->getParent();
283 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
284 return BB->getParent();
285 return 0;
286}
287
288void TDDataStructures::deleteValue(Value *V) {
289 if (const Function *F = getFnForValue(V)) { // Function local value?
290 // If this is a function local value, just delete it from the scalar map!
291 getDSGraph(*F).getScalarMap().eraseIfExists(V);
292 return;
293 }
294
Chris Lattnercff8ac22005-01-31 00:10:45 +0000295 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000296 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
297 "cannot handle scc's");
298 delete DSInfo[F];
299 DSInfo.erase(F);
300 return;
301 }
302
303 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
304}
305
306void TDDataStructures::copyValue(Value *From, Value *To) {
307 if (From == To) return;
308 if (const Function *F = getFnForValue(From)) { // Function local value?
309 // If this is a function local value, just delete it from the scalar map!
310 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
311 return;
312 }
313
314 if (Function *FromF = dyn_cast<Function>(From)) {
315 Function *ToF = cast<Function>(To);
316 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000317 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000318 DSInfo[ToF] = NG;
319 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
320
321 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000322 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000323 NG->getReturnNodes().clear();
324 NG->getReturnNodes()[ToF] = Ret;
325 return;
326 }
327
328 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
329}