Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 1 | //===- 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 14 | #include <set> |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 15 | using std::map; |
| 16 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 17 | static RegisterAnalysis<BUDataStructures> |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 18 | X("budatastructure", "Bottom-up Data Structure Analysis Closure"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 19 | |
| 20 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 21 | // our memory... here... |
| 22 | // |
| 23 | void BUDataStructures::releaseMemory() { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 24 | for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 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 | // |
| 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) |
| 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 | // |
| 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; |
| 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 63 | // MergeGlobalNodes - Merge all existing global nodes with globals |
| 64 | // inlined from the callee or with globals from the GlobalsGraph. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 65 | // |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 66 | static void MergeGlobalNodes(DSGraph& Graph, |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 67 | map<Value*, DSNodeHandle> &OldValMap) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 68 | 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 Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 80 | for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(), |
| 81 | E = OldValMap.end(); I != E; ++I) |
| 82 | if (isa<GlobalValue>(I->first)) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 83 | 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 Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | DSGraph &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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 100 | // Populate the GlobalsGraph with globals from this one. |
| 101 | Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false); |
| 102 | |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 103 | // Save a copy of the original call nodes for the top-down pass |
| 104 | Graph->saveOrigFunctionCalls(); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 106 | // Start resolving calls... |
| 107 | std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls(); |
| 108 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 109 | 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 Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 118 | |
| 119 | bool Inlined; |
| 120 | do { |
| 121 | Inlined = false; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 123 | 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 132 | std::vector<GlobalValue*> Callees(Call[1]->getGlobals()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 133 | |
| 134 | // Loop over the functions, inlining whatever we can... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 135 | for (unsigned c = 0; c != Callees.size(); ++c) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 136 | // Must be a function type, so this cast MUST succeed. |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 137 | Function &FI = cast<Function>(*Callees[c]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 138 | if (&FI == &F) { |
| 139 | // Self recursion... simply link up the formal arguments with the |
| 140 | // actual arguments... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 141 | |
| 142 | DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 143 | |
| 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 150 | // Erase the entry in the callees vector |
| 151 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 152 | } else if (!FI.isExternal()) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 153 | DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: " |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 154 | << FI.getName() << "\n"); |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 156 | // 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 162 | DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName() |
| 163 | << " in: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 164 | |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 165 | // Clone the callee's graph into the current graph, keeping |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 166 | // 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. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 168 | std::map<Value*, DSNodeHandle> OldValMap; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 169 | std::map<const DSNode*, DSNode*> OldNodeMap; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 170 | |
| 171 | // The clone call may invalidate any of the vectors in the data |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 172 | // 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 Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 178 | |
| 179 | ResolveArguments(Call, FI, OldValMap); |
| 180 | |
Chris Lattner | d124c38 | 2002-07-18 01:58:24 +0000 | [diff] [blame] | 181 | if (Call[0]) // Handle the return value if present |
| 182 | RetVal->mergeWith(Call[0]); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 184 | // 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 187 | MergeGlobalNodes(*Graph, OldValMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 188 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 189 | // 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 Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 196 | } 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 201 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 205 | if (Callees.empty()) { // Inlined all of the function calls? |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 206 | // 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 209 | } else if (Callees.size() != Call[1]->getGlobals().size()) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 210 | // 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. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 224 | Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ true); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 225 | } |
| 226 | } while (Inlined && !FCs.empty()); |
| 227 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 228 | // 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 Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 237 | DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " [" |
| 238 | << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size() |
| 239 | << "]\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 241 | return *Graph; |
| 242 | } |