blob: 11ee6065d190b4e2a4e1cb5206ad57f9f1372579 [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00002//
3// This file implements the BUDataStructures class, which represents the
4// Bottom-Up Interprocedural closure of the data structure graph over the
5// program. This is useful for applications like pool allocation, but **not**
Chris Lattner55c10582002-10-03 20:38:41 +00006// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
Chris Lattner55c10582002-10-03 20:38:41 +000011#include "llvm/Analysis/DSGraph.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000012#include "llvm/Module.h"
Chris Lattner55c10582002-10-03 20:38:41 +000013//#include "llvm/DerivedTypes.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000014#include "Support/Statistic.h"
Chris Lattner55c10582002-10-03 20:38:41 +000015//#include <set>
Chris Lattner0d9bab82002-07-18 00:12:30 +000016using std::map;
17
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterAnalysis<BUDataStructures>
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000019X("budatastructure", "Bottom-up Data Structure Analysis Closure");
Chris Lattner0d9bab82002-07-18 00:12:30 +000020
Chris Lattner55c10582002-10-03 20:38:41 +000021// TODO: FIXME
22namespace DataStructureAnalysis {
23 // isPointerType - Return true if this first class type is big enough to hold
24 // a pointer.
25 //
26 bool isPointerType(const Type *Ty);
27}
28using namespace DataStructureAnalysis;
29
30
Chris Lattner0d9bab82002-07-18 00:12:30 +000031// releaseMemory - If the pass pipeline is done with this pass, we can release
32// our memory... here...
33//
34void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000035 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000036 E = DSInfo.end(); I != E; ++I)
37 delete I->second;
38
39 // Empty map so next time memory is released, data structures are not
40 // re-deleted.
41 DSInfo.clear();
42}
43
44// run - Calculate the bottom up data structure graphs for each function in the
45// program.
46//
47bool BUDataStructures::run(Module &M) {
48 // Simply calculate the graphs for each function...
49 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
50 if (!I->isExternal())
51 calculateGraph(*I);
52 return false;
53}
54
Chris Lattner0d9bab82002-07-18 00:12:30 +000055// ResolveArguments - Resolve the formal and actual arguments for a function
56// call.
57//
58static void ResolveArguments(std::vector<DSNodeHandle> &Call, Function &F,
59 map<Value*, DSNodeHandle> &ValueMap) {
60 // Resolve all of the function arguments...
61 Function::aiterator AI = F.abegin();
62 for (unsigned i = 2, e = Call.size(); i != e; ++i) {
63 // Advance the argument iterator to the first pointer argument...
Chris Lattner55c10582002-10-03 20:38:41 +000064 while (!isPointerType(AI->getType())) ++AI;
Chris Lattner0d9bab82002-07-18 00:12:30 +000065
66 // Add the link from the argument scalar to the provided value
Chris Lattner55c10582002-10-03 20:38:41 +000067 DSNodeHandle &NN = ValueMap[AI];
68 NN.addEdgeTo(Call[i]);
Chris Lattner0d9bab82002-07-18 00:12:30 +000069 ++AI;
70 }
71}
72
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000073// MergeGlobalNodes - Merge all existing global nodes with globals
74// inlined from the callee or with globals from the GlobalsGraph.
Chris Lattner0d9bab82002-07-18 00:12:30 +000075//
Chris Lattner55c10582002-10-03 20:38:41 +000076static void MergeGlobalNodes(DSGraph &Graph,
Chris Lattner0d9bab82002-07-18 00:12:30 +000077 map<Value*, DSNodeHandle> &OldValMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000078 map<Value*, DSNodeHandle> &ValMap = Graph.getValueMap();
79 for (map<Value*, DSNodeHandle>::iterator I = ValMap.begin(), E = ValMap.end();
80 I != E; ++I)
Chris Lattner55c10582002-10-03 20:38:41 +000081 if (GlobalValue *GV = dyn_cast<GlobalValue>(I->first)) {
82 map<Value*, DSNodeHandle>::iterator NHI = OldValMap.find(GV);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000083 if (NHI != OldValMap.end()) // was it inlined from the callee?
Chris Lattner55c10582002-10-03 20:38:41 +000084 I->second.mergeWith(NHI->second);
85#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000086 else // get it from the GlobalsGraph
Chris Lattner55c10582002-10-03 20:38:41 +000087 I->second.mergeWith(Graph.cloneGlobalInto(GV));
88#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000089 }
90
91 // Add unused inlined global nodes into the value map
Chris Lattner0d9bab82002-07-18 00:12:30 +000092 for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(),
93 E = OldValMap.end(); I != E; ++I)
94 if (isa<GlobalValue>(I->first)) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000095 DSNodeHandle &NH = ValMap[I->first]; // If global is not in ValMap...
Chris Lattner55c10582002-10-03 20:38:41 +000096 if (NH.getNode() == 0)
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000097 NH = I->second; // Add the one just inlined.
Chris Lattner0d9bab82002-07-18 00:12:30 +000098 }
99
100}
101
102DSGraph &BUDataStructures::calculateGraph(Function &F) {
103 // Make sure this graph has not already been calculated, or that we don't get
104 // into an infinite loop with mutually recursive functions.
105 //
106 DSGraph *&Graph = DSInfo[&F];
107 if (Graph) return *Graph;
108
109 // Copy the local version into DSInfo...
110 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
111
Chris Lattner55c10582002-10-03 20:38:41 +0000112#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000113 // Populate the GlobalsGraph with globals from this one.
114 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
115
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000116 // Save a copy of the original call nodes for the top-down pass
117 Graph->saveOrigFunctionCalls();
Chris Lattner55c10582002-10-03 20:38:41 +0000118#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000119
Chris Lattner0d9bab82002-07-18 00:12:30 +0000120 // Start resolving calls...
121 std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls();
122
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000123 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
124
Chris Lattner55c10582002-10-03 20:38:41 +0000125#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000126 // Add F to the PendingCallers list of each direct callee for use in the
127 // top-down pass so we don't have to compute this again. We don't want
128 // to do it for indirect callees inlined later, so remember which calls
129 // are in the original FCs set.
130 std::set<const DSNode*> directCallees;
131 for (unsigned i = 0; i < FCs.size(); ++i)
132 directCallees.insert(FCs[i][1]); // ptr to function node
Chris Lattner55c10582002-10-03 20:38:41 +0000133#endif
Chris Lattner0d9bab82002-07-18 00:12:30 +0000134
135 bool Inlined;
136 do {
137 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000138
Chris Lattner0d9bab82002-07-18 00:12:30 +0000139 for (unsigned i = 0; i != FCs.size(); ++i) {
140 // Copy the call, because inlining graphs may invalidate the FCs vector.
141 std::vector<DSNodeHandle> Call = FCs[i];
142
Chris Lattner55c10582002-10-03 20:38:41 +0000143 // If the function list is complete...
144 if ((Call[1].getNode()->NodeType & DSNode::Incomplete) == 0) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000145 // Start inlining all of the functions we can... some may not be
146 // inlinable if they are external...
147 //
Chris Lattner55c10582002-10-03 20:38:41 +0000148 std::vector<GlobalValue*> Callees(Call[1].getNode()->getGlobals());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000149
150 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000151 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000152 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000153 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000154 if (&FI == &F) {
155 // Self recursion... simply link up the formal arguments with the
156 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000157
158 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000159
Chris Lattner55c10582002-10-03 20:38:41 +0000160 if (Call[0].getNode()) // Handle the return value if present...
161 Graph->getRetNode().mergeWith(Call[0]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000162
163 // Resolve the arguments in the call to the actual values...
164 ResolveArguments(Call, F, Graph->getValueMap());
165
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000166 // Erase the entry in the callees vector
167 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000168 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000169 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000170 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000171
Chris Lattner0d9bab82002-07-18 00:12:30 +0000172 // Get the data structure graph for the called function, closing it
173 // if possible (which is only impossible in the case of mutual
174 // recursion...
175 //
176 DSGraph &GI = calculateGraph(FI); // Graph to inline
177
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000178 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
179 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000180
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000181 // Clone the callee's graph into the current graph, keeping
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000182 // track of where scalars in the old graph _used_ to point,
183 // and of the new nodes matching nodes of the old graph.
Chris Lattner55c10582002-10-03 20:38:41 +0000184 map<Value*, DSNodeHandle> OldValMap;
185 map<const DSNode*, DSNode*> OldNodeMap;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000186
187 // The clone call may invalidate any of the vectors in the data
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000188 // structure graph. Strip locals and don't copy the list of callers
Chris Lattner55c10582002-10-03 20:38:41 +0000189 DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
190 /*StripScalars*/ true,
191 /*StripAllocas*/ true,
192 /*CopyCallers*/ false,
193 /*CopyOrigCalls*/ false);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000194
Chris Lattner55c10582002-10-03 20:38:41 +0000195 // Resolve the arguments in the call to the actual values...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000196 ResolveArguments(Call, FI, OldValMap);
197
Chris Lattner55c10582002-10-03 20:38:41 +0000198 if (Call[0].getNode()) // Handle the return value if present
199 RetVal.mergeWith(Call[0]);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000200
Chris Lattner0d9bab82002-07-18 00:12:30 +0000201 // Merge global value nodes in the inlined graph with the global
202 // value nodes in the current graph if there are duplicates.
203 //
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000204 MergeGlobalNodes(*Graph, OldValMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000205
Chris Lattner55c10582002-10-03 20:38:41 +0000206#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000207 // If this was an original call, add F to the PendingCallers list
208 if (directCallees.find(Call[1]) != directCallees.end())
209 GI.addCaller(F);
Chris Lattner55c10582002-10-03 20:38:41 +0000210#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000211
212 // Erase the entry in the Callees vector
213 Callees.erase(Callees.begin()+c--);
214
Chris Lattner9eee58d2002-07-19 18:11:43 +0000215 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
216 FI.getName() == "fprintf" || FI.getName() == "open" ||
217 FI.getName() == "sprintf") {
218
219 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000220 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000221 }
222 }
223
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000224 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000225 // Erase the call if it is resolvable...
226 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
227 Inlined = true;
Chris Lattner55c10582002-10-03 20:38:41 +0000228 } else if (Callees.size() != Call[1].getNode()->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000229 // Was able to inline SOME, but not all of the functions. Construct a
230 // new global node here.
231 //
232 assert(0 && "Unimpl!");
233 Inlined = true;
234 }
235 }
236 }
237
238 // Recompute the Incomplete markers. If there are any function calls left
239 // now that are complete, we must loop!
240 if (Inlined) {
241 Graph->maskIncompleteMarkers();
242 Graph->markIncompleteNodes();
Chris Lattner55c10582002-10-03 20:38:41 +0000243 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000244 }
245 } while (Inlined && !FCs.empty());
246
Chris Lattner55c10582002-10-03 20:38:41 +0000247#if 0
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000248 // Copy any unresolved call nodes into the Globals graph and
249 // filter out unresolved call nodes inlined from the callee.
250 if (!FCs.empty())
251 Graph->GlobalsGraph->cloneCalls(*Graph);
Chris Lattner55c10582002-10-03 20:38:41 +0000252#endif
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000253
254 Graph->maskIncompleteMarkers();
255 Graph->markIncompleteNodes();
Chris Lattner55c10582002-10-03 20:38:41 +0000256 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000257
Chris Lattner221c9792002-08-07 21:41:11 +0000258 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
259 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
260 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000261
Chris Lattner0d9bab82002-07-18 00:12:30 +0000262 return *Graph;
263}