Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +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 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the BUDataStructures class, which represents the |
| 11 | // Bottom-Up Interprocedural closure of the data structure graph over the |
| 12 | // program. This is useful for applications like pool allocation, but **not** |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 13 | // applications like alias analysis. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "bu_dsa" |
Chris Lattner | 8adbec8 | 2004-07-07 06:35:22 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Chris Lattner | a5ed1bd | 2005-04-21 16:09:43 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Timer.h" |
Chris Lattner | 7238210 | 2006-01-22 23:19:18 +0000 | [diff] [blame] | 25 | #include <iostream> |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph"); |
Chris Lattner | d391d70 | 2003-07-02 20:24:42 +0000 | [diff] [blame] | 30 | Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined"); |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 31 | Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges"); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 32 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 33 | cl::opt<bool> |
| 34 | AddGlobals("budatastructures-annotate-calls", |
| 35 | cl::desc("Annotate call sites with functions as they are resolved")); |
| 36 | cl::opt<bool> |
| 37 | UpdateGlobals("budatastructures-update-from-globals", |
| 38 | cl::desc("Update local graph from global graph when processing function")); |
| 39 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 40 | RegisterAnalysis<BUDataStructures> |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 41 | X("budatastructure", "Bottom-up Data Structure Analysis"); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 42 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 43 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 44 | static bool GetAllCallees(const DSCallSite &CS, |
| 45 | std::vector<Function*> &Callees); |
| 46 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 47 | /// BuildGlobalECs - Look at all of the nodes in the globals graph. If any node |
| 48 | /// contains multiple globals, DSA will never, ever, be able to tell the globals |
| 49 | /// apart. Instead of maintaining this information in all of the graphs |
| 50 | /// throughout the entire program, store only a single global (the "leader") in |
| 51 | /// the graphs, and build equivalence classes for the rest of the globals. |
| 52 | static void BuildGlobalECs(DSGraph &GG, std::set<GlobalValue*> &ECGlobals) { |
| 53 | DSScalarMap &SM = GG.getScalarMap(); |
| 54 | EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs(); |
| 55 | for (DSGraph::node_iterator I = GG.node_begin(), E = GG.node_end(); |
| 56 | I != E; ++I) { |
| 57 | if (I->getGlobalsList().size() <= 1) continue; |
| 58 | |
| 59 | // First, build up the equivalence set for this block of globals. |
| 60 | const std::vector<GlobalValue*> &GVs = I->getGlobalsList(); |
| 61 | GlobalValue *First = GVs[0]; |
| 62 | for (unsigned i = 1, e = GVs.size(); i != e; ++i) |
| 63 | GlobalECs.unionSets(First, GVs[i]); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 65 | // Next, get the leader element. |
| 66 | assert(First == GlobalECs.getLeaderValue(First) && |
| 67 | "First did not end up being the leader?"); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 69 | // Next, remove all globals from the scalar map that are not the leader. |
| 70 | assert(GVs[0] == First && "First had to be at the front!"); |
| 71 | for (unsigned i = 1, e = GVs.size(); i != e; ++i) { |
| 72 | ECGlobals.insert(GVs[i]); |
| 73 | SM.erase(SM.find(GVs[i])); |
| 74 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 76 | // Finally, change the global node to only contain the leader. |
| 77 | I->clearGlobals(); |
| 78 | I->addGlobal(First); |
| 79 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 81 | DEBUG(GG.AssertGraphOK()); |
| 82 | } |
| 83 | |
| 84 | /// EliminateUsesOfECGlobals - Once we have determined that some globals are in |
| 85 | /// really just equivalent to some other globals, remove the globals from the |
| 86 | /// specified DSGraph (if present), and merge any nodes with their leader nodes. |
| 87 | static void EliminateUsesOfECGlobals(DSGraph &G, |
| 88 | const std::set<GlobalValue*> &ECGlobals) { |
| 89 | DSScalarMap &SM = G.getScalarMap(); |
| 90 | EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs(); |
| 91 | |
| 92 | bool MadeChange = false; |
| 93 | for (DSScalarMap::global_iterator GI = SM.global_begin(), E = SM.global_end(); |
| 94 | GI != E; ) { |
| 95 | GlobalValue *GV = *GI++; |
| 96 | if (!ECGlobals.count(GV)) continue; |
| 97 | |
| 98 | const DSNodeHandle &GVNH = SM[GV]; |
| 99 | assert(!GVNH.isNull() && "Global has null NH!?"); |
| 100 | |
| 101 | // Okay, this global is in some equivalence class. Start by finding the |
| 102 | // leader of the class. |
| 103 | GlobalValue *Leader = GlobalECs.getLeaderValue(GV); |
| 104 | |
| 105 | // If the leader isn't already in the graph, insert it into the node |
| 106 | // corresponding to GV. |
| 107 | if (!SM.global_count(Leader)) { |
| 108 | GVNH.getNode()->addGlobal(Leader); |
| 109 | SM[Leader] = GVNH; |
| 110 | } else { |
| 111 | // Otherwise, the leader is in the graph, make sure the nodes are the |
| 112 | // merged in the specified graph. |
| 113 | const DSNodeHandle &LNH = SM[Leader]; |
| 114 | if (LNH.getNode() != GVNH.getNode()) |
| 115 | LNH.mergeWith(GVNH); |
| 116 | } |
| 117 | |
| 118 | // Next step, remove the global from the DSNode. |
| 119 | GVNH.getNode()->removeGlobal(GV); |
| 120 | |
| 121 | // Finally, remove the global from the ScalarMap. |
| 122 | SM.erase(GV); |
| 123 | MadeChange = true; |
| 124 | } |
| 125 | |
| 126 | DEBUG(if(MadeChange) G.AssertGraphOK()); |
| 127 | } |
| 128 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 129 | static void AddGlobalToNode(BUDataStructures* B, DSCallSite D, Function* F) { |
| 130 | if(!AddGlobals) |
| 131 | return; |
| 132 | if(D.isIndirectCall()) { |
| 133 | DSGraph* GI = &B->getDSGraph(D.getCaller()); |
| 134 | DSNodeHandle& NHF = GI->getNodeForValue(F); |
| 135 | DSCallSite DL = GI->getDSCallSiteForCallSite(D.getCallSite()); |
| 136 | if (DL.getCalleeNode() != NHF.getNode() || NHF.isNull()) { |
| 137 | if (NHF.isNull()) { |
| 138 | DSNode *N = new DSNode(F->getType()->getElementType(), GI); // Create the node |
| 139 | N->addGlobal(F); |
| 140 | NHF.setTo(N,0); |
| 141 | DEBUG(std::cerr << "Adding " << F->getName() << " to a call node in " |
| 142 | << D.getCaller().getName() << "\n"); |
| 143 | } |
| 144 | DL.getCalleeNode()->mergeWith(NHF, 0); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 149 | // run - Calculate the bottom up data structure graphs for each function in the |
| 150 | // program. |
| 151 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 152 | bool BUDataStructures::runOnModule(Module &M) { |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 153 | LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 154 | GlobalECs = LocalDSA.getGlobalECs(); |
| 155 | |
| 156 | GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs); |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 157 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 158 | |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 159 | IndCallGraphMap = new std::map<std::vector<Function*>, |
| 160 | std::pair<DSGraph*, std::vector<DSNodeHandle> > >(); |
| 161 | |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 162 | std::vector<Function*> Stack; |
| 163 | hash_map<Function*, unsigned> ValMap; |
| 164 | unsigned NextID = 1; |
| 165 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 166 | Function *MainFunc = M.getMainFunction(); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 167 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 168 | if (MainFunc) |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 169 | calculateGraphs(MainFunc, Stack, NextID, ValMap); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 170 | |
| 171 | // Calculate the graphs for any functions that are unreachable from main... |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 172 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 173 | if (!I->isExternal() && !DSInfo.count(I)) { |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 174 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 175 | if (MainFunc) |
Andrew Lenharth | 5e091f3 | 2006-04-19 15:33:35 +0000 | [diff] [blame] | 176 | std::cerr << "*** BU: Function unreachable from main: " |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 177 | << I->getName() << "\n"; |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 178 | #endif |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 179 | calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs. |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 182 | // If we computed any temporary indcallgraphs, free them now. |
| 183 | for (std::map<std::vector<Function*>, |
| 184 | std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I = |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 185 | IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) { |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 186 | I->second.second.clear(); // Drop arg refs into the graph. |
| 187 | delete I->second.first; |
| 188 | } |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 189 | delete IndCallGraphMap; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 190 | |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 191 | // At the end of the bottom-up pass, the globals graph becomes complete. |
| 192 | // FIXME: This is not the right way to do this, but it is sorta better than |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 193 | // nothing! In particular, externally visible globals and unresolvable call |
| 194 | // nodes at the end of the BU phase should make things that they point to |
| 195 | // incomplete in the globals graph. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 196 | // |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 197 | GlobalsGraph->removeTriviallyDeadNodes(); |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 198 | GlobalsGraph->maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 9547ade | 2005-03-22 22:10:22 +0000 | [diff] [blame] | 200 | // Mark external globals incomplete. |
| 201 | GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals); |
| 202 | |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 203 | // Grow the equivalence classes for the globals to include anything that we |
| 204 | // now know to be aliased. |
| 205 | std::set<GlobalValue*> ECGlobals; |
| 206 | BuildGlobalECs(*GlobalsGraph, ECGlobals); |
| 207 | if (!ECGlobals.empty()) { |
Chris Lattner | a5ed1bd | 2005-04-21 16:09:43 +0000 | [diff] [blame] | 208 | NamedRegionTimer X("Bottom-UP EC Cleanup"); |
| 209 | std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n"; |
Chris Lattner | 33b4276 | 2005-03-25 16:45:43 +0000 | [diff] [blame] | 210 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 211 | E = DSInfo.end(); I != E; ++I) |
| 212 | EliminateUsesOfECGlobals(*I->second, ECGlobals); |
| 213 | } |
| 214 | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 215 | // Merge the globals variables (not the calls) from the globals graph back |
| 216 | // into the main function's graph so that the main function contains all of |
| 217 | // the information about global pools and GV usage in the program. |
Chris Lattner | 49e88e8 | 2005-03-15 22:10:04 +0000 | [diff] [blame] | 218 | if (MainFunc && !MainFunc->isExternal()) { |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 219 | DSGraph &MainGraph = getOrCreateGraph(MainFunc); |
| 220 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 221 | ReachabilityCloner RC(MainGraph, GG, DSGraph::DontCloneCallNodes | |
| 222 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 223 | |
| 224 | // Clone the global nodes into this graph. |
| 225 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 226 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 227 | if (isa<GlobalVariable>(*I)) |
| 228 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 229 | |
Chris Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 230 | MainGraph.maskIncompleteMarkers(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 231 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 232 | DSGraph::IgnoreGlobals); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 233 | |
| 234 | //Debug messages if along the way we didn't resolve a call site |
| 235 | //also update the call graph and callsites we did find. |
| 236 | for(DSGraph::afc_iterator ii = MainGraph.afc_begin(), |
| 237 | ee = MainGraph.afc_end(); ii != ee; ++ii) { |
| 238 | std::vector<Function*> Funcs; |
| 239 | GetAllCallees(*ii, Funcs); |
| 240 | std::cerr << "Lost site\n"; |
| 241 | for (std::vector<Function*>::iterator iif = Funcs.begin(), eef = Funcs.end(); |
| 242 | iif != eef; ++iif) { |
| 243 | AddGlobalToNode(this, *ii, *iif); |
| 244 | std::cerr << "Adding\n"; |
| 245 | ActualCallees.insert(std::make_pair(ii->getCallSite().getInstruction(), *iif)); |
| 246 | } |
| 247 | } |
| 248 | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 251 | NumCallEdges += ActualCallees.size(); |
| 252 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 255 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 256 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { |
| 257 | // Has the graph already been created? |
| 258 | DSGraph *&Graph = DSInfo[F]; |
| 259 | if (Graph) return *Graph; |
| 260 | |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 261 | DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 262 | |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 263 | // Steal the local graph. |
| 264 | Graph = new DSGraph(GlobalECs, LocGraph.getTargetData()); |
| 265 | Graph->spliceFrom(LocGraph); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 266 | |
| 267 | Graph->setGlobalsGraph(GlobalsGraph); |
| 268 | Graph->setPrintAuxCalls(); |
| 269 | |
| 270 | // Start with a copy of the original call sites... |
| 271 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); |
| 272 | return *Graph; |
| 273 | } |
| 274 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 275 | static bool isVAHackFn(const Function *F) { |
| 276 | return F->getName() == "printf" || F->getName() == "sscanf" || |
| 277 | F->getName() == "fprintf" || F->getName() == "open" || |
| 278 | F->getName() == "sprintf" || F->getName() == "fputs" || |
Chris Lattner | a5ed1bd | 2005-04-21 16:09:43 +0000 | [diff] [blame] | 279 | F->getName() == "fscanf" || F->getName() == "malloc" || |
| 280 | F->getName() == "free"; |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static bool isResolvableFunc(const Function* callee) { |
| 284 | return !callee->isExternal() || isVAHackFn(callee); |
| 285 | } |
| 286 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 287 | //returns true if all callees were resolved |
| 288 | static bool GetAllCallees(const DSCallSite &CS, |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 289 | std::vector<Function*> &Callees) { |
| 290 | if (CS.isDirectCall()) { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 291 | if (isResolvableFunc(CS.getCalleeFunc())) { |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 292 | Callees.push_back(CS.getCalleeFunc()); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 293 | return true; |
| 294 | } else |
| 295 | return false; |
| 296 | } else { |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 297 | // Get all callees. |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 298 | bool retval = CS.getCalleeNode()->isComplete(); |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 299 | unsigned OldSize = Callees.size(); |
| 300 | CS.getCalleeNode()->addFullFunctionList(Callees); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 301 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 302 | // If any of the callees are unresolvable, remove that one |
| 303 | for (unsigned i = OldSize; i != Callees.size(); ++i) |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 304 | if (!isResolvableFunc(Callees[i])) { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 305 | Callees.erase(Callees.begin()+i); |
| 306 | --i; |
| 307 | retval = false; |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 308 | } |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 309 | return retval; |
| 310 | //return false; |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 314 | /// GetAllAuxCallees - Return a list containing all of the resolvable callees in |
| 315 | /// the aux list for the specified graph in the Callees vector. |
| 316 | static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) { |
| 317 | Callees.clear(); |
| 318 | for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I) |
| 319 | GetAllCallees(*I, Callees); |
| 320 | } |
| 321 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 322 | unsigned BUDataStructures::calculateGraphs(Function *F, |
| 323 | std::vector<Function*> &Stack, |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 324 | unsigned &NextID, |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 325 | hash_map<Function*, unsigned> &ValMap) { |
Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 326 | assert(!ValMap.count(F) && "Shouldn't revisit functions!"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 327 | unsigned Min = NextID++, MyID = Min; |
| 328 | ValMap[F] = Min; |
| 329 | Stack.push_back(F); |
| 330 | |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 331 | // FIXME! This test should be generalized to be any function that we have |
| 332 | // already processed, in the case when there isn't a main or there are |
| 333 | // unreachable functions! |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 334 | if (F->isExternal()) { // sprintf, fprintf, sscanf, etc... |
| 335 | // No callees! |
| 336 | Stack.pop_back(); |
| 337 | ValMap[F] = ~0; |
| 338 | return Min; |
| 339 | } |
| 340 | |
| 341 | DSGraph &Graph = getOrCreateGraph(F); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 342 | if (UpdateGlobals) |
| 343 | Graph.updateFromGlobalGraph(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 345 | // Find all callee functions. |
| 346 | std::vector<Function*> CalleeFunctions; |
| 347 | GetAllAuxCallees(Graph, CalleeFunctions); |
| 348 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 349 | // The edges out of the current node are the call site targets... |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) { |
| 351 | Function *Callee = CalleeFunctions[i]; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 352 | unsigned M; |
| 353 | // Have we visited the destination function yet? |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 354 | hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 355 | if (It == ValMap.end()) // No, visit it now. |
| 356 | M = calculateGraphs(Callee, Stack, NextID, ValMap); |
| 357 | else // Yes, get it's number. |
| 358 | M = It->second; |
| 359 | if (M < Min) Min = M; |
| 360 | } |
| 361 | |
| 362 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); |
| 363 | if (Min != MyID) |
| 364 | return Min; // This is part of a larger SCC! |
| 365 | |
| 366 | // If this is a new SCC, process it now. |
| 367 | if (Stack.back() == F) { // Special case the single "SCC" case here. |
| 368 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " |
| 369 | << F->getName() << "\n"); |
| 370 | Stack.pop_back(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 371 | DSGraph &G = getDSGraph(*F); |
| 372 | DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n"); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 373 | bool redo = calculateGraph(G); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 374 | DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " [" |
| 375 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
| 376 | << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 377 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 378 | if (MaxSCC < 1) MaxSCC = 1; |
| 379 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 380 | // Should we revisit the graph? Only do it if there are now new resolvable |
| 381 | // callees. |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 382 | if (redo) { |
| 383 | DEBUG(std::cerr << "Recalculating " << F->getName() << " due to new knowledge\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 384 | ValMap.erase(F); |
| 385 | return calculateGraphs(F, Stack, NextID, ValMap); |
| 386 | } else { |
| 387 | ValMap[F] = ~0U; |
| 388 | } |
| 389 | return MyID; |
| 390 | |
| 391 | } else { |
| 392 | // SCCFunctions - Keep track of the functions in the current SCC |
| 393 | // |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 394 | std::vector<DSGraph*> SCCGraphs; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 395 | |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 396 | unsigned SCCSize = 1; |
| 397 | Function *NF = Stack.back(); |
| 398 | ValMap[NF] = ~0U; |
| 399 | DSGraph &SCCGraph = getDSGraph(*NF); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 401 | // First thing first, collapse all of the DSGraphs into a single graph for |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 402 | // the entire SCC. Splice all of the graphs into one and discard all of the |
| 403 | // old graphs. |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 404 | // |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 405 | while (NF != F) { |
| 406 | Stack.pop_back(); |
| 407 | NF = Stack.back(); |
| 408 | ValMap[NF] = ~0U; |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 409 | |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 410 | DSGraph &NFG = getDSGraph(*NF); |
| 411 | |
| 412 | // Update the Function -> DSG map. |
| 413 | for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(), |
| 414 | E = NFG.retnodes_end(); I != E; ++I) |
| 415 | DSInfo[I->first] = &SCCGraph; |
| 416 | |
| 417 | SCCGraph.spliceFrom(NFG); |
| 418 | delete &NFG; |
| 419 | |
| 420 | ++SCCSize; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 421 | } |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 422 | Stack.pop_back(); |
Chris Lattner | 20da24c | 2005-03-25 00:06:09 +0000 | [diff] [blame] | 423 | |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 424 | std::cerr << "Calculating graph for SCC #: " << MyID << " of size: " |
| 425 | << SCCSize << "\n"; |
| 426 | |
| 427 | // Compute the Max SCC Size. |
| 428 | if (MaxSCC < SCCSize) |
| 429 | MaxSCC = SCCSize; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 431 | // Clean up the graph before we start inlining a bunch again... |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 432 | SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 433 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 434 | // Now that we have one big happy family, resolve all of the call sites in |
| 435 | // the graph... |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 436 | bool redo = calculateGraph(SCCGraph); |
Chris Lattner | b2dbdc1 | 2005-03-25 00:05:04 +0000 | [diff] [blame] | 437 | DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize() |
| 438 | << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 439 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 440 | if (redo) { |
| 441 | DEBUG(std::cerr << "MISSING REDO\n"); |
| 442 | } |
| 443 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 444 | std::cerr << "DONE with SCC #: " << MyID << "\n"; |
| 445 | |
| 446 | // We never have to revisit "SCC" processed functions... |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 447 | return MyID; |
| 448 | } |
| 449 | |
| 450 | return MyID; // == Min |
| 451 | } |
| 452 | |
| 453 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 454 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 455 | // our memory... here... |
| 456 | // |
Chris Lattner | 65512d2 | 2005-03-23 21:59:34 +0000 | [diff] [blame] | 457 | void BUDataStructures::releaseMyMemory() { |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 458 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 459 | E = DSInfo.end(); I != E; ++I) { |
| 460 | I->second->getReturnNodes().erase(I->first); |
| 461 | if (I->second->getReturnNodes().empty()) |
| 462 | delete I->second; |
| 463 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 464 | |
| 465 | // Empty map so next time memory is released, data structures are not |
| 466 | // re-deleted. |
| 467 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 468 | delete GlobalsGraph; |
| 469 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | a5ed1bd | 2005-04-21 16:09:43 +0000 | [diff] [blame] | 472 | DSGraph &BUDataStructures::CreateGraphForExternalFunction(const Function &Fn) { |
| 473 | Function *F = const_cast<Function*>(&Fn); |
| 474 | DSGraph *DSG = new DSGraph(GlobalECs, GlobalsGraph->getTargetData()); |
| 475 | DSInfo[F] = DSG; |
| 476 | DSG->setGlobalsGraph(GlobalsGraph); |
| 477 | DSG->setPrintAuxCalls(); |
| 478 | |
| 479 | // Add function to the graph. |
| 480 | DSG->getReturnNodes().insert(std::make_pair(F, DSNodeHandle())); |
| 481 | |
| 482 | if (F->getName() == "free") { // Taking the address of free. |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 483 | |
Chris Lattner | a5ed1bd | 2005-04-21 16:09:43 +0000 | [diff] [blame] | 484 | // Free should take a single pointer argument, mark it as heap memory. |
| 485 | DSNode *N = new DSNode(0, DSG); |
| 486 | N->setHeapNodeMarker(); |
| 487 | DSG->getNodeForValue(F->arg_begin()).mergeWith(N); |
| 488 | |
| 489 | } else { |
| 490 | std::cerr << "Unrecognized external function: " << F->getName() << "\n"; |
| 491 | abort(); |
| 492 | } |
| 493 | |
| 494 | return *DSG; |
| 495 | } |
| 496 | |
| 497 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 498 | bool BUDataStructures::calculateGraph(DSGraph &Graph) { |
Chris Lattner | a1198b5 | 2005-04-25 19:16:31 +0000 | [diff] [blame] | 499 | // If this graph contains the main function, clone the globals graph into this |
| 500 | // graph before we inline callees and other fun stuff. |
| 501 | bool ContainsMain = false; |
| 502 | DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes(); |
| 503 | |
| 504 | for (DSGraph::ReturnNodesTy::iterator I = ReturnNodes.begin(), |
| 505 | E = ReturnNodes.end(); I != E; ++I) |
| 506 | if (I->first->hasExternalLinkage() && I->first->getName() == "main") { |
| 507 | ContainsMain = true; |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | // If this graph contains main, copy the contents of the globals graph over. |
| 512 | // Note that this is *required* for correctness. If a callee contains a use |
| 513 | // of a global, we have to make sure to link up nodes due to global-argument |
| 514 | // bindings. |
| 515 | if (ContainsMain) { |
| 516 | const DSGraph &GG = *Graph.getGlobalsGraph(); |
| 517 | ReachabilityCloner RC(Graph, GG, |
| 518 | DSGraph::DontCloneCallNodes | |
| 519 | DSGraph::DontCloneAuxCallNodes); |
| 520 | |
| 521 | // Clone the global nodes into this graph. |
| 522 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 523 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 524 | if (isa<GlobalVariable>(*I)) |
| 525 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 526 | } |
| 527 | |
| 528 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 529 | // Move our call site list into TempFCs so that inline call sites go into the |
| 530 | // new call site list and doesn't invalidate our iterators! |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 531 | std::list<DSCallSite> TempFCs; |
| 532 | std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 533 | TempFCs.swap(AuxCallsList); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 534 | //remember what we've seen (or will see) |
| 535 | unsigned oldSize = TempFCs.size(); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 536 | |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 537 | bool Printed = false; |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 538 | bool missingNode = false; |
| 539 | |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 540 | while (!TempFCs.empty()) { |
| 541 | DSCallSite &CS = *TempFCs.begin(); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 542 | Instruction *TheCall = CS.getCallSite().getInstruction(); |
| 543 | DSGraph *GI; |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 544 | |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 545 | // Fast path for noop calls. Note that we don't care about merging globals |
| 546 | // in the callee with nodes in the caller here. |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 547 | if (CS.isDirectCall()) { |
| 548 | if (!isVAHackFn(CS.getCalleeFunc()) && isResolvableFunc(CS.getCalleeFunc())) { |
| 549 | Function* Callee = CS.getCalleeFunc(); |
Chris Lattner | eb144f5 | 2005-03-21 20:20:49 +0000 | [diff] [blame] | 550 | ActualCallees.insert(std::make_pair(TheCall, Callee)); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 551 | |
| 552 | assert(doneDSGraph(Callee) && "Direct calls should always be precomputed"); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 553 | GI = &getDSGraph(*Callee); // Graph to inline |
| 554 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName()); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 555 | DEBUG(std::cerr << "[" << GI->getGraphSize() << "+" |
| 556 | << GI->getAuxFunctionCalls().size() << "] into '" |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 557 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+" |
| 558 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 559 | Graph.mergeInGraph(CS, *Callee, *GI, |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 560 | DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes); |
| 561 | ++NumBUInlines; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 562 | } else { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 563 | DEBUG(std::cerr << "Graph " << Graph.getFunctionNames() << " Call Site " << |
| 564 | CS.getCallSite().getInstruction() << " never resolvable\n"); |
| 565 | } |
| 566 | --oldSize; |
| 567 | TempFCs.pop_front(); |
| 568 | continue; |
| 569 | } else { |
| 570 | std::vector<Function*> CalledFuncs; |
| 571 | bool resolved = GetAllCallees(CS, CalledFuncs); |
| 572 | |
| 573 | if (CalledFuncs.empty()) { |
| 574 | DEBUG(std::cerr << "Graph " << Graph.getFunctionNames() << " Call Site " << |
| 575 | CS.getCallSite().getInstruction() << " delayed\n"); |
| 576 | } else { |
| 577 | DEBUG( |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 578 | if (!Printed) |
| 579 | std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n"; |
| 580 | std::cerr << " calls " << CalledFuncs.size() |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 581 | << " fns from site: " << CS.getCallSite().getInstruction() |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 582 | << " " << *CS.getCallSite().getInstruction(); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 583 | std::cerr << " Fns ="; |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 584 | ); |
Chris Lattner | eb144f5 | 2005-03-21 20:20:49 +0000 | [diff] [blame] | 585 | unsigned NumPrinted = 0; |
| 586 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 587 | for (std::vector<Function*>::iterator I = CalledFuncs.begin(), |
Chris Lattner | eb144f5 | 2005-03-21 20:20:49 +0000 | [diff] [blame] | 588 | E = CalledFuncs.end(); I != E; ++I) { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 589 | DEBUG(if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();); |
Chris Lattner | eb144f5 | 2005-03-21 20:20:49 +0000 | [diff] [blame] | 590 | |
| 591 | // Add the call edges to the call graph. |
| 592 | ActualCallees.insert(std::make_pair(TheCall, *I)); |
| 593 | } |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 594 | DEBUG(std::cerr << "\n"); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 595 | |
| 596 | // See if we already computed a graph for this set of callees. |
| 597 | std::sort(CalledFuncs.begin(), CalledFuncs.end()); |
| 598 | std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph = |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 599 | (*IndCallGraphMap)[CalledFuncs]; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 600 | |
| 601 | if (IndCallGraph.first == 0) { |
| 602 | std::vector<Function*>::iterator I = CalledFuncs.begin(), |
| 603 | E = CalledFuncs.end(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 605 | // Start with a copy of the first graph. |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 606 | if (!doneDSGraph(*I)) { |
| 607 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin()); |
| 608 | missingNode = true; |
| 609 | continue; |
| 610 | } |
| 611 | |
| 612 | AddGlobalToNode(this, CS, *I); |
| 613 | |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 614 | GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 615 | GI->setGlobalsGraph(Graph.getGlobalsGraph()); |
| 616 | std::vector<DSNodeHandle> &Args = IndCallGraph.second; |
| 617 | |
| 618 | // Get the argument nodes for the first callee. The return value is |
| 619 | // the 0th index in the vector. |
| 620 | GI->getFunctionArgumentsForCall(*I, Args); |
| 621 | |
| 622 | // Merge all of the other callees into this graph. |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 623 | bool locMissing = false; |
| 624 | for (++I; I != E && !locMissing; ++I) { |
| 625 | AddGlobalToNode(this, CS, *I); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 626 | // If the graph already contains the nodes for the function, don't |
| 627 | // bother merging it in again. |
| 628 | if (!GI->containsFunction(*I)) { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 629 | if (!doneDSGraph(*I)) { |
| 630 | locMissing = true; |
| 631 | break; |
| 632 | } |
| 633 | |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 634 | GI->cloneInto(getDSGraph(**I)); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 635 | ++NumBUInlines; |
| 636 | } |
| 637 | |
| 638 | std::vector<DSNodeHandle> NextArgs; |
| 639 | GI->getFunctionArgumentsForCall(*I, NextArgs); |
| 640 | unsigned i = 0, e = Args.size(); |
| 641 | for (; i != e; ++i) { |
| 642 | if (i == NextArgs.size()) break; |
| 643 | Args[i].mergeWith(NextArgs[i]); |
| 644 | } |
| 645 | for (e = NextArgs.size(); i != e; ++i) |
| 646 | Args.push_back(NextArgs[i]); |
| 647 | } |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 648 | if (locMissing) { |
| 649 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin()); |
| 650 | missingNode = true; |
| 651 | continue; |
| 652 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 654 | // Clean up the final graph! |
| 655 | GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
| 656 | } else { |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 657 | DEBUG(std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n"); |
| 658 | for (std::vector<Function*>::iterator I = CalledFuncs.begin(), E = CalledFuncs.end(); I != E; ++I) { |
| 659 | AddGlobalToNode(this, CS, *I); |
| 660 | } |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | GI = IndCallGraph.first; |
| 664 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 665 | if (AlreadyInlined[CS.getCallSite()] != CalledFuncs) { |
| 666 | AlreadyInlined[CS.getCallSite()].swap(CalledFuncs); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 667 | |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 668 | // Merge the unified graph into this graph now. |
| 669 | DEBUG(std::cerr << " Inlining multi callee graph " |
| 670 | << "[" << GI->getGraphSize() << "+" |
| 671 | << GI->getAuxFunctionCalls().size() << "] into '" |
| 672 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+" |
| 673 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
| 674 | |
| 675 | Graph.mergeInGraph(CS, IndCallGraph.second, *GI, |
| 676 | DSGraph::StripAllocaBit | |
| 677 | DSGraph::DontCloneCallNodes); |
| 678 | |
| 679 | ++NumBUInlines; |
| 680 | } else { |
| 681 | DEBUG(std::cerr << " Skipping already inlined graph\n"); |
| 682 | } |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 683 | } |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 684 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin()); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 687 | |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 688 | // Recompute the Incomplete markers |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 689 | Graph.maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 690 | Graph.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 691 | |
| 692 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 693 | // reach live nodes as live. |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 694 | Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 696 | // When this graph is finalized, clone the globals in the graph into the |
| 697 | // globals graph to make sure it has everything, from all graphs. |
| 698 | DSScalarMap &MainSM = Graph.getScalarMap(); |
| 699 | ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit); |
| 700 | |
Chris Lattner | 3b7b81b | 2004-10-31 21:54:51 +0000 | [diff] [blame] | 701 | // Clone everything reachable from globals in the function graph into the |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 702 | // globals graph. |
| 703 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 704 | E = MainSM.global_end(); I != E; ++I) |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 705 | RC.getClonedNH(MainSM[*I]); |
| 706 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 707 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
Andrew Lenharth | ab390d0 | 2006-06-19 18:23:36 +0000 | [diff] [blame] | 708 | AuxCallsList.sort(); |
| 709 | AuxCallsList.unique(); |
| 710 | //conditionally prune the call list keeping only one copy of each actual |
| 711 | //CallSite |
| 712 | if (AuxCallsList.size() > 100) { |
| 713 | DEBUG(std::cerr << "Reducing Aux from " << AuxCallsList.size()); |
| 714 | std::map<CallSite, std::list<DSCallSite>::iterator> keepers; |
| 715 | TempFCs.swap(AuxCallsList); |
| 716 | for( std::list<DSCallSite>::iterator ii = TempFCs.begin(), ee = TempFCs.end(); |
| 717 | ii != ee; ++ii) |
| 718 | keepers[ii->getCallSite()] = ii; |
| 719 | for (std::map<CallSite, std::list<DSCallSite>::iterator>::iterator |
| 720 | ii = keepers.begin(), ee = keepers.end(); |
| 721 | ii != ee; ++ii) |
| 722 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, ii->second); |
| 723 | DEBUG(std::cerr << " to " << AuxCallsList.size() << "\n"); |
| 724 | } |
| 725 | return missingNode || oldSize != AuxCallsList.size(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 726 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 727 | |
| 728 | static const Function *getFnForValue(const Value *V) { |
| 729 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 730 | return I->getParent()->getParent(); |
| 731 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 732 | return A->getParent(); |
| 733 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 734 | return BB->getParent(); |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program. |
| 739 | /// These correspond to the interfaces defined in the AliasAnalysis class. |
| 740 | void BUDataStructures::deleteValue(Value *V) { |
| 741 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 742 | // If this is a function local value, just delete it from the scalar map! |
| 743 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 744 | return; |
| 745 | } |
| 746 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 747 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 748 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 749 | "cannot handle scc's"); |
| 750 | delete DSInfo[F]; |
| 751 | DSInfo.erase(F); |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 756 | } |
| 757 | |
| 758 | void BUDataStructures::copyValue(Value *From, Value *To) { |
| 759 | if (From == To) return; |
| 760 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 761 | // If this is a function local value, just delete it from the scalar map! |
| 762 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 767 | Function *ToF = cast<Function>(To); |
| 768 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 769 | DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs); |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 770 | DSInfo[ToF] = NG; |
| 771 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 772 | |
| 773 | // Change the Function* is the returnnodes map to the ToF. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 774 | DSNodeHandle Ret = NG->retnodes_begin()->second; |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 775 | NG->getReturnNodes().clear(); |
| 776 | NG->getReturnNodes()[ToF] = Ret; |
| 777 | return; |
| 778 | } |
| 779 | |
Chris Lattner | c5132e6 | 2005-03-24 04:22:04 +0000 | [diff] [blame] | 780 | if (const Function *F = getFnForValue(To)) { |
| 781 | DSGraph &G = getDSGraph(*F); |
| 782 | G.getScalarMap().copyScalarIfExists(From, To); |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | std::cerr << *From; |
| 787 | std::cerr << *To; |
| 788 | assert(0 && "Do not know how to copy this yet!"); |
| 789 | abort(); |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 790 | } |