blob: 85a79d1076a31602c80cecc563355752b37c1ce8 [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"
Chris Lattner0e744122002-10-17 04:26:54 +000011#include "llvm/Analysis/DSGraph.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000012#include "llvm/Module.h"
13#include "llvm/DerivedTypes.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include "Support/Statistic.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 Lattner4923d1b2003-02-03 22:51:28 +000019}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000020
Chris Lattneraa0b4682002-11-09 21:12:07 +000021// run - Calculate the top down data structure graphs for each function in the
22// program.
23//
24bool TDDataStructures::run(Module &M) {
25 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattner312edd32003-06-28 22:14:55 +000026 GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
Chris Lattneraa0b4682002-11-09 21:12:07 +000027
28 // Calculate top-down from main...
29 if (Function *F = M.getMainFunction())
30 calculateGraph(*F);
31
32 // Next calculate the graphs for each function unreachable function...
33 for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
34 if (!I->isExternal())
35 calculateGraph(*I);
36
37 GraphDone.clear(); // Free temporary memory...
38 return false;
39}
40
Vikram S. Adveaaeee752002-07-30 22:06:40 +000041// releaseMemory - If the pass pipeline is done with this pass, we can release
42// our memory... here...
43//
Chris Lattner4923d1b2003-02-03 22:51:28 +000044// FIXME: This should be releaseMemory and will work fine, except that LoadVN
45// has no way to extend the lifetime of the pass, which screws up ds-aa.
46//
47void TDDataStructures::releaseMyMemory() {
Chris Lattnere0f4b982003-06-22 03:03:52 +000048 return;
Chris Lattner41c04f72003-02-01 04:52:08 +000049 for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Vikram S. Adveaaeee752002-07-30 22:06:40 +000050 E = DSInfo.end(); I != E; ++I)
51 delete I->second;
52
53 // Empty map so next time memory is released, data structures are not
54 // re-deleted.
55 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000056 delete GlobalsGraph;
57 GlobalsGraph = 0;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000058}
59
Chris Lattner5a540632003-06-30 03:15:25 +000060#if 0
Chris Lattner0e744122002-10-17 04:26:54 +000061/// ResolveCallSite - This method is used to link the actual arguments together
62/// with the formal arguments for a function call in the top-down closure. This
63/// method assumes that the call site arguments have been mapped into nodes
64/// local to the specified graph.
65///
66void TDDataStructures::ResolveCallSite(DSGraph &Graph,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000067 const DSCallSite &CallSite) {
Chris Lattner0e744122002-10-17 04:26:54 +000068 // Resolve all of the function formal arguments...
69 Function &F = Graph.getFunction();
70 Function::aiterator AI = F.abegin();
Vikram S. Adveaaeee752002-07-30 22:06:40 +000071
Vikram S. Adve26b98262002-10-20 21:41:02 +000072 for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0e744122002-10-17 04:26:54 +000073 // Advance the argument iterator to the first pointer argument...
Chris Lattnerb1060432002-11-07 05:20:53 +000074 while (!DS::isPointerType(AI->getType())) ++AI;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000075
Chris Lattner0e744122002-10-17 04:26:54 +000076 // TD ...Merge the formal arg scalar with the actual arg node
77 DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
Chris Lattner99a22842002-10-21 15:04:18 +000078 assert(NodeForFormal.getNode() && "Pointer argument has no dest node!");
79 NodeForFormal.mergeWith(CallSite.getPtrArg(i));
Chris Lattner0e744122002-10-17 04:26:54 +000080 }
81
82 // Merge returned node in the caller with the "return" node in callee
Chris Lattner0969c502002-10-21 02:08:03 +000083 if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
84 Graph.getRetNode().mergeWith(CallSite.getRetVal());
Vikram S. Adveaaeee752002-07-30 22:06:40 +000085}
Chris Lattner5a540632003-06-30 03:15:25 +000086#endif
Vikram S. Adveaaeee752002-07-30 22:06:40 +000087
Chris Lattner7a211632002-11-08 21:28:37 +000088DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
89 DSGraph *&G = DSInfo[&F];
90 if (G == 0) { // Not created yet? Clone BU graph...
91 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
92 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +000093 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000094 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +000095 }
96 return *G;
97}
Vikram S. Adve26b98262002-10-20 21:41:02 +000098
Chris Lattnerdea81462003-06-29 22:37:07 +000099
100/// FunctionHasCompleteArguments - This function returns true if it is safe not
101/// to mark arguments to the function complete.
102///
103/// FIXME: Need to check if all callers have been found, or rather if a
104/// funcpointer escapes!
105///
106static bool FunctionHasCompleteArguments(Function &F) {
107 return F.hasInternalLinkage();
108}
109
110
Chris Lattner7a211632002-11-08 21:28:37 +0000111void TDDataStructures::calculateGraph(Function &F) {
112 // Make sure this graph has not already been calculated, and that we don't get
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000113 // into an infinite loop with mutually recursive functions.
114 //
Chris Lattner7a211632002-11-08 21:28:37 +0000115 if (GraphDone.count(&F)) return;
116 GraphDone.insert(&F);
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000117
Chris Lattner7a211632002-11-08 21:28:37 +0000118 // Get the current functions graph...
119 DSGraph &Graph = getOrCreateDSGraph(F);
Chris Lattner198be222002-10-21 19:47:18 +0000120
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000121 // Recompute the Incomplete markers and eliminate unreachable nodes.
122 Graph.maskIncompleteMarkers();
Chris Lattnerdea81462003-06-29 22:37:07 +0000123 unsigned Flags = FunctionHasCompleteArguments(F) ?
124 DSGraph::IgnoreFormalArgs : DSGraph::MarkFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000125 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
126 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
127
Chris Lattner7a211632002-11-08 21:28:37 +0000128 const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls();
Chris Lattner7a211632002-11-08 21:28:37 +0000129 if (CallSites.empty()) {
130 DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n");
Chris Lattnere0fbd482003-02-09 18:42:43 +0000131 } else {
132 // Loop over all of the call sites, building a multi-map from Callees to
133 // DSCallSite*'s. With this map we can then loop over each callee, cloning
134 // this graph once into it, then resolving arguments.
135 //
136 std::multimap<Function*, const DSCallSite*> CalleeSites;
137 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
138 const DSCallSite &CS = CallSites[i];
139 if (CS.isDirectCall()) {
140 if (!CS.getCalleeFunc()->isExternal()) // If it's not external
141 CalleeSites.insert(std::make_pair(CS.getCalleeFunc(), &CS));// Keep it
142 } else {
143 const std::vector<GlobalValue*> &Callees =
144 CS.getCalleeNode()->getGlobals();
Chris Lattnerce2d1322002-11-08 22:26:43 +0000145
Chris Lattnere0fbd482003-02-09 18:42:43 +0000146 // Loop over all of the functions that this call may invoke...
147 for (unsigned c = 0, e = Callees.size(); c != e; ++c)
148 if (Function *F = dyn_cast<Function>(Callees[c]))// If this is a fn...
149 if (!F->isExternal()) // If it's not extern
150 CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it!
151 }
Chris Lattner923fc052003-02-05 21:59:58 +0000152 }
Chris Lattner0e744122002-10-17 04:26:54 +0000153
Chris Lattnere0fbd482003-02-09 18:42:43 +0000154 // Now that we have information about all of the callees, propagate the
155 // current graph into the callees.
156 //
157 DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into "
158 << CalleeSites.size() << " callees.\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000159
Chris Lattnere0fbd482003-02-09 18:42:43 +0000160 // Loop over all the callees...
161 for (std::multimap<Function*, const DSCallSite*>::iterator
162 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; )
163 if (I->first == &F) { // Bottom-up pass takes care of self loops!
164 ++I;
165 } else {
166 // For each callee...
Chris Lattnerdea81462003-06-29 22:37:07 +0000167 Function &Callee = *I->first;
168 DSGraph &CG = getOrCreateDSGraph(Callee); // Get the callee's graph...
Chris Lattner7a211632002-11-08 21:28:37 +0000169
Chris Lattnerdea81462003-06-29 22:37:07 +0000170 DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee.getName()
171 << "'\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000172
Chris Lattnere0fbd482003-02-09 18:42:43 +0000173 // Clone our current graph into the callee...
Chris Lattner5a540632003-06-30 03:15:25 +0000174 DSGraph::ScalarMapTy OldValMap;
175 DSGraph::NodeMapTy OldNodeMap;
176 DSGraph::ReturnNodesTy ReturnNodes;
177 CG.cloneInto(Graph, OldValMap, ReturnNodes, OldNodeMap,
Chris Lattnere0fbd482003-02-09 18:42:43 +0000178 DSGraph::StripModRefBits |
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000179 DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes |
180 DSGraph::DontCloneAuxCallNodes);
Chris Lattnere0fbd482003-02-09 18:42:43 +0000181 OldValMap.clear(); // We don't care about the ValMap
Chris Lattner5a540632003-06-30 03:15:25 +0000182 ReturnNodes.clear(); // We don't care about return values either
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000183
Chris Lattnere0fbd482003-02-09 18:42:43 +0000184 // Loop over all of the invocation sites of the callee, resolving
185 // arguments to our graph. This loop may iterate multiple times if the
186 // current function calls this callee multiple times with different
187 // signatures.
188 //
Chris Lattnerdea81462003-06-29 22:37:07 +0000189 for (; I != E && I->first == &Callee; ++I) {
Chris Lattnere0fbd482003-02-09 18:42:43 +0000190 // Map call site into callee graph
191 DSCallSite NewCS(*I->second, OldNodeMap);
Chris Lattner7a211632002-11-08 21:28:37 +0000192
Chris Lattnere0fbd482003-02-09 18:42:43 +0000193 // Resolve the return values...
Chris Lattner5a540632003-06-30 03:15:25 +0000194 NewCS.getRetVal().mergeWith(CG.getReturnNodeFor(Callee));
Chris Lattner7a211632002-11-08 21:28:37 +0000195
Chris Lattnere0fbd482003-02-09 18:42:43 +0000196 // Resolve all of the arguments...
Chris Lattnerdea81462003-06-29 22:37:07 +0000197 Function::aiterator AI = Callee.abegin();
Chris Lattnere0fbd482003-02-09 18:42:43 +0000198 for (unsigned i = 0, e = NewCS.getNumPtrArgs();
Chris Lattnerdea81462003-06-29 22:37:07 +0000199 i != e && AI != Callee.aend(); ++i, ++AI) {
Chris Lattnere0fbd482003-02-09 18:42:43 +0000200 // Advance the argument iterator to the first pointer argument...
Chris Lattnerdea81462003-06-29 22:37:07 +0000201 while (AI != Callee.aend() && !DS::isPointerType(AI->getType()))
Chris Lattnere0fbd482003-02-09 18:42:43 +0000202 ++AI;
Chris Lattnerdea81462003-06-29 22:37:07 +0000203 if (AI == Callee.aend()) break;
Chris Lattner72d29a42003-02-11 23:11:51 +0000204
Chris Lattnere0fbd482003-02-09 18:42:43 +0000205 // Add the link from the argument scalar to the provided value
206 DSNodeHandle &NH = CG.getNodeForValue(AI);
207 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
208 NH.mergeWith(NewCS.getPtrArg(i));
209 }
Chris Lattner7a211632002-11-08 21:28:37 +0000210 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000211
212 // Done with the nodemap...
213 OldNodeMap.clear();
214
215 // Recompute the Incomplete markers and eliminate unreachable nodes.
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000216 CG.removeTriviallyDeadNodes();
Chris Lattnere0fbd482003-02-09 18:42:43 +0000217 CG.maskIncompleteMarkers();
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000218 CG.markIncompleteNodes(DSGraph::MarkFormalArgs |DSGraph::IgnoreGlobals);
Chris Lattnere0fbd482003-02-09 18:42:43 +0000219 CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
Chris Lattner7a211632002-11-08 21:28:37 +0000220 }
221
Chris Lattnere0fbd482003-02-09 18:42:43 +0000222 DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName()
223 << " [" << Graph.getGraphSize() << "+"
224 << Graph.getFunctionCalls().size() << "]\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000225
Chris Lattnere0fbd482003-02-09 18:42:43 +0000226 // Loop over all the callees... making sure they are all resolved now...
227 Function *LastFunc = 0;
228 for (std::multimap<Function*, const DSCallSite*>::iterator
229 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
230 if (I->first != LastFunc) { // Only visit each callee once...
231 LastFunc = I->first;
232 calculateGraph(*I->first);
233 }
234 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000235}
Chris Lattner4923d1b2003-02-03 22:51:28 +0000236