blob: da9bc7c1348dff4a8cb22df3335e770baab57ee1 [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");
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000103 return; // If no call sites, there is nothing more to do here
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,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000146 DSGraph::StripModRefBits |
Chris Lattner7a211632002-11-08 21:28:37 +0000147 DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes);
148 OldValMap.clear(); // We don't care about the ValMap
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000149
Chris Lattner7a211632002-11-08 21:28:37 +0000150 // Loop over all of the invocation sites of the callee, resolving
151 // arguments to our graph. This loop may iterate multiple times if the
152 // current function calls this callee multiple times with different
153 // signatures.
154 //
155 for (; I != E && I->first == Callee; ++I) {
156 // Map call site into callee graph
157 DSCallSite NewCS(*I->second, OldNodeMap);
158
159 // Resolve the return values...
160 NewCS.getRetVal().mergeWith(CG.getRetNode());
161
162 // Resolve all of the arguments...
163 Function::aiterator AI = Callee->abegin();
Chris Lattner0ecc4262002-11-11 21:36:05 +0000164 for (unsigned i = 0, e = NewCS.getNumPtrArgs();
165 i != e && AI != Callee->aend(); ++i, ++AI) {
Chris Lattner7a211632002-11-08 21:28:37 +0000166 // Advance the argument iterator to the first pointer argument...
167 while (!DS::isPointerType(AI->getType())) {
168 ++AI;
169#ifndef NDEBUG
170 if (AI == Callee->aend())
171 std::cerr << "Bad call to Function: " << Callee->getName()<< "\n";
172#endif
173 assert(AI != Callee->aend() &&
174 "# Args provided is not # Args required!");
175 }
176
177 // Add the link from the argument scalar to the provided value
178 DSNodeHandle &NH = CG.getNodeForValue(AI);
179 assert(NH.getNode() && "Pointer argument without scalarmap entry?");
180 NH.mergeWith(NewCS.getPtrArg(i));
181 }
182 }
183
184 // Done with the nodemap...
185 OldNodeMap.clear();
186
187 // Recompute the Incomplete markers and eliminate unreachable nodes.
188 CG.maskIncompleteMarkers();
189 CG.markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage()
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000190 /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000191 CG.removeDeadNodes();
Chris Lattner7a211632002-11-08 21:28:37 +0000192 }
Chris Lattner4bdb9b72002-10-22 16:01:03 +0000193
Chris Lattner7a211632002-11-08 21:28:37 +0000194 DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName()
195 << " [" << Graph.getGraphSize() << "+"
196 << Graph.getFunctionCalls().size() << "]\n");
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000197
Chris Lattner7a211632002-11-08 21:28:37 +0000198
199 // Loop over all the callees... making sure they are all resolved now...
200 Function *LastFunc = 0;
201 for (std::multimap<Function*, const DSCallSite*>::iterator
202 I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
203 if (I->first != LastFunc) { // Only visit each callee once...
204 LastFunc = I->first;
205 calculateGraph(*I->first);
206 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000207}