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