blob: 2ab97442d18a62d77ba3009c593dda17106381e8 [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"
13#include "Support/StatisticReporter.h"
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000014#include <set>
Chris Lattner0d9bab82002-07-18 00:12:30 +000015using std::map;
16
Chris Lattner1e435162002-07-26 21:12:44 +000017static RegisterAnalysis<BUDataStructures>
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000018X("budatastructure", "Bottom-up Data Structure Analysis Closure");
Chris Lattner0d9bab82002-07-18 00:12:30 +000019
20// releaseMemory - If the pass pipeline is done with this pass, we can release
21// our memory... here...
22//
23void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000024 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +000025 E = DSInfo.end(); I != E; ++I)
26 delete I->second;
27
28 // Empty map so next time memory is released, data structures are not
29 // re-deleted.
30 DSInfo.clear();
31}
32
33// run - Calculate the bottom up data structure graphs for each function in the
34// program.
35//
36bool BUDataStructures::run(Module &M) {
37 // Simply calculate the graphs for each function...
38 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
39 if (!I->isExternal())
40 calculateGraph(*I);
41 return false;
42}
43
44
45// ResolveArguments - Resolve the formal and actual arguments for a function
46// call.
47//
48static void ResolveArguments(std::vector<DSNodeHandle> &Call, Function &F,
49 map<Value*, DSNodeHandle> &ValueMap) {
50 // Resolve all of the function arguments...
51 Function::aiterator AI = F.abegin();
52 for (unsigned i = 2, e = Call.size(); i != e; ++i) {
53 // Advance the argument iterator to the first pointer argument...
54 while (!isa<PointerType>(AI->getType())) ++AI;
55
56 // Add the link from the argument scalar to the provided value
57 DSNode *NN = ValueMap[AI];
58 NN->addEdgeTo(Call[i]);
59 ++AI;
60 }
61}
62
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000063// MergeGlobalNodes - Merge all existing global nodes with globals
64// inlined from the callee or with globals from the GlobalsGraph.
Chris Lattner0d9bab82002-07-18 00:12:30 +000065//
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000066static void MergeGlobalNodes(DSGraph& Graph,
Chris Lattner0d9bab82002-07-18 00:12:30 +000067 map<Value*, DSNodeHandle> &OldValMap) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000068 map<Value*, DSNodeHandle> &ValMap = Graph.getValueMap();
69 for (map<Value*, DSNodeHandle>::iterator I = ValMap.begin(), E = ValMap.end();
70 I != E; ++I)
71 if (GlobalValue* GV = dyn_cast<GlobalValue>(I->first)) {
72 map<Value*, DSNodeHandle>:: iterator NHI = OldValMap.find(GV);
73 if (NHI != OldValMap.end()) // was it inlined from the callee?
74 I->second->mergeWith(NHI->second);
75 else // get it from the GlobalsGraph
76 I->second->mergeWith(Graph.cloneGlobalInto(GV));
77 }
78
79 // Add unused inlined global nodes into the value map
Chris Lattner0d9bab82002-07-18 00:12:30 +000080 for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(),
81 E = OldValMap.end(); I != E; ++I)
82 if (isa<GlobalValue>(I->first)) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000083 DSNodeHandle &NH = ValMap[I->first]; // If global is not in ValMap...
84 if (NH == 0)
85 NH = I->second; // Add the one just inlined.
Chris Lattner0d9bab82002-07-18 00:12:30 +000086 }
87
88}
89
90DSGraph &BUDataStructures::calculateGraph(Function &F) {
91 // Make sure this graph has not already been calculated, or that we don't get
92 // into an infinite loop with mutually recursive functions.
93 //
94 DSGraph *&Graph = DSInfo[&F];
95 if (Graph) return *Graph;
96
97 // Copy the local version into DSInfo...
98 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
99
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000100 // Populate the GlobalsGraph with globals from this one.
101 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
102
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000103 // Save a copy of the original call nodes for the top-down pass
104 Graph->saveOrigFunctionCalls();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000105
Chris Lattner0d9bab82002-07-18 00:12:30 +0000106 // Start resolving calls...
107 std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls();
108
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000109 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
110
111 // Add F to the PendingCallers list of each direct callee for use in the
112 // top-down pass so we don't have to compute this again. We don't want
113 // to do it for indirect callees inlined later, so remember which calls
114 // are in the original FCs set.
115 std::set<const DSNode*> directCallees;
116 for (unsigned i = 0; i < FCs.size(); ++i)
117 directCallees.insert(FCs[i][1]); // ptr to function node
Chris Lattner0d9bab82002-07-18 00:12:30 +0000118
119 bool Inlined;
120 do {
121 Inlined = false;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000122
Chris Lattner0d9bab82002-07-18 00:12:30 +0000123 for (unsigned i = 0; i != FCs.size(); ++i) {
124 // Copy the call, because inlining graphs may invalidate the FCs vector.
125 std::vector<DSNodeHandle> Call = FCs[i];
126
127 // If the function list is not incomplete...
128 if ((Call[1]->NodeType & DSNode::Incomplete) == 0) {
129 // Start inlining all of the functions we can... some may not be
130 // inlinable if they are external...
131 //
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000132 std::vector<GlobalValue*> Callees(Call[1]->getGlobals());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000133
134 // Loop over the functions, inlining whatever we can...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000135 for (unsigned c = 0; c != Callees.size(); ++c) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000136 // Must be a function type, so this cast MUST succeed.
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000137 Function &FI = cast<Function>(*Callees[c]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000138 if (&FI == &F) {
139 // Self recursion... simply link up the formal arguments with the
140 // actual arguments...
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000141
142 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000143
144 if (Call[0]) // Handle the return value if present...
145 Graph->RetNode->mergeWith(Call[0]);
146
147 // Resolve the arguments in the call to the actual values...
148 ResolveArguments(Call, F, Graph->getValueMap());
149
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000150 // Erase the entry in the callees vector
151 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000152 } else if (!FI.isExternal()) {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000153 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000154 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000155
Chris Lattner0d9bab82002-07-18 00:12:30 +0000156 // Get the data structure graph for the called function, closing it
157 // if possible (which is only impossible in the case of mutual
158 // recursion...
159 //
160 DSGraph &GI = calculateGraph(FI); // Graph to inline
161
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000162 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
163 << " in: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000165 // Clone the callee's graph into the current graph, keeping
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000166 // track of where scalars in the old graph _used_ to point,
167 // and of the new nodes matching nodes of the old graph.
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000168 std::map<Value*, DSNodeHandle> OldValMap;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000169 std::map<const DSNode*, DSNode*> OldNodeMap;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000170
171 // The clone call may invalidate any of the vectors in the data
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000172 // structure graph. Strip locals and don't copy the list of callers
173 DSNode *RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
174 /*StripScalars*/ true,
175 /*StripAllocas*/ true,
176 /*CopyCallers*/ false,
177 /*CopyOrigCalls*/ false);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000178
179 ResolveArguments(Call, FI, OldValMap);
180
Chris Lattnerd124c382002-07-18 01:58:24 +0000181 if (Call[0]) // Handle the return value if present
182 RetVal->mergeWith(Call[0]);
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000183
Chris Lattner0d9bab82002-07-18 00:12:30 +0000184 // Merge global value nodes in the inlined graph with the global
185 // value nodes in the current graph if there are duplicates.
186 //
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000187 MergeGlobalNodes(*Graph, OldValMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000188
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000189 // If this was an original call, add F to the PendingCallers list
190 if (directCallees.find(Call[1]) != directCallees.end())
191 GI.addCaller(F);
192
193 // Erase the entry in the Callees vector
194 Callees.erase(Callees.begin()+c--);
195
Chris Lattner9eee58d2002-07-19 18:11:43 +0000196 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
197 FI.getName() == "fprintf" || FI.getName() == "open" ||
198 FI.getName() == "sprintf") {
199
200 // Erase the entry in the globals vector
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000201 Callees.erase(Callees.begin()+c--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000202 }
203 }
204
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000205 if (Callees.empty()) { // Inlined all of the function calls?
Chris Lattner0d9bab82002-07-18 00:12:30 +0000206 // Erase the call if it is resolvable...
207 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
208 Inlined = true;
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000209 } else if (Callees.size() != Call[1]->getGlobals().size()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000210 // Was able to inline SOME, but not all of the functions. Construct a
211 // new global node here.
212 //
213 assert(0 && "Unimpl!");
214 Inlined = true;
215 }
216 }
217 }
218
219 // Recompute the Incomplete markers. If there are any function calls left
220 // now that are complete, we must loop!
221 if (Inlined) {
222 Graph->maskIncompleteMarkers();
223 Graph->markIncompleteNodes();
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000224 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ true);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000225 }
226 } while (Inlined && !FCs.empty());
227
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000228 // Copy any unresolved call nodes into the Globals graph and
229 // filter out unresolved call nodes inlined from the callee.
230 if (!FCs.empty())
231 Graph->GlobalsGraph->cloneCalls(*Graph);
232
233 Graph->maskIncompleteMarkers();
234 Graph->markIncompleteNodes();
235 Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
236
Chris Lattner221c9792002-08-07 21:41:11 +0000237 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
238 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
239 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000240
Chris Lattner0d9bab82002-07-18 00:12:30 +0000241 return *Graph;
242}