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 | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 13 | #include "Support/Statistic.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | using std::map; |
| 15 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 16 | static RegisterAnalysis<BUDataStructures> |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 17 | X("budatastructure", "Bottom-up Data Structure Analysis Closure"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 18 | |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 19 | using namespace DS; |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 20 | |
| 21 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 22 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 23 | // our memory... here... |
| 24 | // |
| 25 | void BUDataStructures::releaseMemory() { |
Chris Lattner | 613692c | 2002-10-17 04:24:08 +0000 | [diff] [blame] | 26 | // Delete all call site information |
| 27 | CallSites.clear(); |
| 28 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 29 | for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 30 | E = DSInfo.end(); I != E; ++I) |
| 31 | delete I->second; |
| 32 | |
| 33 | // Empty map so next time memory is released, data structures are not |
| 34 | // re-deleted. |
| 35 | DSInfo.clear(); |
| 36 | } |
| 37 | |
| 38 | // run - Calculate the bottom up data structure graphs for each function in the |
| 39 | // program. |
| 40 | // |
| 41 | bool BUDataStructures::run(Module &M) { |
| 42 | // Simply calculate the graphs for each function... |
| 43 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 44 | if (!I->isExternal()) |
| 45 | calculateGraph(*I); |
| 46 | return false; |
| 47 | } |
| 48 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 49 | // ResolveArguments - Resolve the formal and actual arguments for a function |
| 50 | // call. |
| 51 | // |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 52 | static void ResolveArguments(DSCallSite &Call, Function &F, |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 53 | map<Value*, DSNodeHandle> &ScalarMap) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 54 | // Resolve all of the function arguments... |
| 55 | Function::aiterator AI = F.abegin(); |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 56 | for (unsigned i = 0, e = Call.getNumPtrArgs(); i != e; ++i, ++AI) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 57 | // Advance the argument iterator to the first pointer argument... |
Chris Lattner | 048912b | 2002-11-04 02:29:15 +0000 | [diff] [blame] | 58 | while (!isPointerType(AI->getType())) { |
| 59 | ++AI; |
| 60 | #ifndef NDEBUG |
| 61 | if (AI == F.aend()) |
| 62 | std::cerr << "Bad call to Function: " << F.getName() << "\n"; |
| 63 | #endif |
| 64 | assert(AI != F.aend() && "# Args provided is not # Args required!"); |
| 65 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 66 | |
| 67 | // Add the link from the argument scalar to the provided value |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 68 | ScalarMap[AI].mergeWith(Call.getPtrArg(i)); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 72 | DSGraph &BUDataStructures::calculateGraph(Function &F) { |
| 73 | // Make sure this graph has not already been calculated, or that we don't get |
| 74 | // into an infinite loop with mutually recursive functions. |
| 75 | // |
| 76 | DSGraph *&Graph = DSInfo[&F]; |
| 77 | if (Graph) return *Graph; |
| 78 | |
| 79 | // Copy the local version into DSInfo... |
| 80 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F)); |
| 81 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 82 | #if 0 |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 83 | // Populate the GlobalsGraph with globals from this one. |
| 84 | Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 85 | #endif |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 87 | // Start resolving calls... |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 88 | std::vector<DSCallSite> &FCs = Graph->getFunctionCalls(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 89 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 90 | DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n"); |
| 91 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 92 | bool Inlined; |
| 93 | do { |
| 94 | Inlined = false; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 96 | for (unsigned i = 0; i != FCs.size(); ++i) { |
| 97 | // Copy the call, because inlining graphs may invalidate the FCs vector. |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 98 | DSCallSite Call = FCs[i]; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 100 | // If the function list is complete... |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 101 | if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 102 | // Start inlining all of the functions we can... some may not be |
| 103 | // inlinable if they are external... |
| 104 | // |
Chris Lattner | 7836d60 | 2002-10-20 22:11:17 +0000 | [diff] [blame] | 105 | std::vector<GlobalValue*> Callees = |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 106 | Call.getCallee().getNode()->getGlobals(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 107 | |
| 108 | // Loop over the functions, inlining whatever we can... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 109 | for (unsigned c = 0; c != Callees.size(); ++c) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 110 | // Must be a function type, so this cast MUST succeed. |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 111 | Function &FI = cast<Function>(*Callees[c]); |
Chris Lattner | 613692c | 2002-10-17 04:24:08 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 113 | if (&FI == &F) { |
| 114 | // Self recursion... simply link up the formal arguments with the |
| 115 | // actual arguments... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 116 | DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 7836d60 | 2002-10-20 22:11:17 +0000 | [diff] [blame] | 118 | // Handle the return value if present... |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 119 | Graph->getRetNode().mergeWith(Call.getRetVal()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 120 | |
| 121 | // Resolve the arguments in the call to the actual values... |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 122 | ResolveArguments(Call, F, Graph->getScalarMap()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 123 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 124 | // Erase the entry in the callees vector |
| 125 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 613692c | 2002-10-17 04:24:08 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 127 | } else if (!FI.isExternal()) { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 128 | DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: " |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 129 | << FI.getName() << "\n"); |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 131 | // Get the data structure graph for the called function, closing it |
| 132 | // if possible (which is only impossible in the case of mutual |
| 133 | // recursion... |
| 134 | // |
| 135 | DSGraph &GI = calculateGraph(FI); // Graph to inline |
| 136 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 137 | DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName() |
| 138 | << " in: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 139 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 140 | // Record that the original DSCallSite was a call site of FI. |
| 141 | // This may or may not have been known when the DSCallSite was |
| 142 | // originally created. |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 143 | std::vector<DSCallSite> &CallSitesForFunc = CallSites[&FI]; |
| 144 | CallSitesForFunc.push_back(Call); |
| 145 | CallSitesForFunc.back().setResolvingCaller(&F); |
Chris Lattner | 9faf18d | 2002-10-22 15:58:46 +0000 | [diff] [blame] | 146 | CallSitesForFunc.back().setCallee(0); |
Chris Lattner | 7a0b5bb | 2002-11-02 00:26:32 +0000 | [diff] [blame] | 147 | |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 148 | // Clone the callee's graph into the current graph, keeping |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 149 | // track of where scalars in the old graph _used_ to point, |
| 150 | // and of the new nodes matching nodes of the old graph. |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 151 | map<Value*, DSNodeHandle> OldValMap; |
| 152 | map<const DSNode*, DSNode*> OldNodeMap; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 153 | |
| 154 | // 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] | 155 | // structure graph. Strip locals and don't copy the list of callers |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 156 | DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap, |
Chris Lattner | e4ae304 | 2002-10-21 19:50:29 +0000 | [diff] [blame] | 157 | /*StripAllocas*/ true); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 159 | // Resolve the arguments in the call to the actual values... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 160 | ResolveArguments(Call, FI, OldValMap); |
| 161 | |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 162 | // Handle the return value if present... |
| 163 | RetVal.mergeWith(Call.getRetVal()); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 164 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 165 | // Erase the entry in the Callees vector |
| 166 | Callees.erase(Callees.begin()+c--); |
| 167 | |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 168 | } else if (FI.getName() == "printf" || FI.getName() == "sscanf" || |
| 169 | FI.getName() == "fprintf" || FI.getName() == "open" || |
| 170 | FI.getName() == "sprintf") { |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 171 | // FIXME: These special cases (eg printf) should go away when we can |
| 172 | // define functions that take a variable number of arguments. |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 174 | // FIXME: at the very least, this should update mod/ref info |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 175 | // Erase the entry in the globals vector |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 176 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 180 | if (Callees.empty()) { // Inlined all of the function calls? |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 181 | // Erase the call if it is resolvable... |
| 182 | FCs.erase(FCs.begin()+i--); // Don't skip a the next call... |
| 183 | Inlined = true; |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 184 | } else if (Callees.size() != |
| 185 | Call.getCallee().getNode()->getGlobals().size()) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 186 | // Was able to inline SOME, but not all of the functions. Construct a |
| 187 | // new global node here. |
| 188 | // |
| 189 | assert(0 && "Unimpl!"); |
| 190 | Inlined = true; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Recompute the Incomplete markers. If there are any function calls left |
| 196 | // now that are complete, we must loop! |
| 197 | if (Inlined) { |
| 198 | Graph->maskIncompleteMarkers(); |
| 199 | Graph->markIncompleteNodes(); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 200 | Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 201 | } |
| 202 | } while (Inlined && !FCs.empty()); |
| 203 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 204 | Graph->maskIncompleteMarkers(); |
| 205 | Graph->markIncompleteNodes(); |
Chris Lattner | a00397e | 2002-10-03 21:55:28 +0000 | [diff] [blame] | 206 | Graph->removeTriviallyDeadNodes(false); |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 207 | Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 209 | DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " [" |
| 210 | << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size() |
| 211 | << "]\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 213 | return *Graph; |
| 214 | } |