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