blob: c3d19d4bc22850f223076029f50d846d70b3a51c [file] [log] [blame]
Chris Lattner0d9bab82002-07-18 00:12:30 +00001//===- BottomUpClosure.cpp - Compute the bottom up interprocedure closure -===//
2//
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**
6// applications like pointer analysis.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
11#include "llvm/Module.h"
12#include "llvm/DerivedTypes.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.h"
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000014#include <set>
Chris Lattner0d9bab82002-07-18 00:12:30 +000015using std::map;
16
Chris Lattnerfccd06f2002-10-01 22:33:50 +000017#if 0
18
Chris Lattner1e435162002-07-26 21:12:44 +000019static RegisterAnalysis<BUDataStructures>
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000020X("budatastructure", "Bottom-up Data Structure Analysis Closure");
Chris Lattner0d9bab82002-07-18 00:12:30 +000021
22// releaseMemory - If the pass pipeline is done with this pass, we can release
23// our memory... here...
24//
25void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000026 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000027 E = DSInfo.end(); I != E; ++I)
28 delete I->second;
29
30 // Empty map so next time memory is released, data structures are not
31 // re-deleted.
32 DSInfo.clear();
33}
34
35// run - Calculate the bottom up data structure graphs for each function in the
36// program.
37//
38bool BUDataStructures::run(Module &M) {
39 // Simply calculate the graphs for each function...
40 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
41 if (!I->isExternal())
42 calculateGraph(*I);
43 return false;
44}
45
46
47// ResolveArguments - Resolve the formal and actual arguments for a function
48// call.
49//
50static void ResolveArguments(std::vector<DSNodeHandle> &Call, Function &F,
51 map<Value*, DSNodeHandle> &ValueMap) {
52 // Resolve all of the function arguments...
53 Function::aiterator AI = F.abegin();
54 for (unsigned i = 2, e = Call.size(); i != e; ++i) {
55 // Advance the argument iterator to the first pointer argument...
56 while (!isa<PointerType>(AI->getType())) ++AI;
57
58 // Add the link from the argument scalar to the provided value
59 DSNode *NN = ValueMap[AI];
60 NN->addEdgeTo(Call[i]);
61 ++AI;
62 }
63}
64
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000065// MergeGlobalNodes - Merge all existing global nodes with globals
66// inlined from the callee or with globals from the GlobalsGraph.
Chris Lattner0d9bab82002-07-18 00:12:30 +000067//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000068static void MergeGlobalNodes(DSGraph& Graph,
Chris Lattner0d9bab82002-07-18 00:12:30 +000069 map<Value*, DSNodeHandle> &OldValMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000070 map<Value*, DSNodeHandle> &ValMap = Graph.getValueMap();
71 for (map<Value*, DSNodeHandle>::iterator I = ValMap.begin(), E = ValMap.end();
72 I != E; ++I)
73 if (GlobalValue* GV = dyn_cast<GlobalValue>(I->first)) {
74 map<Value*, DSNodeHandle>:: iterator NHI = OldValMap.find(GV);
75 if (NHI != OldValMap.end()) // was it inlined from the callee?
76 I->second->mergeWith(NHI->second);
77 else // get it from the GlobalsGraph
78 I->second->mergeWith(Graph.cloneGlobalInto(GV));
79 }
80
81 // Add unused inlined global nodes into the value map
Chris Lattner0d9bab82002-07-18 00:12:30 +000082 for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(),
83 E = OldValMap.end(); I != E; ++I)
84 if (isa<GlobalValue>(I->first)) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000085 DSNodeHandle &NH = ValMap[I->first]; // If global is not in ValMap...
86 if (NH == 0)
87 NH = I->second; // Add the one just inlined.
Chris Lattner0d9bab82002-07-18 00:12:30 +000088 }
89
90}
91
92DSGraph &BUDataStructures::calculateGraph(Function &F) {
93 // Make sure this graph has not already been calculated, or that we don't get
94 // into an infinite loop with mutually recursive functions.
95 //
96 DSGraph *&Graph = DSInfo[&F];
97 if (Graph) return *Graph;
98
99 // Copy the local version into DSInfo...
100 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
101
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000102 // Populate the GlobalsGraph with globals from this one.
103 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
104
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000105 // Save a copy of the original call nodes for the top-down pass
106 Graph->saveOrigFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000107
Chris Lattner0d9bab82002-07-18 00:12:30 +0000108 // Start resolving calls...
109 std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls();
110
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000111 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
112
113 // Add F to the PendingCallers list of each direct callee for use in the
114 // top-down pass so we don't have to compute this again. We don't want
115 // to do it for indirect callees inlined later, so remember which calls
116 // are in the original FCs set.
117 std::set<const DSNode*> directCallees;
118 for (unsigned i = 0; i < FCs.size(); ++i)
119 directCallees.insert(FCs[i][1]); // ptr to function node
Chris Lattner0d9bab82002-07-18 00:12:30 +0000120
121 bool Inlined;
122 do {
123 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000124
Chris Lattner0d9bab82002-07-18 00:12:30 +0000125 for (unsigned i = 0; i != FCs.size(); ++i) {
126 // Copy the call, because inlining graphs may invalidate the FCs vector.
127 std::vector<DSNodeHandle> Call = FCs[i];
128
129 // If the function list is not incomplete...
130 if ((Call[1]->NodeType & DSNode::Incomplete) == 0) {
131 // Start inlining all of the functions we can... some may not be
132 // inlinable if they are external...
133 //
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000134 std::vector<GlobalValue*> Callees(Call[1]->getGlobals());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000135
136 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000137 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000138 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000139 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000140 if (&FI == &F) {
141 // Self recursion... simply link up the formal arguments with the
142 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000143
144 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000145
146 if (Call[0]) // Handle the return value if present...
147 Graph->RetNode->mergeWith(Call[0]);
148
149 // Resolve the arguments in the call to the actual values...
150 ResolveArguments(Call, F, Graph->getValueMap());
151
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000152 // Erase the entry in the callees vector
153 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000154 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000155 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000156 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000157
Chris Lattner0d9bab82002-07-18 00:12:30 +0000158 // Get the data structure graph for the called function, closing it
159 // if possible (which is only impossible in the case of mutual
160 // recursion...
161 //
162 DSGraph &GI = calculateGraph(FI); // Graph to inline
163
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000164 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
165 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000166
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000167 // Clone the callee's graph into the current graph, keeping
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000168 // track of where scalars in the old graph _used_ to point,
169 // and of the new nodes matching nodes of the old graph.
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000170 std::map<Value*, DSNodeHandle> OldValMap;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000171 std::map<const DSNode*, DSNode*> OldNodeMap;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000172
173 // The clone call may invalidate any of the vectors in the data
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000174 // structure graph. Strip locals and don't copy the list of callers
175 DSNode *RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
176 /*StripScalars*/ true,
177 /*StripAllocas*/ true,
178 /*CopyCallers*/ false,
179 /*CopyOrigCalls*/ false);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000180
181 ResolveArguments(Call, FI, OldValMap);
182
Chris Lattnerd124c382002-07-18 01:58:24 +0000183 if (Call[0]) // Handle the return value if present
184 RetVal->mergeWith(Call[0]);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000185
Chris Lattner0d9bab82002-07-18 00:12:30 +0000186 // Merge global value nodes in the inlined graph with the global
187 // value nodes in the current graph if there are duplicates.
188 //
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000189 MergeGlobalNodes(*Graph, OldValMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000190
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000191 // If this was an original call, add F to the PendingCallers list
192 if (directCallees.find(Call[1]) != directCallees.end())
193 GI.addCaller(F);
194
195 // Erase the entry in the Callees vector
196 Callees.erase(Callees.begin()+c--);
197
Chris Lattner9eee58d2002-07-19 18:11:43 +0000198 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
199 FI.getName() == "fprintf" || FI.getName() == "open" ||
200 FI.getName() == "sprintf") {
201
202 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000203 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000204 }
205 }
206
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000207 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000208 // Erase the call if it is resolvable...
209 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
210 Inlined = true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000211 } else if (Callees.size() != Call[1]->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000212 // Was able to inline SOME, but not all of the functions. Construct a
213 // new global node here.
214 //
215 assert(0 && "Unimpl!");
216 Inlined = true;
217 }
218 }
219 }
220
221 // Recompute the Incomplete markers. If there are any function calls left
222 // now that are complete, we must loop!
223 if (Inlined) {
224 Graph->maskIncompleteMarkers();
225 Graph->markIncompleteNodes();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000226 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000227 }
228 } while (Inlined && !FCs.empty());
229
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000230 // Copy any unresolved call nodes into the Globals graph and
231 // filter out unresolved call nodes inlined from the callee.
232 if (!FCs.empty())
233 Graph->GlobalsGraph->cloneCalls(*Graph);
234
235 Graph->maskIncompleteMarkers();
236 Graph->markIncompleteNodes();
237 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
238
Chris Lattner221c9792002-08-07 21:41:11 +0000239 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
240 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
241 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000242
Chris Lattner0d9bab82002-07-18 00:12:30 +0000243 return *Graph;
244}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000245#endif