1 //===- BottomUpClosure.cpp - Compute the bottom up interprocedure closure -===//
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.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/DataStructure.h"
11 #include "llvm/Module.h"
12 #include "llvm/DerivedTypes.h"
13 #include "Support/StatisticReporter.h"
16 static RegisterAnalysis<BUDataStructures>
17 X("budatastructure", "Bottom-Up Data Structure Analysis Closure");
18 AnalysisID BUDataStructures::ID(AnalysisID::create<BUDataStructures>());
20 // releaseMemory - If the pass pipeline is done with this pass, we can release
21 // our memory... here...
23 void BUDataStructures::releaseMemory() {
24 for (map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
25 E = DSInfo.end(); I != E; ++I)
28 // Empty map so next time memory is released, data structures are not
33 // run - Calculate the bottom up data structure graphs for each function in the
36 bool 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)
45 // ResolveArguments - Resolve the formal and actual arguments for a function
48 static 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;
56 // Add the link from the argument scalar to the provided value
57 DSNode *NN = ValueMap[AI];
58 NN->addEdgeTo(Call[i]);
63 // MergeGlobalNodes - Merge global value nodes in the inlined graph with the
64 // global value nodes in the current graph if there are duplicates.
66 static 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.
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...
78 NH->mergeWith(I->second); // Merge the two globals together.
84 DSGraph &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.
88 DSGraph *&Graph = DSInfo[&F];
89 if (Graph) return *Graph;
91 // Copy the local version into DSInfo...
92 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
94 // Save a copy of the original call nodes for the top-down pass
95 Graph->saveOrigFunctionCalls();
97 // Start resolving calls...
98 std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls();
100 DEBUG(std::cerr << "Inlining: " << F.getName() << "\n");
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];
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...
114 std::vector<GlobalValue*> Globals(Call[1]->getGlobals());
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]);
121 // Self recursion... simply link up the formal arguments with the
122 // actual arguments...
124 DEBUG(std::cerr << "Self Inlining: " << F.getName() << "\n");
126 if (Call[0]) // Handle the return value if present...
127 Graph->RetNode->mergeWith(Call[0]);
129 // Resolve the arguments in the call to the actual values...
130 ResolveArguments(Call, F, Graph->getValueMap());
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");
138 // Get the data structure graph for the called function, closing it
139 // if possible (which is only impossible in the case of mutual
142 DSGraph &GI = calculateGraph(FI); // Graph to inline
144 DEBUG(std::cerr << "Got graph for " << FI.getName() << " in: "
145 << F.getName() << "\n");
147 // Remember the callers for each callee for use in the top-down
148 // pass so we don't have to compute this again
151 // 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
157 // The clone call may invalidate any of the vectors in the data
159 DSNode *RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap);
161 ResolveArguments(Call, FI, OldValMap);
163 if (Call[0]) // Handle the return value if present
164 RetVal->mergeWith(Call[0]);
166 // Merge global value nodes in the inlined graph with the global
167 // value nodes in the current graph if there are duplicates.
169 MergeGlobalNodes(Graph->getValueMap(), OldValMap);
171 // Erase the entry in the globals vector
172 Globals.erase(Globals.begin()+g--);
173 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
174 FI.getName() == "fprintf" || FI.getName() == "open" ||
175 FI.getName() == "sprintf") {
177 // Erase the entry in the globals vector
178 Globals.erase(Globals.begin()+g--);
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...
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.
190 assert(0 && "Unimpl!");
196 // Recompute the Incomplete markers. If there are any function calls left
197 // now that are complete, we must loop!
199 Graph->maskIncompleteMarkers();
200 Graph->markIncompleteNodes();
201 Graph->removeDeadNodes();
203 } while (Inlined && !FCs.empty());