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