blob: 75104ca49cee96107ae407edb4db304f6536f482 [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 Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.h"
Chris Lattner18f07a12003-07-01 16:28:11 +000014#include "DSCallSiteIterator.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000015
Chris Lattner4923d1b2003-02-03 22:51:28 +000016namespace {
17 RegisterAnalysis<TDDataStructures> // Register the pass
Chris Lattner312edd32003-06-28 22:14:55 +000018 Y("tddatastructure", "Top-down Data Structure Analysis");
Chris Lattnera8da51b2003-07-02 04:39:44 +000019
20 Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
21}
22
23/// FunctionHasCompleteArguments - This function returns true if it is safe not
24/// to mark arguments to the function complete.
25///
26/// FIXME: Need to check if all callers have been found, or rather if a
27/// funcpointer escapes!
28///
29static bool FunctionHasCompleteArguments(Function &F) {
30 return F.hasInternalLinkage();
Chris Lattner4923d1b2003-02-03 22:51:28 +000031}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000032
Chris Lattneraa0b4682002-11-09 21:12:07 +000033// run - Calculate the top down data structure graphs for each function in the
34// program.
35//
36bool TDDataStructures::run(Module &M) {
37 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattner312edd32003-06-28 22:14:55 +000038 GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
Chris Lattneraa0b4682002-11-09 21:12:07 +000039
Chris Lattnera8da51b2003-07-02 04:39:44 +000040 // Figure out which functions must not mark their arguments complete because
41 // they are accessible outside this compilation unit.
42 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
43 if (!FunctionHasCompleteArguments(*I))
44 ArgsRemainIncomplete.insert(I);
45
Chris Lattner6c874612003-07-02 23:42:48 +000046 // We want to traverse the call graph in reverse post-order. To do this, we
47 // calculate a post-order traversal, then reverse it.
48 hash_set<DSGraph*> VisitedGraph;
49 std::vector<DSGraph*> PostOrder;
50 const BUDataStructures::ActualCalleesTy &ActualCallees =
51 getAnalysis<BUDataStructures>().getActualCallees();
52
Chris Lattneraa0b4682002-11-09 21:12:07 +000053 // Calculate top-down from main...
54 if (Function *F = M.getMainFunction())
Chris Lattner6c874612003-07-02 23:42:48 +000055 ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
Chris Lattneraa0b4682002-11-09 21:12:07 +000056
57 // Next calculate the graphs for each function unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +000058 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner6c874612003-07-02 23:42:48 +000059 ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
60
61 VisitedGraph.clear(); // Release memory!
62
63 // Visit each of the graphs in reverse post-order now!
64 while (!PostOrder.empty()) {
65 inlineGraphIntoCallees(*PostOrder.back());
66 PostOrder.pop_back();
67 }
Chris Lattneraa0b4682002-11-09 21:12:07 +000068
Chris Lattnera8da51b2003-07-02 04:39:44 +000069 ArgsRemainIncomplete.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000070 return false;
71}
72
Vikram S. Adveaaeee752002-07-30 22:06:40 +000073
Chris Lattner7a211632002-11-08 21:28:37 +000074DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
75 DSGraph *&G = DSInfo[&F];
76 if (G == 0) { // Not created yet? Clone BU graph...
77 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
78 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +000079 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000080 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +000081 }
82 return *G;
83}
Vikram S. Adve26b98262002-10-20 21:41:02 +000084
Chris Lattnerdea81462003-06-29 22:37:07 +000085
Chris Lattner18f07a12003-07-01 16:28:11 +000086void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
87 std::vector<DSGraph*> &PostOrder,
88 const BUDataStructures::ActualCalleesTy &ActualCallees) {
89 if (F.isExternal()) return;
90 DSGraph &G = getOrCreateDSGraph(F);
91 if (Visited.count(&G)) return;
92 Visited.insert(&G);
93
94 // Recursively traverse all of the callee graphs.
95 const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls();
Chris Lattnerdea81462003-06-29 22:37:07 +000096
Chris Lattner18f07a12003-07-01 16:28:11 +000097 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
98 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
99 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattnera8da51b2003-07-02 04:39:44 +0000100 IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000101
Chris Lattner18f07a12003-07-01 16:28:11 +0000102 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
103 I != IP.second; ++I)
104 ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
105 }
Chris Lattner198be222002-10-21 19:47:18 +0000106
Chris Lattner18f07a12003-07-01 16:28:11 +0000107 PostOrder.push_back(&G);
108}
109
110
111
Chris Lattner18f07a12003-07-01 16:28:11 +0000112
Chris Lattner6c874612003-07-02 23:42:48 +0000113
114// releaseMemory - If the pass pipeline is done with this pass, we can release
115// our memory... here...
116//
117// FIXME: This should be releaseMemory and will work fine, except that LoadVN
118// has no way to extend the lifetime of the pass, which screws up ds-aa.
119//
120void TDDataStructures::releaseMyMemory() {
121 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
122 E = DSInfo.end(); I != E; ++I) {
123 I->second->getReturnNodes().erase(I->first);
124 if (I->second->getReturnNodes().empty())
125 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000126 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000127
Chris Lattner6c874612003-07-02 23:42:48 +0000128 // Empty map so next time memory is released, data structures are not
129 // re-deleted.
130 DSInfo.clear();
131 delete GlobalsGraph;
132 GlobalsGraph = 0;
133}
Chris Lattner18f07a12003-07-01 16:28:11 +0000134
135void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000136 // Recompute the Incomplete markers and eliminate unreachable nodes.
Chris Lattner47030f82003-07-02 19:49:11 +0000137 Graph.removeTriviallyDeadNodes();
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000138 Graph.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000139
140 // If any of the functions has incomplete incoming arguments, don't mark any
141 // of them as complete.
142 bool HasIncompleteArgs = false;
143 const DSGraph::ReturnNodesTy &GraphReturnNodes = Graph.getReturnNodes();
144 for (DSGraph::ReturnNodesTy::const_iterator I = GraphReturnNodes.begin(),
145 E = GraphReturnNodes.end(); I != E; ++I)
146 if (ArgsRemainIncomplete.count(I->first)) {
147 HasIncompleteArgs = true;
148 break;
149 }
150
151 unsigned Flags
152 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000153 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
154 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
155
Chris Lattnera8da51b2003-07-02 04:39:44 +0000156 const std::vector<DSCallSite> &FunctionCalls = Graph.getFunctionCalls();
157 if (FunctionCalls.empty()) {
Chris Lattner18f07a12003-07-01 16:28:11 +0000158 DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
159 << "\n");
160 return;
161 }
162
Chris Lattner18f07a12003-07-01 16:28:11 +0000163 // Now that we have information about all of the callees, propagate the
164 // current graph into the callees.
165 //
166 DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
Chris Lattnera8da51b2003-07-02 04:39:44 +0000167 << FunctionCalls.size() << " call nodes.\n");
Chris Lattner18f07a12003-07-01 16:28:11 +0000168
Chris Lattnera8da51b2003-07-02 04:39:44 +0000169 const BUDataStructures::ActualCalleesTy &ActualCallees =
170 getAnalysis<BUDataStructures>().getActualCallees();
Chris Lattner18f07a12003-07-01 16:28:11 +0000171
Chris Lattnera8da51b2003-07-02 04:39:44 +0000172 // Only inline this function into each real callee once. After that, just
173 // merge information into arguments...
174 hash_map<DSGraph*, DSGraph::NodeMapTy> InlinedSites;
175
176 // Loop over all the callees... cloning this graph into each one exactly once,
177 // keeping track of the node mapping information...
178 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
179 // Inline this graph into each function in the invoked function list.
180 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
181 BUDataStructures::ActualCalleesTy::const_iterator>
182 IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
183
184 int NumArgs = 0;
185 if (IP.first != IP.second) {
186 NumArgs = IP.first->second->getFunctionType()->getNumParams();
187 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
188 I != IP.second; ++I)
189 if (NumArgs != (int)I->second->getFunctionType()->getNumParams()) {
190 NumArgs = -1;
191 break;
192 }
193 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000194
Chris Lattnera8da51b2003-07-02 04:39:44 +0000195 if (NumArgs == -1) {
196 std::cerr << "ERROR: NONSAME NUMBER OF ARGUMENTS TO CALLEES\n";
197 }
198
199 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
200 I != IP.second; ++I) {
201 DSGraph &CG = getDSGraph(*I->second);
202 assert(&CG != &Graph && "TD need not inline graph into self!");
Chris Lattner18f07a12003-07-01 16:28:11 +0000203
Chris Lattnera8da51b2003-07-02 04:39:44 +0000204 if (!InlinedSites.count(&CG)) { // If we haven't already inlined into CG
205 DEBUG(std::cerr << " [TD] Inlining graph into callee graph '"
206 << CG.getFunctionNames() << "': " << I->second->getFunctionType()->getNumParams() << " args\n");
207 DSGraph::ScalarMapTy OldScalarMap;
208 DSGraph::ReturnNodesTy ReturnNodes;
209 CG.cloneInto(Graph, OldScalarMap, ReturnNodes, InlinedSites[&CG],
210 DSGraph::StripModRefBits | DSGraph::KeepAllocaBit |
211 DSGraph::DontCloneCallNodes |
212 DSGraph::DontCloneAuxCallNodes);
213 ++NumTDInlines;
Chris Lattnere0fbd482003-02-09 18:42:43 +0000214 }
Chris Lattner923fc052003-02-05 21:59:58 +0000215 }
Chris Lattnera8da51b2003-07-02 04:39:44 +0000216 }
Chris Lattner0e744122002-10-17 04:26:54 +0000217
Chris Lattnera8da51b2003-07-02 04:39:44 +0000218 // Loop over all the callees...
219 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
220 // Inline this graph into each function in the invoked function list.
221 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
222 BUDataStructures::ActualCalleesTy::const_iterator>
223 IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
224 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
225 I != IP.second; ++I) {
226 DSGraph &CG = getDSGraph(*I->second);
227 DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
228 << CG.getFunctionNames() << "'\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000229
Chris Lattnera8da51b2003-07-02 04:39:44 +0000230 // Transform our call site information into the cloned version for CG
231 DSCallSite CS(FunctionCalls[i], InlinedSites[&CG]);
232
233 // Get the arguments bindings for the called function in CG... and merge
234 // them with the cloned graph.
235 CG.getCallSiteForArguments(*I->second).mergeWith(CS);
236 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000237 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000238
239 DEBUG(std::cerr << " [TD] Done inlining into callees for: "
240 << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
241 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000242}
Chris Lattner4923d1b2003-02-03 22:51:28 +0000243