1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural 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 alias analysis.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/DataStructure.h"
11 #include "llvm/Analysis/DSGraph.h"
12 #include "llvm/Module.h"
13 #include "Support/Statistic.h"
16 static RegisterAnalysis<BUDataStructures>
17 X("budatastructure", "Bottom-up Data Structure Analysis Closure");
19 namespace DataStructureAnalysis { // TODO: FIXME: Eliminate
20 // isPointerType - Return true if this first class type is big enough to hold
23 bool isPointerType(const Type *Ty);
25 using namespace DataStructureAnalysis;
28 // releaseMemory - If the pass pipeline is done with this pass, we can release
29 // our memory... here...
31 void BUDataStructures::releaseMemory() {
32 // Delete all call site information
35 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
36 E = DSInfo.end(); I != E; ++I)
39 // Empty map so next time memory is released, data structures are not
44 // run - Calculate the bottom up data structure graphs for each function in the
47 bool 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)
55 // ResolveArguments - Resolve the formal and actual arguments for a function
58 static void ResolveArguments(DSCallSite &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 = 0, e = Call.getNumPtrArgs(); i != e; ++i, ++AI) {
63 // Advance the argument iterator to the first pointer argument...
64 while (!isPointerType(AI->getType())) ++AI;
66 // Add the link from the argument scalar to the provided value
67 ValueMap[AI].mergeWith(Call.getPtrArg(i));
71 DSGraph &BUDataStructures::calculateGraph(Function &F) {
72 // Make sure this graph has not already been calculated, or that we don't get
73 // into an infinite loop with mutually recursive functions.
75 DSGraph *&Graph = DSInfo[&F];
76 if (Graph) return *Graph;
78 // Copy the local version into DSInfo...
79 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
82 // Populate the GlobalsGraph with globals from this one.
83 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
86 // Start resolving calls...
87 std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
89 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
95 for (unsigned i = 0; i != FCs.size(); ++i) {
96 // Copy the call, because inlining graphs may invalidate the FCs vector.
97 DSCallSite Call = FCs[i];
99 // If the function list is complete...
100 if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) {
101 // Start inlining all of the functions we can... some may not be
102 // inlinable if they are external...
104 std::vector<GlobalValue*> Callees =
105 Call.getCallee().getNode()->getGlobals();
107 // Loop over the functions, inlining whatever we can...
108 for (unsigned c = 0; c != Callees.size(); ++c) {
109 // Must be a function type, so this cast MUST succeed.
110 Function &FI = cast<Function>(*Callees[c]);
113 // Self recursion... simply link up the formal arguments with the
114 // actual arguments...
115 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
117 // Handle the return value if present...
118 Graph->getRetNode().mergeWith(Call.getRetVal());
120 // Resolve the arguments in the call to the actual values...
121 ResolveArguments(Call, F, Graph->getValueMap());
123 // Erase the entry in the callees vector
124 Callees.erase(Callees.begin()+c--);
126 } else if (!FI.isExternal()) {
127 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
128 << FI.getName() << "\n");
130 // Get the data structure graph for the called function, closing it
131 // if possible (which is only impossible in the case of mutual
134 DSGraph &GI = calculateGraph(FI); // Graph to inline
136 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
137 << " in: " << F.getName() << "\n");
139 // Record that the original DSCallSite was a call site of FI.
140 // This may or may not have been known when the DSCallSite was
141 // originally created.
142 std::vector<DSCallSite> &CallSitesForFunc = CallSites[&FI];
143 CallSitesForFunc.push_back(Call);
144 CallSitesForFunc.back().setResolvingCaller(&F);
145 CallSitesForFunc.back().setCallee(0);
147 // Clone the callee's graph into the current graph, keeping
148 // track of where scalars in the old graph _used_ to point,
149 // and of the new nodes matching nodes of the old graph.
150 map<Value*, DSNodeHandle> OldValMap;
151 map<const DSNode*, DSNode*> OldNodeMap;
153 // The clone call may invalidate any of the vectors in the data
154 // structure graph. Strip locals and don't copy the list of callers
155 DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
156 /*StripScalars*/ true,
157 /*StripAllocas*/ true);
159 // Resolve the arguments in the call to the actual values...
160 ResolveArguments(Call, FI, OldValMap);
162 // Handle the return value if present...
163 RetVal.mergeWith(Call.getRetVal());
165 // Erase the entry in the Callees vector
166 Callees.erase(Callees.begin()+c--);
168 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
169 FI.getName() == "fprintf" || FI.getName() == "open" ||
170 FI.getName() == "sprintf") {
171 // FIXME: These special cases (eg printf) should go away when we can
172 // define functions that take a variable number of arguments.
174 // FIXME: at the very least, this should update mod/ref info
175 // Erase the entry in the globals vector
176 Callees.erase(Callees.begin()+c--);
180 if (Callees.empty()) { // Inlined all of the function calls?
181 // Erase the call if it is resolvable...
182 FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
184 } else if (Callees.size() !=
185 Call.getCallee().getNode()->getGlobals().size()) {
186 // Was able to inline SOME, but not all of the functions. Construct a
187 // new global node here.
189 assert(0 && "Unimpl!");
195 // Recompute the Incomplete markers. If there are any function calls left
196 // now that are complete, we must loop!
198 Graph->maskIncompleteMarkers();
199 Graph->markIncompleteNodes();
200 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
202 } while (Inlined && !FCs.empty());
204 Graph->maskIncompleteMarkers();
205 Graph->markIncompleteNodes();
206 Graph->removeTriviallyDeadNodes(false);
207 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
209 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
210 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()