Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 1 | //===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the TDDataStructures class, which represents the |
| 11 | // Top-down Interprocedural closure of the data structure graph over the |
| 12 | // program. This is useful (but not strictly necessary?) for applications |
| 13 | // like pointer analysis. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | RegisterAnalysis<TDDataStructures> // Register the pass |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 27 | Y("tddatastructure", "Top-down Data Structure Analysis"); |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 28 | |
| 29 | Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined"); |
| 30 | } |
| 31 | |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 32 | void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N, |
| 33 | hash_set<DSNode*> &Visited) { |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 34 | if (!N || Visited.count(N)) return; |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 35 | Visited.insert(N); |
| 36 | |
| 37 | for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) { |
| 38 | DSNodeHandle &NH = N->getLink(i*N->getPointerSize()); |
| 39 | if (DSNode *NN = NH.getNode()) { |
Chris Lattner | 9454dda | 2005-03-20 02:39:49 +0000 | [diff] [blame] | 40 | std::vector<Function*> Functions; |
| 41 | NN->addFullFunctionList(Functions); |
| 42 | ArgsRemainIncomplete.insert(Functions.begin(), Functions.end()); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 43 | markReachableFunctionsExternallyAccessible(NN, Visited); |
| 44 | } |
| 45 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 46 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 48 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 49 | // run - Calculate the top down data structure graphs for each function in the |
| 50 | // program. |
| 51 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 52 | bool TDDataStructures::runOnModule(Module &M) { |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 53 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 54 | GlobalECs = BU.getGlobalECs(); |
| 55 | GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs); |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 56 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 58 | // Figure out which functions must not mark their arguments complete because |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 59 | // they are accessible outside this compilation unit. Currently, these |
| 60 | // arguments are functions which are reachable by global variables in the |
| 61 | // globals graph. |
Chris Lattner | 6d8f3dc | 2004-01-28 03:12:48 +0000 | [diff] [blame] | 62 | const DSScalarMap &GGSM = GlobalsGraph->getScalarMap(); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 63 | hash_set<DSNode*> Visited; |
Misha Brukman | 96a8bd7 | 2004-04-29 04:05:30 +0000 | [diff] [blame] | 64 | for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end(); |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 65 | I != E; ++I) { |
| 66 | DSNode *N = GGSM.find(*I)->second.getNode(); |
| 67 | if (N->isIncomplete()) |
| 68 | markReachableFunctionsExternallyAccessible(N, Visited); |
| 69 | } |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 70 | |
| 71 | // Loop over unresolved call nodes. Any functions passed into (but not |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 72 | // returned!) from unresolvable call nodes may be invoked outside of the |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 73 | // current module. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 74 | for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(), |
| 75 | E = GlobalsGraph->afc_end(); I != E; ++I) |
| 76 | for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg) |
| 77 | markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(), |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 78 | Visited); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 79 | Visited.clear(); |
| 80 | |
| 81 | // Functions without internal linkage also have unknown incoming arguments! |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 82 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 83 | if (!I->isExternal() && !I->hasInternalLinkage()) |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 84 | ArgsRemainIncomplete.insert(I); |
| 85 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 86 | // We want to traverse the call graph in reverse post-order. To do this, we |
| 87 | // calculate a post-order traversal, then reverse it. |
| 88 | hash_set<DSGraph*> VisitedGraph; |
| 89 | std::vector<DSGraph*> PostOrder; |
| 90 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 91 | getAnalysis<BUDataStructures>().getActualCallees(); |
| 92 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 93 | // Calculate top-down from main... |
| 94 | if (Function *F = M.getMainFunction()) |
Chris Lattner | 61691c5 | 2003-07-02 23:44:15 +0000 | [diff] [blame] | 95 | ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 96 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 97 | // Next calculate the graphs for each unreachable function... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 98 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 99 | ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees); |
| 100 | |
| 101 | VisitedGraph.clear(); // Release memory! |
| 102 | |
| 103 | // Visit each of the graphs in reverse post-order now! |
| 104 | while (!PostOrder.empty()) { |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 105 | InlineCallersIntoGraph(*PostOrder.back()); |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 106 | PostOrder.pop_back(); |
| 107 | } |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 109 | // Free the IndCallMap. |
| 110 | while (!IndCallMap.empty()) { |
| 111 | delete IndCallMap.begin()->second; |
| 112 | IndCallMap.erase(IndCallMap.begin()); |
| 113 | } |
| 114 | |
| 115 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 116 | ArgsRemainIncomplete.clear(); |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 117 | GlobalsGraph->removeTriviallyDeadNodes(); |
| 118 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 123 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 124 | DSGraph *&G = DSInfo[&F]; |
| 125 | if (G == 0) { // Not created yet? Clone BU graph... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 126 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 127 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 128 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 129 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 130 | } |
| 131 | return *G; |
| 132 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 133 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 135 | void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited, |
| 136 | std::vector<DSGraph*> &PostOrder, |
| 137 | const BUDataStructures::ActualCalleesTy &ActualCallees) { |
| 138 | if (F.isExternal()) return; |
| 139 | DSGraph &G = getOrCreateDSGraph(F); |
| 140 | if (Visited.count(&G)) return; |
| 141 | Visited.insert(&G); |
| 142 | |
| 143 | // Recursively traverse all of the callee graphs. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 144 | for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E; ++CI) { |
| 145 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 146 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
| 147 | BUDataStructures::ActualCalleesTy::const_iterator> |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 148 | IP = ActualCallees.equal_range(CallI); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 150 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 151 | I != IP.second; ++I) |
| 152 | ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees); |
| 153 | } |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 155 | PostOrder.push_back(&G); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 161 | |
| 162 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 163 | // our memory... here... |
| 164 | // |
| 165 | // FIXME: This should be releaseMemory and will work fine, except that LoadVN |
| 166 | // has no way to extend the lifetime of the pass, which screws up ds-aa. |
| 167 | // |
| 168 | void TDDataStructures::releaseMyMemory() { |
| 169 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 170 | E = DSInfo.end(); I != E; ++I) { |
| 171 | I->second->getReturnNodes().erase(I->first); |
| 172 | if (I->second->getReturnNodes().empty()) |
| 173 | delete I->second; |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 174 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 176 | // Empty map so next time memory is released, data structures are not |
| 177 | // re-deleted. |
| 178 | DSInfo.clear(); |
| 179 | delete GlobalsGraph; |
| 180 | GlobalsGraph = 0; |
| 181 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 182 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 183 | /// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph |
| 184 | /// into it, then recompute completeness of nodes in the resultant graph. |
| 185 | void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) { |
| 186 | // Inline caller graphs into this graph. First step, get the list of call |
| 187 | // sites that call into this graph. |
| 188 | std::vector<CallerCallEdge> EdgesFromCaller; |
| 189 | std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator |
| 190 | CEI = CallerEdges.find(&DSG); |
| 191 | if (CEI != CallerEdges.end()) { |
| 192 | std::swap(CEI->second, EdgesFromCaller); |
| 193 | CallerEdges.erase(CEI); |
| 194 | } |
| 195 | |
| 196 | // Sort the caller sites to provide a by-caller-graph ordering. |
| 197 | std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end()); |
| 198 | |
| 199 | |
Chris Lattner | c2b9480 | 2005-03-21 08:43:32 +0000 | [diff] [blame] | 200 | // Merge information from the globals graph into this graph. FIXME: This is |
| 201 | // stupid. Instead of us cloning information from the GG into this graph, |
| 202 | // then having RemoveDeadNodes clone it back, we should do all of this as a |
| 203 | // post-pass over all of the graphs. We need to take cloning out of |
| 204 | // removeDeadNodes and gut removeDeadNodes at the same time first though. :( |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 205 | { |
| 206 | DSGraph &GG = *DSG.getGlobalsGraph(); |
| 207 | ReachabilityCloner RC(DSG, GG, |
| 208 | DSGraph::DontCloneCallNodes | |
| 209 | DSGraph::DontCloneAuxCallNodes); |
| 210 | for (DSScalarMap::global_iterator |
| 211 | GI = DSG.getScalarMap().global_begin(), |
| 212 | E = DSG.getScalarMap().global_end(); GI != E; ++GI) |
| 213 | RC.getClonedNH(GG.getNodeForValue(*GI)); |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | DEBUG(std::cerr << "[TD] Inlining callers into '" << DSG.getFunctionNames() |
| 217 | << "'\n"); |
| 218 | |
| 219 | // Iteratively inline caller graphs into this graph. |
| 220 | while (!EdgesFromCaller.empty()) { |
| 221 | DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph; |
| 222 | |
| 223 | // Iterate through all of the call sites of this graph, cloning and merging |
| 224 | // any nodes required by the call. |
| 225 | ReachabilityCloner RC(DSG, CallerGraph, |
| 226 | DSGraph::DontCloneCallNodes | |
| 227 | DSGraph::DontCloneAuxCallNodes); |
| 228 | |
| 229 | // Inline all call sites from this caller graph. |
| 230 | do { |
| 231 | const DSCallSite &CS = *EdgesFromCaller.back().CS; |
| 232 | Function &CF = *EdgesFromCaller.back().CalledFunction; |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 233 | DEBUG(std::cerr << " [TD] Inlining graph into Fn '" |
| 234 | << CF.getName() << "' from "); |
| 235 | if (CallerGraph.getReturnNodes().empty()) |
| 236 | DEBUG(std::cerr << "SYNTHESIZED INDIRECT GRAPH"); |
| 237 | else |
| 238 | DEBUG (std::cerr << "Fn '" |
| 239 | << CS.getCallSite().getInstruction()-> |
| 240 | getParent()->getParent()->getName() << "'"); |
| 241 | DEBUG(std::cerr << ": " << CF.getFunctionType()->getNumParams() |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 242 | << " args\n"); |
| 243 | |
| 244 | // Get the formal argument and return nodes for the called function and |
| 245 | // merge them with the cloned subgraph. |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 246 | DSCallSite T1 = DSG.getCallSiteForArguments(CF); |
| 247 | RC.mergeCallSite(T1, CS); |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 248 | ++NumTDInlines; |
| 249 | |
| 250 | EdgesFromCaller.pop_back(); |
| 251 | } while (!EdgesFromCaller.empty() && |
| 252 | EdgesFromCaller.back().CallerGraph == &CallerGraph); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | // Next, now that this graph is finalized, we need to recompute the |
| 257 | // incompleteness markers for this graph and remove unreachable nodes. |
| 258 | DSG.maskIncompleteMarkers(); |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 259 | |
| 260 | // If any of the functions has incomplete incoming arguments, don't mark any |
| 261 | // of them as complete. |
| 262 | bool HasIncompleteArgs = false; |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 263 | for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(), |
| 264 | E = DSG.retnodes_end(); I != E; ++I) |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 265 | if (ArgsRemainIncomplete.count(I->first)) { |
| 266 | HasIncompleteArgs = true; |
| 267 | break; |
| 268 | } |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 269 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 270 | // Recompute the Incomplete markers. Depends on whether args are complete |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 271 | unsigned Flags |
| 272 | = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs; |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 273 | DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 274 | |
| 275 | // Delete dead nodes. Treat globals that are unreachable as dead also. |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 276 | DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 277 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 278 | // We are done with computing the current TD Graph! Finally, before we can |
| 279 | // finish processing this function, we figure out which functions it calls and |
| 280 | // records these call graph edges, so that we have them when we process the |
| 281 | // callee graphs. |
| 282 | if (DSG.fc_begin() == DSG.fc_end()) return; |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 283 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 284 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 285 | getAnalysis<BUDataStructures>().getActualCallees(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 286 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 287 | // Loop over all the call sites and all the callees at each call site, and add |
| 288 | // edges to the CallerEdges structure for each callee. |
| 289 | for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end(); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 290 | CI != E; ++CI) { |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 291 | |
| 292 | // Handle direct calls efficiently. |
| 293 | if (CI->isDirectCall()) { |
| 294 | if (!CI->getCalleeFunc()->isExternal() && |
| 295 | !DSG.getReturnNodes().count(CI->getCalleeFunc())) |
| 296 | CallerEdges[&getDSGraph(*CI->getCalleeFunc())] |
| 297 | .push_back(CallerCallEdge(&DSG, &*CI, CI->getCalleeFunc())); |
| 298 | continue; |
| 299 | } |
| 300 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 301 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 302 | // For each function in the invoked function list at this call site... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 303 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 304 | BUDataStructures::ActualCalleesTy::const_iterator> |
| 305 | IP = ActualCallees.equal_range(CallI); |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 306 | |
| 307 | // Skip over all calls to this graph (SCC calls). |
| 308 | while (IP.first != IP.second && &getDSGraph(*IP.first->second) == &DSG) |
| 309 | ++IP.first; |
| 310 | |
| 311 | // All SCC calls? |
| 312 | if (IP.first == IP.second) continue; |
| 313 | |
| 314 | Function *FirstCallee = IP.first->second; |
| 315 | ++IP.first; |
| 316 | |
| 317 | // Skip over more SCC calls. |
| 318 | while (IP.first != IP.second && &getDSGraph(*IP.first->second) == &DSG) |
| 319 | ++IP.first; |
| 320 | |
| 321 | // If there is exactly one callee from this call site, remember the edge in |
| 322 | // CallerEdges. |
| 323 | if (IP.first == IP.second) { |
| 324 | if (!FirstCallee->isExternal()) |
| 325 | CallerEdges[&getDSGraph(*FirstCallee)] |
| 326 | .push_back(CallerCallEdge(&DSG, &*CI, FirstCallee)); |
| 327 | continue; |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 328 | } |
Chris Lattner | 275b301 | 2005-03-21 20:31:29 +0000 | [diff] [blame] | 329 | |
| 330 | // Otherwise, there are multiple callees from this call site, so it must be |
| 331 | // an indirect call. Chances are that there will be other call sites with |
| 332 | // this set of targets. If so, we don't want to do M*N inlining operations, |
| 333 | // so we build up a new, private, graph that represents the calls of all |
| 334 | // calls to this set of functions. |
| 335 | std::vector<Function*> Callees; |
| 336 | IP = ActualCallees.equal_range(CallI); |
| 337 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 338 | I != IP.second; ++I) |
| 339 | if (!I->second->isExternal()) |
| 340 | Callees.push_back(I->second); |
| 341 | std::sort(Callees.begin(), Callees.end()); |
| 342 | |
| 343 | std::map<std::vector<Function*>, DSGraph*>::iterator IndCallRecI = |
| 344 | IndCallMap.lower_bound(Callees); |
| 345 | |
| 346 | DSGraph *IndCallGraph; |
| 347 | |
| 348 | // If we already have this graph, recycle it. |
| 349 | if (IndCallRecI != IndCallMap.end() && IndCallRecI->first == Callees) { |
| 350 | std::cerr << " [TD] *** Reuse of indcall graph for " << Callees.size() |
| 351 | << " callees!\n"; |
| 352 | IndCallGraph = IndCallRecI->second; |
| 353 | } else { |
| 354 | // Otherwise, create a new DSGraph to represent this. |
| 355 | IndCallGraph = new DSGraph(DSG.getGlobalECs(), DSG.getTargetData()); |
| 356 | |
| 357 | // Make a nullary dummy call site, which will eventually get some content |
| 358 | // merged into it. The actual callee function doesn't matter here, so we |
| 359 | // just pass it something to keep the ctor happy. |
| 360 | std::vector<DSNodeHandle> ArgDummyVec; |
| 361 | DSCallSite DummyCS(CI->getCallSite(), DSNodeHandle(), Callees[0]/*dummy*/, |
| 362 | ArgDummyVec); |
| 363 | IndCallGraph->getFunctionCalls().push_back(DummyCS); |
| 364 | |
| 365 | IndCallRecI = IndCallMap.insert(IndCallRecI, |
| 366 | std::make_pair(Callees, IndCallGraph)); |
| 367 | |
| 368 | // Additionally, make sure that each of the callees inlines this graph |
| 369 | // exactly once. |
| 370 | DSCallSite *NCS = &IndCallGraph->getFunctionCalls().front(); |
| 371 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) { |
| 372 | DSGraph& CalleeGraph = getDSGraph(*Callees[i]); |
| 373 | if (&CalleeGraph != &DSG) |
| 374 | CallerEdges[&CalleeGraph].push_back(CallerCallEdge(IndCallGraph, NCS, |
| 375 | Callees[i])); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Now that we know which graph to use for this, merge the caller |
| 380 | // information into the graph, based on information from the call site. |
| 381 | ReachabilityCloner RC(*IndCallGraph, DSG, 0); |
| 382 | RC.mergeCallSite(IndCallGraph->getFunctionCalls().front(), *CI); |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 383 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 384 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 385 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 387 | static const Function *getFnForValue(const Value *V) { |
| 388 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 389 | return I->getParent()->getParent(); |
| 390 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 391 | return A->getParent(); |
| 392 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 393 | return BB->getParent(); |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | void TDDataStructures::deleteValue(Value *V) { |
| 398 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 399 | // If this is a function local value, just delete it from the scalar map! |
| 400 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 401 | return; |
| 402 | } |
| 403 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 404 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 405 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 406 | "cannot handle scc's"); |
| 407 | delete DSInfo[F]; |
| 408 | DSInfo.erase(F); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 413 | } |
| 414 | |
| 415 | void TDDataStructures::copyValue(Value *From, Value *To) { |
| 416 | if (From == To) return; |
| 417 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 418 | // If this is a function local value, just delete it from the scalar map! |
| 419 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 424 | Function *ToF = cast<Function>(To); |
| 425 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 426 | DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs); |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 427 | DSInfo[ToF] = NG; |
| 428 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 429 | |
| 430 | // Change the Function* is the returnnodes map to the ToF. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 431 | DSNodeHandle Ret = NG->retnodes_begin()->second; |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 432 | NG->getReturnNodes().clear(); |
| 433 | NG->getReturnNodes()[ToF] = Ret; |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!"); |
| 438 | } |