Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 1 | //===- CompleteBottomUp.cpp - Complete Bottom-Up Data Structure Graphs ----===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the exact same as the bottom-up graphs, but we use take a completed |
| 11 | // call graph and inline all indirect callees into their callers graphs, making |
| 12 | // the result more useful for things like pool allocation. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/ADT/SCCIterator.h" |
| 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | RegisterAnalysis<CompleteBUDataStructures> |
| 27 | X("cbudatastructure", "'Complete' Bottom-up Data Structure Analysis"); |
Chris Lattner | da5c5a5 | 2004-03-04 19:16:35 +0000 | [diff] [blame] | 28 | Statistic<> NumCBUInlines("cbudatastructures", "Number of graphs inlined"); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 31 | |
| 32 | // run - Calculate the bottom up data structure graphs for each function in the |
| 33 | // program. |
| 34 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 35 | bool CompleteBUDataStructures::runOnModule(Module &M) { |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 36 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame^] | 37 | GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 38 | GlobalsGraph->setPrintAuxCalls(); |
| 39 | |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 40 | #if 1 // REMOVE ME EVENTUALLY |
| 41 | // FIXME: TEMPORARY (remove once finalization of indirect call sites in the |
| 42 | // globals graph has been implemented in the BU pass) |
| 43 | TDDataStructures &TD = getAnalysis<TDDataStructures>(); |
| 44 | |
Vikram S. Adve | 052682f | 2004-05-23 08:00:34 +0000 | [diff] [blame] | 45 | ActualCallees.clear(); |
| 46 | |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 47 | // The call graph extractable from the TD pass is _much more complete_ and |
| 48 | // trustable than that generated by the BU pass so far. Until this is fixed, |
| 49 | // we hack it like this: |
| 50 | for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) { |
| 51 | if (MI->isExternal()) continue; |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 52 | const std::list<DSCallSite> &CSs = TD.getDSGraph(*MI).getFunctionCalls(); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 53 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 54 | for (std::list<DSCallSite>::const_iterator CSI = CSs.begin(), E = CSs.end(); |
| 55 | CSI != E; ++CSI) { |
| 56 | Instruction *TheCall = CSI->getCallSite().getInstruction(); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 58 | if (CSI->isIndirectCall()) { // indirect call: insert all callees |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 59 | const std::vector<GlobalValue*> &Callees = |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 60 | CSI->getCalleeNode()->getGlobals(); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) |
| 62 | if (Function *F = dyn_cast<Function>(Callees[i])) |
| 63 | ActualCallees.insert(std::make_pair(TheCall, F)); |
Vikram S. Adve | 052682f | 2004-05-23 08:00:34 +0000 | [diff] [blame] | 64 | } else { // direct call: insert the single callee directly |
| 65 | ActualCallees.insert(std::make_pair(TheCall, |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 66 | CSI->getCalleeFunc())); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Vikram S. Adve | 052682f | 2004-05-23 08:00:34 +0000 | [diff] [blame] | 70 | #else |
| 71 | // Our call graph is the same as the BU data structures call graph |
| 72 | ActualCallees = BU.getActualCallees(); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 75 | std::vector<DSGraph*> Stack; |
| 76 | hash_map<DSGraph*, unsigned> ValMap; |
| 77 | unsigned NextID = 1; |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 78 | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 79 | Function *MainFunc = M.getMainFunction(); |
| 80 | if (MainFunc) { |
| 81 | if (!MainFunc->isExternal()) |
| 82 | calculateSCCGraphs(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 83 | } else { |
| 84 | std::cerr << "CBU-DSA: No 'main' function found!\n"; |
| 85 | } |
| 86 | |
| 87 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 88 | if (!I->isExternal() && !DSInfo.count(I)) |
| 89 | calculateSCCGraphs(getOrCreateGraph(*I), Stack, NextID, ValMap); |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 2dea8d6 | 2004-02-08 01:53:10 +0000 | [diff] [blame] | 91 | GlobalsGraph->removeTriviallyDeadNodes(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 92 | |
| 93 | |
| 94 | // Merge the globals variables (not the calls) from the globals graph back |
| 95 | // into the main function's graph so that the main function contains all of |
| 96 | // the information about global pools and GV usage in the program. |
Chris Lattner | 49e88e8 | 2005-03-15 22:10:04 +0000 | [diff] [blame] | 97 | if (MainFunc && !MainFunc->isExternal()) { |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 98 | DSGraph &MainGraph = getOrCreateGraph(*MainFunc); |
| 99 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
| 100 | ReachabilityCloner RC(MainGraph, GG, |
| 101 | DSGraph::DontCloneCallNodes | |
| 102 | DSGraph::DontCloneAuxCallNodes); |
| 103 | |
| 104 | // Clone the global nodes into this graph. |
| 105 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 106 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 107 | if (isa<GlobalVariable>(*I)) |
| 108 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 109 | |
Chris Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 110 | MainGraph.maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 111 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
| 112 | DSGraph::IgnoreGlobals); |
| 113 | } |
| 114 | |
Chris Lattner | 95724a4 | 2003-11-13 01:43:00 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 117 | |
| 118 | DSGraph &CompleteBUDataStructures::getOrCreateGraph(Function &F) { |
| 119 | // Has the graph already been created? |
| 120 | DSGraph *&Graph = DSInfo[&F]; |
| 121 | if (Graph) return *Graph; |
| 122 | |
| 123 | // Copy the BU graph... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame^] | 124 | Graph = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 125 | Graph->setGlobalsGraph(GlobalsGraph); |
| 126 | Graph->setPrintAuxCalls(); |
| 127 | |
| 128 | // Make sure to update the DSInfo map for all of the functions currently in |
| 129 | // this graph! |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 130 | for (DSGraph::retnodes_iterator I = Graph->retnodes_begin(); |
| 131 | I != Graph->retnodes_end(); ++I) |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 132 | DSInfo[I->first] = Graph; |
| 133 | |
| 134 | return *Graph; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | unsigned CompleteBUDataStructures::calculateSCCGraphs(DSGraph &FG, |
| 140 | std::vector<DSGraph*> &Stack, |
| 141 | unsigned &NextID, |
| 142 | hash_map<DSGraph*, unsigned> &ValMap) { |
| 143 | assert(!ValMap.count(&FG) && "Shouldn't revisit functions!"); |
| 144 | unsigned Min = NextID++, MyID = Min; |
| 145 | ValMap[&FG] = Min; |
| 146 | Stack.push_back(&FG); |
| 147 | |
| 148 | // The edges out of the current node are the call site targets... |
Chris Lattner | f9aace2 | 2005-01-31 00:10:58 +0000 | [diff] [blame] | 149 | for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end(); |
| 150 | CI != CE; ++CI) { |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 151 | Instruction *Call = CI->getCallSite().getInstruction(); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 152 | |
| 153 | // Loop over all of the actually called functions... |
| 154 | ActualCalleesTy::iterator I, E; |
Chris Lattner | a366c98 | 2003-11-13 18:48:11 +0000 | [diff] [blame] | 155 | for (tie(I, E) = ActualCallees.equal_range(Call); I != E; ++I) |
| 156 | if (!I->second->isExternal()) { |
| 157 | DSGraph &Callee = getOrCreateGraph(*I->second); |
| 158 | unsigned M; |
| 159 | // Have we visited the destination function yet? |
| 160 | hash_map<DSGraph*, unsigned>::iterator It = ValMap.find(&Callee); |
| 161 | if (It == ValMap.end()) // No, visit it now. |
| 162 | M = calculateSCCGraphs(Callee, Stack, NextID, ValMap); |
| 163 | else // Yes, get it's number. |
| 164 | M = It->second; |
| 165 | if (M < Min) Min = M; |
| 166 | } |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!"); |
| 170 | if (Min != MyID) |
| 171 | return Min; // This is part of a larger SCC! |
| 172 | |
| 173 | // If this is a new SCC, process it now. |
| 174 | bool IsMultiNodeSCC = false; |
| 175 | while (Stack.back() != &FG) { |
| 176 | DSGraph *NG = Stack.back(); |
| 177 | ValMap[NG] = ~0U; |
| 178 | |
| 179 | DSGraph::NodeMapTy NodeMap; |
Chris Lattner | 825a02a | 2004-01-27 21:50:41 +0000 | [diff] [blame] | 180 | FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 181 | |
| 182 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 183 | for (DSGraph::retnodes_iterator I = NG->retnodes_begin(); |
| 184 | I != NG->retnodes_end(); ++I) |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 185 | DSInfo[I->first] = &FG; |
Chris Lattner | a1c972d | 2004-10-07 20:01:31 +0000 | [diff] [blame] | 186 | |
| 187 | // Remove NG from the ValMap since the pointer may get recycled. |
| 188 | ValMap.erase(NG); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 189 | delete NG; |
| 190 | |
| 191 | Stack.pop_back(); |
| 192 | IsMultiNodeSCC = true; |
| 193 | } |
| 194 | |
| 195 | // Clean up the graph before we start inlining a bunch again... |
| 196 | if (IsMultiNodeSCC) |
| 197 | FG.removeTriviallyDeadNodes(); |
| 198 | |
| 199 | Stack.pop_back(); |
| 200 | processGraph(FG); |
| 201 | ValMap[&FG] = ~0U; |
| 202 | return MyID; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /// processGraph - Process the BU graphs for the program in bottom-up order on |
| 207 | /// the SCC of the __ACTUAL__ call graph. This builds "complete" BU graphs. |
| 208 | void CompleteBUDataStructures::processGraph(DSGraph &G) { |
Chris Lattner | da5c5a5 | 2004-03-04 19:16:35 +0000 | [diff] [blame] | 209 | hash_set<Instruction*> calls; |
Chris Lattner | d10b5fd | 2004-02-20 23:52:15 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 211 | // The edges out of the current node are the call site targets... |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 212 | unsigned i = 0; |
Chris Lattner | f9aace2 | 2005-01-31 00:10:58 +0000 | [diff] [blame] | 213 | for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE; |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 214 | ++CI, ++i) { |
| 215 | const DSCallSite &CS = *CI; |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 216 | Instruction *TheCall = CS.getCallSite().getInstruction(); |
| 217 | |
Chris Lattner | da5c5a5 | 2004-03-04 19:16:35 +0000 | [diff] [blame] | 218 | assert(calls.insert(TheCall).second && |
| 219 | "Call instruction occurs multiple times in graph??"); |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 220 | |
| 221 | // Fast path for noop calls. Note that we don't care about merging globals |
| 222 | // in the callee with nodes in the caller here. |
| 223 | if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) |
| 224 | continue; |
Chris Lattner | da5c5a5 | 2004-03-04 19:16:35 +0000 | [diff] [blame] | 225 | |
Vikram S. Adve | 052682f | 2004-05-23 08:00:34 +0000 | [diff] [blame] | 226 | // Loop over all of the potentially called functions... |
| 227 | // Inline direct calls as well as indirect calls because the direct |
| 228 | // callee may have indirect callees and so may have changed. |
| 229 | // |
| 230 | ActualCalleesTy::iterator I, E; |
| 231 | tie(I, E) = ActualCallees.equal_range(TheCall); |
| 232 | unsigned TNum = 0, Num = std::distance(I, E); |
| 233 | for (; I != E; ++I, ++TNum) { |
| 234 | Function *CalleeFunc = I->second; |
| 235 | if (!CalleeFunc->isExternal()) { |
| 236 | // Merge the callee's graph into this graph. This works for normal |
| 237 | // calls or for self recursion within an SCC. |
| 238 | DSGraph &GI = getOrCreateGraph(*CalleeFunc); |
| 239 | ++NumCBUInlines; |
| 240 | G.mergeInGraph(CS, *CalleeFunc, GI, DSGraph::KeepModRefBits | |
| 241 | DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes | |
| 242 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 243 | DEBUG(std::cerr << " Inlining graph [" << i << "/" |
| 244 | << G.getFunctionCalls().size()-1 |
Vikram S. Adve | 052682f | 2004-05-23 08:00:34 +0000 | [diff] [blame] | 245 | << ":" << TNum << "/" << Num-1 << "] for " |
| 246 | << CalleeFunc->getName() << "[" |
| 247 | << GI.getGraphSize() << "+" << GI.getAuxFunctionCalls().size() |
| 248 | << "] into '" /*<< G.getFunctionNames()*/ << "' [" |
| 249 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
| 250 | << "]\n"); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 255 | // Recompute the Incomplete markers |
Chris Lattner | d10b5fd | 2004-02-20 23:52:15 +0000 | [diff] [blame] | 256 | assert(G.getInlinedGlobals().empty()); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 257 | G.maskIncompleteMarkers(); |
| 258 | G.markIncompleteNodes(DSGraph::MarkFormalArgs); |
| 259 | |
| 260 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 261 | // reach live nodes as live. |
Chris Lattner | 4ab483c | 2004-03-04 21:36:57 +0000 | [diff] [blame] | 262 | G.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | 79390d4 | 2003-11-13 05:05:41 +0000 | [diff] [blame] | 263 | } |