Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 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** |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 6 | // applications like alias analysis. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 11 | #include "llvm/Analysis/DSGraph.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 13 | //#include "llvm/DerivedTypes.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 14 | #include "Support/Statistic.h" |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 15 | //#include <set> |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 16 | using std::map; |
| 17 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 18 | static RegisterAnalysis<BUDataStructures> |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 19 | X("budatastructure", "Bottom-up Data Structure Analysis Closure"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 21 | // TODO: FIXME |
| 22 | namespace DataStructureAnalysis { |
| 23 | // isPointerType - Return true if this first class type is big enough to hold |
| 24 | // a pointer. |
| 25 | // |
| 26 | bool isPointerType(const Type *Ty); |
| 27 | } |
| 28 | using namespace DataStructureAnalysis; |
| 29 | |
| 30 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 31 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 32 | // our memory... here... |
| 33 | // |
| 34 | void BUDataStructures::releaseMemory() { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 35 | for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 36 | E = DSInfo.end(); I != E; ++I) |
| 37 | delete I->second; |
| 38 | |
| 39 | // Empty map so next time memory is released, data structures are not |
| 40 | // re-deleted. |
| 41 | DSInfo.clear(); |
| 42 | } |
| 43 | |
| 44 | // run - Calculate the bottom up data structure graphs for each function in the |
| 45 | // program. |
| 46 | // |
| 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) |
| 50 | if (!I->isExternal()) |
| 51 | calculateGraph(*I); |
| 52 | return false; |
| 53 | } |
| 54 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 55 | // ResolveArguments - Resolve the formal and actual arguments for a function |
| 56 | // call. |
| 57 | // |
| 58 | static void ResolveArguments(std::vector<DSNodeHandle> &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 = 2, e = Call.size(); i != e; ++i) { |
| 63 | // Advance the argument iterator to the first pointer argument... |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 64 | while (!isPointerType(AI->getType())) ++AI; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 65 | |
| 66 | // Add the link from the argument scalar to the provided value |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 67 | DSNodeHandle &NN = ValueMap[AI]; |
| 68 | NN.addEdgeTo(Call[i]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 69 | ++AI; |
| 70 | } |
| 71 | } |
| 72 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 73 | // MergeGlobalNodes - Merge all existing global nodes with globals |
| 74 | // inlined from the callee or with globals from the GlobalsGraph. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 75 | // |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 76 | static void MergeGlobalNodes(DSGraph &Graph, |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 77 | map<Value*, DSNodeHandle> &OldValMap) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 78 | map<Value*, DSNodeHandle> &ValMap = Graph.getValueMap(); |
| 79 | for (map<Value*, DSNodeHandle>::iterator I = ValMap.begin(), E = ValMap.end(); |
| 80 | I != E; ++I) |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 81 | if (GlobalValue *GV = dyn_cast<GlobalValue>(I->first)) { |
| 82 | map<Value*, DSNodeHandle>::iterator NHI = OldValMap.find(GV); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 83 | if (NHI != OldValMap.end()) // was it inlined from the callee? |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 84 | I->second.mergeWith(NHI->second); |
| 85 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 86 | else // get it from the GlobalsGraph |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 87 | I->second.mergeWith(Graph.cloneGlobalInto(GV)); |
| 88 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Add unused inlined global nodes into the value map |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 92 | for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(), |
| 93 | E = OldValMap.end(); I != E; ++I) |
| 94 | if (isa<GlobalValue>(I->first)) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 95 | DSNodeHandle &NH = ValMap[I->first]; // If global is not in ValMap... |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 96 | if (NH.getNode() == 0) |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 97 | NH = I->second; // Add the one just inlined. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | DSGraph &BUDataStructures::calculateGraph(Function &F) { |
| 103 | // Make sure this graph has not already been calculated, or that we don't get |
| 104 | // into an infinite loop with mutually recursive functions. |
| 105 | // |
| 106 | DSGraph *&Graph = DSInfo[&F]; |
| 107 | if (Graph) return *Graph; |
| 108 | |
| 109 | // Copy the local version into DSInfo... |
| 110 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F)); |
| 111 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 112 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 113 | // Populate the GlobalsGraph with globals from this one. |
| 114 | Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false); |
| 115 | |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 116 | // Save a copy of the original call nodes for the top-down pass |
| 117 | Graph->saveOrigFunctionCalls(); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 118 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 120 | // Start resolving calls... |
| 121 | std::vector<std::vector<DSNodeHandle> > &FCs = Graph->getFunctionCalls(); |
| 122 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 123 | DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n"); |
| 124 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 125 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 126 | // Add F to the PendingCallers list of each direct callee for use in the |
| 127 | // top-down pass so we don't have to compute this again. We don't want |
| 128 | // to do it for indirect callees inlined later, so remember which calls |
| 129 | // are in the original FCs set. |
| 130 | std::set<const DSNode*> directCallees; |
| 131 | for (unsigned i = 0; i < FCs.size(); ++i) |
| 132 | directCallees.insert(FCs[i][1]); // ptr to function node |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 133 | #endif |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 134 | |
| 135 | bool Inlined; |
| 136 | do { |
| 137 | Inlined = false; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 139 | for (unsigned i = 0; i != FCs.size(); ++i) { |
| 140 | // Copy the call, because inlining graphs may invalidate the FCs vector. |
| 141 | std::vector<DSNodeHandle> Call = FCs[i]; |
| 142 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 143 | // If the function list is complete... |
| 144 | if ((Call[1].getNode()->NodeType & DSNode::Incomplete) == 0) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 145 | // Start inlining all of the functions we can... some may not be |
| 146 | // inlinable if they are external... |
| 147 | // |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 148 | std::vector<GlobalValue*> Callees(Call[1].getNode()->getGlobals()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 149 | |
| 150 | // Loop over the functions, inlining whatever we can... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 151 | for (unsigned c = 0; c != Callees.size(); ++c) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 152 | // Must be a function type, so this cast MUST succeed. |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 153 | Function &FI = cast<Function>(*Callees[c]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 154 | if (&FI == &F) { |
| 155 | // Self recursion... simply link up the formal arguments with the |
| 156 | // actual arguments... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 157 | |
| 158 | DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 160 | if (Call[0].getNode()) // Handle the return value if present... |
| 161 | Graph->getRetNode().mergeWith(Call[0]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 162 | |
| 163 | // Resolve the arguments in the call to the actual values... |
| 164 | ResolveArguments(Call, F, Graph->getValueMap()); |
| 165 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 166 | // Erase the entry in the callees vector |
| 167 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 168 | } else if (!FI.isExternal()) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 169 | DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: " |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 170 | << FI.getName() << "\n"); |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 172 | // Get the data structure graph for the called function, closing it |
| 173 | // if possible (which is only impossible in the case of mutual |
| 174 | // recursion... |
| 175 | // |
| 176 | DSGraph &GI = calculateGraph(FI); // Graph to inline |
| 177 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 178 | DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName() |
| 179 | << " in: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 180 | |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 181 | // Clone the callee's graph into the current graph, keeping |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 182 | // track of where scalars in the old graph _used_ to point, |
| 183 | // and of the new nodes matching nodes of the old graph. |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 184 | map<Value*, DSNodeHandle> OldValMap; |
| 185 | map<const DSNode*, DSNode*> OldNodeMap; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 186 | |
| 187 | // 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] | 188 | // structure graph. Strip locals and don't copy the list of callers |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 189 | DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap, |
| 190 | /*StripScalars*/ true, |
| 191 | /*StripAllocas*/ true, |
| 192 | /*CopyCallers*/ false, |
| 193 | /*CopyOrigCalls*/ false); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 195 | // Resolve the arguments in the call to the actual values... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 196 | ResolveArguments(Call, FI, OldValMap); |
| 197 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 198 | if (Call[0].getNode()) // Handle the return value if present |
| 199 | RetVal.mergeWith(Call[0]); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 201 | // Merge global value nodes in the inlined graph with the global |
| 202 | // value nodes in the current graph if there are duplicates. |
| 203 | // |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 204 | MergeGlobalNodes(*Graph, OldValMap); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 206 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 207 | // If this was an original call, add F to the PendingCallers list |
| 208 | if (directCallees.find(Call[1]) != directCallees.end()) |
| 209 | GI.addCaller(F); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 210 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 211 | |
| 212 | // Erase the entry in the Callees vector |
| 213 | Callees.erase(Callees.begin()+c--); |
| 214 | |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 215 | } else if (FI.getName() == "printf" || FI.getName() == "sscanf" || |
| 216 | FI.getName() == "fprintf" || FI.getName() == "open" || |
| 217 | FI.getName() == "sprintf") { |
| 218 | |
| 219 | // Erase the entry in the globals vector |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 220 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 224 | if (Callees.empty()) { // Inlined all of the function calls? |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 225 | // Erase the call if it is resolvable... |
| 226 | FCs.erase(FCs.begin()+i--); // Don't skip a the next call... |
| 227 | Inlined = true; |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 228 | } else if (Callees.size() != Call[1].getNode()->getGlobals().size()) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 229 | // Was able to inline SOME, but not all of the functions. Construct a |
| 230 | // new global node here. |
| 231 | // |
| 232 | assert(0 && "Unimpl!"); |
| 233 | Inlined = true; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Recompute the Incomplete markers. If there are any function calls left |
| 239 | // now that are complete, we must loop! |
| 240 | if (Inlined) { |
| 241 | Graph->maskIncompleteMarkers(); |
| 242 | Graph->markIncompleteNodes(); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 243 | Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 244 | } |
| 245 | } while (Inlined && !FCs.empty()); |
| 246 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 247 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 248 | // Copy any unresolved call nodes into the Globals graph and |
| 249 | // filter out unresolved call nodes inlined from the callee. |
| 250 | if (!FCs.empty()) |
| 251 | Graph->GlobalsGraph->cloneCalls(*Graph); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 252 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 253 | |
| 254 | Graph->maskIncompleteMarkers(); |
| 255 | Graph->markIncompleteNodes(); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame^] | 256 | Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 258 | DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " [" |
| 259 | << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size() |
| 260 | << "]\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 262 | return *Graph; |
| 263 | } |