blob: c8b45b50ccd6bacb653d1b35dfa47204282869a5 [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"
14using std::map;
15
Chris Lattner1e435162002-07-26 21:12:44 +000016static RegisterAnalysis<BUDataStructures>
17X("budatastructure", "Bottom-Up Data Structure Analysis Closure");
Chris Lattner97f51a32002-07-27 01:12:15 +000018AnalysisID BUDataStructures::ID = X;
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() {
24 for (map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
25 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
63// MergeGlobalNodes - Merge global value nodes in the inlined graph with the
64// global value nodes in the current graph if there are duplicates.
65//
66static void MergeGlobalNodes(map<Value*, DSNodeHandle> &ValMap,
67 map<Value*, DSNodeHandle> &OldValMap) {
68 // Loop over all of the nodes inlined, if any of them are global variable
69 // nodes, we must make sure they get properly added or merged with the ValMap.
70 //
71 for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(),
72 E = OldValMap.end(); I != E; ++I)
73 if (isa<GlobalValue>(I->first)) {
74 DSNodeHandle &NH = ValMap[I->first]; // Look up global in ValMap.
75 if (NH == 0) { // No entry for the global yet?
76 NH = I->second; // Add the one just inlined...
77 } else {
78 NH->mergeWith(I->second); // Merge the two globals together.
79 }
80 }
81
82}
83
84DSGraph &BUDataStructures::calculateGraph(Function &F) {
85 // Make sure this graph has not already been calculated, or that we don't get
86 // into an infinite loop with mutually recursive functions.
87 //
88 DSGraph *&Graph = DSInfo[&F];
89 if (Graph) return *Graph;
90
91 // Copy the local version into DSInfo...
92 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
93
Vikram S. Advec44e9bf2002-07-18 16:13:52 +000094 // Save a copy of the original call nodes for the top-down pass
95 Graph->saveOrigFunctionCalls();
96
Chris Lattner0d9bab82002-07-18 00:12:30 +000097 // Start resolving calls...
98 std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls();
99
Chris Lattner18682272002-07-24 22:33:50 +0000100 DEBUG(std::cerr << "Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000101
102 bool Inlined;
103 do {
104 Inlined = false;
105 for (unsigned i = 0; i != FCs.size(); ++i) {
106 // Copy the call, because inlining graphs may invalidate the FCs vector.
107 std::vector<DSNodeHandle> Call = FCs[i];
108
109 // If the function list is not incomplete...
110 if ((Call[1]->NodeType & DSNode::Incomplete) == 0) {
111 // Start inlining all of the functions we can... some may not be
112 // inlinable if they are external...
113 //
114 std::vector<GlobalValue*> Globals(Call[1]->getGlobals());
115
116 // Loop over the functions, inlining whatever we can...
117 for (unsigned g = 0; g != Globals.size(); ++g) {
118 // Must be a function type, so this cast MUST succeed.
119 Function &FI = cast<Function>(*Globals[g]);
120 if (&FI == &F) {
121 // Self recursion... simply link up the formal arguments with the
122 // actual arguments...
123
Chris Lattner18682272002-07-24 22:33:50 +0000124 DEBUG(std::cerr << "Self Inlining: " << F.getName() << "\n");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000125
126 if (Call[0]) // Handle the return value if present...
127 Graph->RetNode->mergeWith(Call[0]);
128
129 // Resolve the arguments in the call to the actual values...
130 ResolveArguments(Call, F, Graph->getValueMap());
131
132 // Erase the entry in the globals vector
133 Globals.erase(Globals.begin()+g--);
134 } else if (!FI.isExternal()) {
135 DEBUG(std::cerr << "In " << F.getName() << " inlining: "
136 << FI.getName() << "\n");
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000137
Chris Lattner0d9bab82002-07-18 00:12:30 +0000138 // Get the data structure graph for the called function, closing it
139 // if possible (which is only impossible in the case of mutual
140 // recursion...
141 //
142 DSGraph &GI = calculateGraph(FI); // Graph to inline
143
Chris Lattner18682272002-07-24 22:33:50 +0000144 DEBUG(std::cerr << "Got graph for " << FI.getName() << " in: "
Chris Lattner0d9bab82002-07-18 00:12:30 +0000145 << F.getName() << "\n");
146
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000147 // Remember the callers for each callee for use in the top-down
148 // pass so we don't have to compute this again
149 GI.addCaller(F);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000150
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000151 // Clone the callee's graph into the current graph, keeping
152 // track of where scalars in the old graph _used_ to point
153 // and of the new nodes matching nodes of the old graph ...
154 std::map<Value*, DSNodeHandle> OldValMap;
155 std::map<const DSNode*, DSNode*> OldNodeMap; // unused
Chris Lattner0d9bab82002-07-18 00:12:30 +0000156
157 // The clone call may invalidate any of the vectors in the data
158 // structure graph.
Vikram S. Advec44e9bf2002-07-18 16:13:52 +0000159 DSNode *RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000160
161 ResolveArguments(Call, FI, OldValMap);
162
Chris Lattnerd124c382002-07-18 01:58:24 +0000163 if (Call[0]) // Handle the return value if present
164 RetVal->mergeWith(Call[0]);
165
Chris Lattner0d9bab82002-07-18 00:12:30 +0000166 // Merge global value nodes in the inlined graph with the global
167 // value nodes in the current graph if there are duplicates.
168 //
169 MergeGlobalNodes(Graph->getValueMap(), OldValMap);
170
171 // Erase the entry in the globals vector
172 Globals.erase(Globals.begin()+g--);
Chris Lattner9eee58d2002-07-19 18:11:43 +0000173 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
174 FI.getName() == "fprintf" || FI.getName() == "open" ||
175 FI.getName() == "sprintf") {
176
177 // Erase the entry in the globals vector
178 Globals.erase(Globals.begin()+g--);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000179 }
180 }
181
182 if (Globals.empty()) { // Inlined all of the function calls?
183 // Erase the call if it is resolvable...
184 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
185 Inlined = true;
186 } else if (Globals.size() != Call[1]->getGlobals().size()) {
187 // Was able to inline SOME, but not all of the functions. Construct a
188 // new global node here.
189 //
190 assert(0 && "Unimpl!");
191 Inlined = true;
192 }
193 }
194 }
195
196 // Recompute the Incomplete markers. If there are any function calls left
197 // now that are complete, we must loop!
198 if (Inlined) {
199 Graph->maskIncompleteMarkers();
200 Graph->markIncompleteNodes();
201 Graph->removeDeadNodes();
202 }
203 } while (Inlined && !FCs.empty());
204
205 return *Graph;
206}