blob: f1d13b4d53de5d5b07d86ce687554b41b759702b [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 Lattner3d162902003-06-30 04:53:08 +000048 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
49 E = DSInfo.end(); I != E; ++I) {
50 I->second->getReturnNodes().erase(I->first);
51 if (I->second->getReturnNodes().empty())
52 delete I->second;
53 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +000054
55 // Empty map so next time memory is released, data structures are not
56 // re-deleted.
57 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000058 delete GlobalsGraph;
59 GlobalsGraph = 0;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000060}
61
Vikram S. Adveaaeee752002-07-30 22:06:40 +000062
Chris Lattner7a211632002-11-08 21:28:37 +000063DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
64 DSGraph *&G = DSInfo[&F];
65 if (G == 0) { // Not created yet? Clone BU graph...
66 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
67 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +000068 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000069 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +000070 }
71 return *G;
72}
Vikram S. Adve26b98262002-10-20 21:41:02 +000073
Chris Lattnerdea81462003-06-29 22:37:07 +000074
75/// FunctionHasCompleteArguments - This function returns true if it is safe not
76/// to mark arguments to the function complete.
77///
78/// FIXME: Need to check if all callers have been found, or rather if a
79/// funcpointer escapes!
80///
81static bool FunctionHasCompleteArguments(Function &F) {
82 return F.hasInternalLinkage();
83}
84
85
Chris Lattner7a211632002-11-08 21:28:37 +000086void TDDataStructures::calculateGraph(Function &F) {
87 // Make sure this graph has not already been calculated, and that we don't get
Vikram S. Adveaaeee752002-07-30 22:06:40 +000088 // into an infinite loop with mutually recursive functions.
89 //
Chris Lattner7a211632002-11-08 21:28:37 +000090 if (GraphDone.count(&F)) return;
91 GraphDone.insert(&F);
Vikram S. Adveaaeee752002-07-30 22:06:40 +000092
Chris Lattner7a211632002-11-08 21:28:37 +000093 // Get the current functions graph...
94 DSGraph &Graph = getOrCreateDSGraph(F);
Chris Lattner198be222002-10-21 19:47:18 +000095
Chris Lattner4f2cfc02003-02-10 18:16:36 +000096 // Recompute the Incomplete markers and eliminate unreachable nodes.
97 Graph.maskIncompleteMarkers();
Chris Lattnerdea81462003-06-29 22:37:07 +000098 unsigned Flags = FunctionHasCompleteArguments(F) ?
99 DSGraph::IgnoreFormalArgs : DSGraph::MarkFormalArgs;
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000100 Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
101 Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
102
Chris Lattner7a211632002-11-08 21:28:37 +0000103 const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls();
Chris Lattner7a211632002-11-08 21:28:37 +0000104 if (CallSites.empty()) {
105 DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n");
Chris Lattnere0fbd482003-02-09 18:42:43 +0000106 } else {
107 // Loop over all of the call sites, building a multi-map from Callees to
108 // DSCallSite*'s. With this map we can then loop over each callee, cloning
109 // this graph once into it, then resolving arguments.
110 //
111 std::multimap<Function*, const DSCallSite*> CalleeSites;
112 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
113 const DSCallSite &CS = CallSites[i];
114 if (CS.isDirectCall()) {
115 if (!CS.getCalleeFunc()->isExternal()) // If it's not external
116 CalleeSites.insert(std::make_pair(CS.getCalleeFunc(), &CS));// Keep it
117 } else {
118 const std::vector<GlobalValue*> &Callees =
119 CS.getCalleeNode()->getGlobals();
Chris Lattnerce2d1322002-11-08 22:26:43 +0000120
Chris Lattnere0fbd482003-02-09 18:42:43 +0000121 // Loop over all of the functions that this call may invoke...
122 for (unsigned c = 0, e = Callees.size(); c != e; ++c)
123 if (Function *F = dyn_cast<Function>(Callees[c]))// If this is a fn...
124 if (!F->isExternal()) // If it's not extern
125 CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it!
126 }
Chris Lattner923fc052003-02-05 21:59:58 +0000127 }
Chris Lattner0e744122002-10-17 04:26:54 +0000128
Chris Lattnere0fbd482003-02-09 18:42:43 +0000129 // Now that we have information about all of the callees, propagate the
130 // current graph into the callees.
131 //
132 DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into "
133 << CalleeSites.size() << " callees.\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000134
Chris Lattnere0fbd482003-02-09 18:42:43 +0000135 // Loop over all the callees...
136 for (std::multimap<Function*, const DSCallSite*>::iterator
137 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; )
138 if (I->first == &F) { // Bottom-up pass takes care of self loops!
139 ++I;
140 } else {
141 // For each callee...
Chris Lattnerdea81462003-06-29 22:37:07 +0000142 Function &Callee = *I->first;
143 DSGraph &CG = getOrCreateDSGraph(Callee); // Get the callee's graph...
Chris Lattner7a211632002-11-08 21:28:37 +0000144
Chris Lattnerdea81462003-06-29 22:37:07 +0000145 DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee.getName()
146 << "'\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000147
Chris Lattnere0fbd482003-02-09 18:42:43 +0000148 // Clone our current graph into the callee...
Chris Lattner5a540632003-06-30 03:15:25 +0000149 DSGraph::ScalarMapTy OldValMap;
150 DSGraph::NodeMapTy OldNodeMap;
151 DSGraph::ReturnNodesTy ReturnNodes;
152 CG.cloneInto(Graph, OldValMap, ReturnNodes, OldNodeMap,
Chris Lattnere0fbd482003-02-09 18:42:43 +0000153 DSGraph::StripModRefBits |
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000154 DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes |
155 DSGraph::DontCloneAuxCallNodes);
Chris Lattnere0fbd482003-02-09 18:42:43 +0000156 OldValMap.clear(); // We don't care about the ValMap
Chris Lattner5a540632003-06-30 03:15:25 +0000157 ReturnNodes.clear(); // We don't care about return values either
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000158
Chris Lattnere0fbd482003-02-09 18:42:43 +0000159 // Loop over all of the invocation sites of the callee, resolving
160 // arguments to our graph. This loop may iterate multiple times if the
161 // current function calls this callee multiple times with different
162 // signatures.
163 //
Chris Lattnerdea81462003-06-29 22:37:07 +0000164 for (; I != E && I->first == &Callee; ++I) {
Chris Lattnere0fbd482003-02-09 18:42:43 +0000165 // Map call site into callee graph
166 DSCallSite NewCS(*I->second, OldNodeMap);
Chris Lattner7a211632002-11-08 21:28:37 +0000167
Chris Lattnere0fbd482003-02-09 18:42:43 +0000168 // Resolve the return values...
Chris Lattner5a540632003-06-30 03:15:25 +0000169 NewCS.getRetVal().mergeWith(CG.getReturnNodeFor(Callee));
Chris Lattner7a211632002-11-08 21:28:37 +0000170
Chris Lattnere0fbd482003-02-09 18:42:43 +0000171 // Resolve all of the arguments...
Chris Lattnerdea81462003-06-29 22:37:07 +0000172 Function::aiterator AI = Callee.abegin();
Chris Lattnere0fbd482003-02-09 18:42:43 +0000173 for (unsigned i = 0, e = NewCS.getNumPtrArgs();
Chris Lattnerdea81462003-06-29 22:37:07 +0000174 i != e && AI != Callee.aend(); ++i, ++AI) {
Chris Lattnere0fbd482003-02-09 18:42:43 +0000175 // Advance the argument iterator to the first pointer argument...
Chris Lattnerdea81462003-06-29 22:37:07 +0000176 while (AI != Callee.aend() && !DS::isPointerType(AI->getType()))
Chris Lattnere0fbd482003-02-09 18:42:43 +0000177 ++AI;
Chris Lattnerdea81462003-06-29 22:37:07 +0000178 if (AI == Callee.aend()) break;
Chris Lattner72d29a42003-02-11 23:11:51 +0000179
Chris Lattnere0fbd482003-02-09 18:42:43 +0000180 // Add the link from the argument scalar to the provided value
181 DSNodeHandle &NH = CG.getNodeForValue(AI);
182 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
183 NH.mergeWith(NewCS.getPtrArg(i));
184 }
Chris Lattner7a211632002-11-08 21:28:37 +0000185 }
Chris Lattnere0fbd482003-02-09 18:42:43 +0000186
187 // Done with the nodemap...
188 OldNodeMap.clear();
189
190 // Recompute the Incomplete markers and eliminate unreachable nodes.
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000191 CG.removeTriviallyDeadNodes();
Chris Lattnere0fbd482003-02-09 18:42:43 +0000192 CG.maskIncompleteMarkers();
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000193 CG.markIncompleteNodes(DSGraph::MarkFormalArgs |DSGraph::IgnoreGlobals);
Chris Lattnere0fbd482003-02-09 18:42:43 +0000194 CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
Chris Lattner7a211632002-11-08 21:28:37 +0000195 }
196
Chris Lattnere0fbd482003-02-09 18:42:43 +0000197 DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName()
198 << " [" << Graph.getGraphSize() << "+"
199 << Graph.getFunctionCalls().size() << "]\n");
Chris Lattner7a211632002-11-08 21:28:37 +0000200
Chris Lattnere0fbd482003-02-09 18:42:43 +0000201 // Loop over all the callees... making sure they are all resolved now...
202 Function *LastFunc = 0;
203 for (std::multimap<Function*, const DSCallSite*>::iterator
204 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
205 if (I->first != LastFunc) { // Only visit each callee once...
206 LastFunc = I->first;
207 calculateGraph(*I->first);
208 }
209 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000210}
Chris Lattner4923d1b2003-02-03 22:51:28 +0000211