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");
20 namespace DataStructureAnalysis {
21 // isPointerType - Return true if this first class type is big enough to hold
24 bool isPointerType(const Type *Ty);
26 using namespace DataStructureAnalysis;
29 // releaseMemory - If the pass pipeline is done with this pass, we can release
30 // our memory... here...
32 void BUDataStructures::releaseMemory() {
33 // Delete all call site information
36 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
37 E = DSInfo.end(); I != E; ++I)
40 // Empty map so next time memory is released, data structures are not
45 // run - Calculate the bottom up data structure graphs for each function in the
48 bool BUDataStructures::run(Module &M) {
49 // Simply calculate the graphs for each function...
50 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
56 // ResolveArguments - Resolve the formal and actual arguments for a function
59 static void ResolveArguments(DSCallSite &Call, Function &F,
60 map<Value*, DSNodeHandle> &ValueMap) {
61 // Resolve all of the function arguments...
62 Function::aiterator AI = F.abegin();
63 for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i) {
64 // Advance the argument iterator to the first pointer argument...
65 while (!isPointerType(AI->getType())) ++AI;
67 // Add the link from the argument scalar to the provided value
68 DSNodeHandle &NN = ValueMap[AI];
69 NN.addEdgeTo(Call.getPtrArgNode(i));
74 DSGraph &BUDataStructures::calculateGraph(Function &F) {
75 // Make sure this graph has not already been calculated, or that we don't get
76 // into an infinite loop with mutually recursive functions.
78 DSGraph *&Graph = DSInfo[&F];
79 if (Graph) return *Graph;
81 // Copy the local version into DSInfo...
82 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
85 // Populate the GlobalsGraph with globals from this one.
86 Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
89 // Start resolving calls...
90 std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
92 DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
98 for (unsigned i = 0; i != FCs.size(); ++i) {
99 // Copy the call, because inlining graphs may invalidate the FCs vector.
100 DSCallSite Call = FCs[i];
102 // If the function list is complete...
103 if ((Call.getCalleeNode().getNode()->NodeType & DSNode::Incomplete)==0) {
104 // Start inlining all of the functions we can... some may not be
105 // inlinable if they are external...
107 std::vector<GlobalValue*> Callees =
108 Call.getCalleeNode().getNode()->getGlobals();
110 // Loop over the functions, inlining whatever we can...
111 for (unsigned c = 0; c != Callees.size(); ++c) {
112 // Must be a function type, so this cast MUST succeed.
113 Function &FI = cast<Function>(*Callees[c]);
116 // Self recursion... simply link up the formal arguments with the
117 // actual arguments...
118 DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
120 // Handle the return value if present...
121 if (Call.getReturnValueNode().getNode())
122 Graph->getRetNode().mergeWith(Call.getReturnValueNode());
124 // Resolve the arguments in the call to the actual values...
125 ResolveArguments(Call, F, Graph->getValueMap());
127 // Erase the entry in the callees vector
128 Callees.erase(Callees.begin()+c--);
130 } else if (!FI.isExternal()) {
131 DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
132 << FI.getName() << "\n");
134 // Get the data structure graph for the called function, closing it
135 // if possible (which is only impossible in the case of mutual
138 DSGraph &GI = calculateGraph(FI); // Graph to inline
140 DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
141 << " in: " << F.getName() << "\n");
143 // Record that the original DSCallSite was a call site of FI.
144 // This may or may not have been known when the DSCallSite was
145 // originally created.
146 CallSites[&FI].push_back(Call);
148 // Clone the callee's graph into the current graph, keeping
149 // track of where scalars in the old graph _used_ to point,
150 // and of the new nodes matching nodes of the old graph.
151 map<Value*, DSNodeHandle> OldValMap;
152 map<const DSNode*, DSNode*> OldNodeMap;
154 // The clone call may invalidate any of the vectors in the data
155 // structure graph. Strip locals and don't copy the list of callers
156 DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
157 /*StripScalars*/ true,
158 /*StripAllocas*/ true,
159 /*CopyCallers*/ false,
160 /*CopyOrigCalls*/ false);
162 // Resolve the arguments in the call to the actual values...
163 ResolveArguments(Call, FI, OldValMap);
165 if (Call.getReturnValueNode().getNode()) // Handle the return value if present
166 RetVal.mergeWith(Call.getReturnValueNode());
168 // Erase the entry in the Callees vector
169 Callees.erase(Callees.begin()+c--);
171 } else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
172 FI.getName() == "fprintf" || FI.getName() == "open" ||
173 FI.getName() == "sprintf") {
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() != Call.getCalleeNode().getNode()->getGlobals().size()) {
185 // Was able to inline SOME, but not all of the functions. Construct a
186 // new global node here.
188 assert(0 && "Unimpl!");
194 // Recompute the Incomplete markers. If there are any function calls left
195 // now that are complete, we must loop!
197 Graph->maskIncompleteMarkers();
198 Graph->markIncompleteNodes();
199 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
201 } while (Inlined && !FCs.empty());
203 Graph->maskIncompleteMarkers();
204 Graph->markIncompleteNodes();
205 Graph->removeTriviallyDeadNodes(false);
206 Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
208 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
209 << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()