blob: 1da43e5634ff81ae4b3a823d439d4235bf33188b [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
16static RegisterAnalysis<TDDataStructures>
17Y("tddatastructure", "Top-down Data Structure Analysis Closure");
Vikram S. Adveaaeee752002-07-30 22:06:40 +000018
Chris Lattneraa0b4682002-11-09 21:12:07 +000019// run - Calculate the top down data structure graphs for each function in the
20// program.
21//
22bool TDDataStructures::run(Module &M) {
23 BUDataStructures &BU = getAnalysis<BUDataStructures>();
24 GlobalsGraph = new DSGraph();
25
26 // Calculate top-down from main...
27 if (Function *F = M.getMainFunction())
28 calculateGraph(*F);
29
30 // Next calculate the graphs for each function unreachable function...
31 for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
32 if (!I->isExternal())
33 calculateGraph(*I);
34
35 GraphDone.clear(); // Free temporary memory...
36 return false;
37}
38
Vikram S. Adveaaeee752002-07-30 22:06:40 +000039// releaseMemory - If the pass pipeline is done with this pass, we can release
40// our memory... here...
41//
42void TDDataStructures::releaseMemory() {
Chris Lattner13ec72a2002-10-21 13:31:48 +000043 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Vikram S. Adveaaeee752002-07-30 22:06:40 +000044 E = DSInfo.end(); I != E; ++I)
45 delete I->second;
46
47 // Empty map so next time memory is released, data structures are not
48 // re-deleted.
49 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000050 delete GlobalsGraph;
51 GlobalsGraph = 0;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000052}
53
Chris Lattner0e744122002-10-17 04:26:54 +000054/// ResolveCallSite - This method is used to link the actual arguments together
55/// with the formal arguments for a function call in the top-down closure. This
56/// method assumes that the call site arguments have been mapped into nodes
57/// local to the specified graph.
58///
59void TDDataStructures::ResolveCallSite(DSGraph &Graph,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000060 const DSCallSite &CallSite) {
Chris Lattner0e744122002-10-17 04:26:54 +000061 // Resolve all of the function formal arguments...
62 Function &F = Graph.getFunction();
63 Function::aiterator AI = F.abegin();
Vikram S. Adveaaeee752002-07-30 22:06:40 +000064
Vikram S. Adve26b98262002-10-20 21:41:02 +000065 for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
Chris Lattner0e744122002-10-17 04:26:54 +000066 // Advance the argument iterator to the first pointer argument...
Chris Lattnerb1060432002-11-07 05:20:53 +000067 while (!DS::isPointerType(AI->getType())) ++AI;
Vikram S. Adveaaeee752002-07-30 22:06:40 +000068
Chris Lattner0e744122002-10-17 04:26:54 +000069 // TD ...Merge the formal arg scalar with the actual arg node
70 DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
Chris Lattner99a22842002-10-21 15:04:18 +000071 assert(NodeForFormal.getNode() && "Pointer argument has no dest node!");
72 NodeForFormal.mergeWith(CallSite.getPtrArg(i));
Chris Lattner0e744122002-10-17 04:26:54 +000073 }
74
75 // Merge returned node in the caller with the "return" node in callee
Chris Lattner0969c502002-10-21 02:08:03 +000076 if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
77 Graph.getRetNode().mergeWith(CallSite.getRetVal());
Vikram S. Adveaaeee752002-07-30 22:06:40 +000078}
79
Chris Lattner7a211632002-11-08 21:28:37 +000080DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
81 DSGraph *&G = DSInfo[&F];
82 if (G == 0) { // Not created yet? Clone BU graph...
83 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
84 G->getAuxFunctionCalls().clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +000085 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +000086 }
87 return *G;
88}
Vikram S. Adve26b98262002-10-20 21:41:02 +000089
Chris Lattner7a211632002-11-08 21:28:37 +000090void TDDataStructures::calculateGraph(Function &F) {
91 // Make sure this graph has not already been calculated, and that we don't get
Vikram S. Adveaaeee752002-07-30 22:06:40 +000092 // into an infinite loop with mutually recursive functions.
93 //
Chris Lattner7a211632002-11-08 21:28:37 +000094 if (GraphDone.count(&F)) return;
95 GraphDone.insert(&F);
Vikram S. Adveaaeee752002-07-30 22:06:40 +000096
Chris Lattner7a211632002-11-08 21:28:37 +000097 // Get the current functions graph...
98 DSGraph &Graph = getOrCreateDSGraph(F);
Chris Lattner198be222002-10-21 19:47:18 +000099
Chris Lattner7a211632002-11-08 21:28:37 +0000100 const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls();
Chris Lattner7a211632002-11-08 21:28:37 +0000101 if (CallSites.empty()) {
102 DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n");
103 return; // If no call sites, the graph is the same as the BU graph!
Chris Lattner4bdb9b72002-10-22 16:01:03 +0000104 }
Chris Lattnerce2d1322002-11-08 22:26:43 +0000105
Chris Lattner7a211632002-11-08 21:28:37 +0000106 // Loop over all of the call sites, building a multi-map from Callees to
107 // DSCallSite*'s. With this map we can then loop over each callee, cloning
108 // this graph once into it, then resolving arguments.
Chris Lattner0e744122002-10-17 04:26:54 +0000109 //
Chris Lattner7a211632002-11-08 21:28:37 +0000110 std::multimap<Function*, const DSCallSite*> CalleeSites;
111 for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
112 const DSCallSite &CS = CallSites[i];
113 const std::vector<GlobalValue*> Callees =
114 CS.getCallee().getNode()->getGlobals();
Chris Lattner198be222002-10-21 19:47:18 +0000115
Chris Lattner7a211632002-11-08 21:28:37 +0000116 // Loop over all of the functions that this call may invoke...
117 for (unsigned c = 0, e = Callees.size(); c != e; ++c)
118 if (Function *F = dyn_cast<Function>(Callees[c])) // If this is a fn...
119 if (!F->isExternal()) // If it's not external
120 CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it!
Chris Lattner0e744122002-10-17 04:26:54 +0000121 }
Chris Lattner0e744122002-10-17 04:26:54 +0000122
Chris Lattner7a211632002-11-08 21:28:37 +0000123 // Now that we have information about all of the callees, propogate the
124 // current graph into the callees.
125 //
126 DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into "
127 << CalleeSites.size() << " callees.\n");
128
129 // Loop over all the callees...
130 for (std::multimap<Function*, const DSCallSite*>::iterator
131 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; )
132 if (I->first == &F) { // Bottom-up pass takes care of self loops!
133 ++I;
134 } else {
135 // For each callee...
136 Function *Callee = I->first;
137 DSGraph &CG = getOrCreateDSGraph(*Callee); // Get the callee's graph...
138
139 DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee->getName()
140 << "'\n");
141
142 // Clone our current graph into the callee...
143 std::map<Value*, DSNodeHandle> OldValMap;
144 std::map<const DSNode*, DSNodeHandle> OldNodeMap;
145 CG.cloneInto(Graph, OldValMap, OldNodeMap,
146 DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes);
147 OldValMap.clear(); // We don't care about the ValMap
148
149 // Loop over all of the invocation sites of the callee, resolving
150 // arguments to our graph. This loop may iterate multiple times if the
151 // current function calls this callee multiple times with different
152 // signatures.
153 //
154 for (; I != E && I->first == Callee; ++I) {
155 // Map call site into callee graph
156 DSCallSite NewCS(*I->second, OldNodeMap);
157
158 // Resolve the return values...
159 NewCS.getRetVal().mergeWith(CG.getRetNode());
160
161 // Resolve all of the arguments...
162 Function::aiterator AI = Callee->abegin();
163 for (unsigned i = 0, e = NewCS.getNumPtrArgs(); i != e; ++i, ++AI) {
164 // Advance the argument iterator to the first pointer argument...
165 while (!DS::isPointerType(AI->getType())) {
166 ++AI;
167#ifndef NDEBUG
168 if (AI == Callee->aend())
169 std::cerr << "Bad call to Function: " << Callee->getName()<< "\n";
170#endif
171 assert(AI != Callee->aend() &&
172 "# Args provided is not # Args required!");
173 }
174
175 // Add the link from the argument scalar to the provided value
176 DSNodeHandle &NH = CG.getNodeForValue(AI);
177 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
178 NH.mergeWith(NewCS.getPtrArg(i));
179 }
180 }
181
182 // Done with the nodemap...
183 OldNodeMap.clear();
184
185 // Recompute the Incomplete markers and eliminate unreachable nodes.
186 CG.maskIncompleteMarkers();
187 CG.markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000188 /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
Chris Lattner65f28972002-11-09 21:00:49 +0000189 CG.removeDeadNodes(/*KeepAllGlobals*/ false);
Chris Lattner7a211632002-11-08 21:28:37 +0000190 }
Chris Lattner4bdb9b72002-10-22 16:01:03 +0000191
Chris Lattner7a211632002-11-08 21:28:37 +0000192 DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName()
193 << " [" << Graph.getGraphSize() << "+"
194 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000195
Chris Lattner7a211632002-11-08 21:28:37 +0000196
197 // Loop over all the callees... making sure they are all resolved now...
198 Function *LastFunc = 0;
199 for (std::multimap<Function*, const DSCallSite*>::iterator
200 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
201 if (I->first != LastFunc) { // Only visit each callee once...
202 LastFunc = I->first;
203 calculateGraph(*I->first);
204 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000205}