Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements the BUDataStructures class, which represents the |
| 4 | // Bottom-Up Interprocedural closure of the data structure graph over the |
| 5 | // program. This is useful for applications like pool allocation, but **not** |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 6 | // applications like alias analysis. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/DSGraph.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 13 | #include "Support/Statistic.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | using std::map; |
| 15 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 16 | static RegisterAnalysis<BUDataStructures> |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 17 | X("budatastructure", "Bottom-up Data Structure Analysis Closure"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 18 | |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 19 | using namespace DS; |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 20 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 21 | // run - Calculate the bottom up data structure graphs for each function in the |
| 22 | // program. |
| 23 | // |
| 24 | bool BUDataStructures::run(Module &M) { |
| 25 | GlobalsGraph = new DSGraph(); |
| 26 | |
| 27 | // Simply calculate the graphs for each function... |
| 28 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 29 | if (!I->isExternal()) |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 30 | calculateGraph(*I, 0); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 31 | return false; |
| 32 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 34 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 35 | // our memory... here... |
| 36 | // |
| 37 | void BUDataStructures::releaseMemory() { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 38 | for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 39 | E = DSInfo.end(); I != E; ++I) |
| 40 | delete I->second; |
| 41 | |
| 42 | // Empty map so next time memory is released, data structures are not |
| 43 | // re-deleted. |
| 44 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 45 | delete GlobalsGraph; |
| 46 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 49 | DSGraph &BUDataStructures::calculateGraph(Function &F, unsigned Indent) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 50 | // Make sure this graph has not already been calculated, or that we don't get |
| 51 | // into an infinite loop with mutually recursive functions. |
| 52 | // |
| 53 | DSGraph *&Graph = DSInfo[&F]; |
| 54 | if (Graph) return *Graph; |
| 55 | |
| 56 | // Copy the local version into DSInfo... |
| 57 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F)); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 58 | Graph->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 59 | Graph->setPrintAuxCalls(); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 61 | // Start resolving calls... |
Chris Lattner | 1a948a8 | 2002-11-08 21:25:24 +0000 | [diff] [blame] | 62 | std::vector<DSCallSite> &FCs = Graph->getAuxFunctionCalls(); |
| 63 | |
| 64 | // Start with a copy of the original call sites... |
| 65 | FCs = Graph->getFunctionCalls(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 66 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 67 | DEBUG(std::cerr << std::string(Indent*4, ' ') |
| 68 | << "[BU] Calculating graph for: " << F.getName() << "\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 70 | bool Inlined; |
| 71 | do { |
| 72 | Inlined = false; |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 74 | for (unsigned i = 0; i != FCs.size(); ++i) { |
| 75 | // Copy the call, because inlining graphs may invalidate the FCs vector. |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 76 | DSCallSite Call = FCs[i]; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 78 | // If the function list is complete... |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 79 | if ((Call.getCallee().getNode()->NodeType & DSNode::Incomplete)==0) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 80 | // Start inlining all of the functions we can... some may not be |
| 81 | // inlinable if they are external... |
| 82 | // |
Chris Lattner | 7836d60 | 2002-10-20 22:11:17 +0000 | [diff] [blame] | 83 | std::vector<GlobalValue*> Callees = |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 84 | Call.getCallee().getNode()->getGlobals(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 85 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 86 | unsigned OldNumCalls = FCs.size(); |
| 87 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 88 | // Loop over the functions, inlining whatever we can... |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 89 | for (unsigned c = 0; c != Callees.size(); ++c) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 90 | // Must be a function type, so this cast MUST succeed. |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 91 | Function &FI = cast<Function>(*Callees[c]); |
Chris Lattner | 613692c | 2002-10-17 04:24:08 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 93 | if (&FI == &F) { |
| 94 | // Self recursion... simply link up the formal arguments with the |
| 95 | // actual arguments... |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 96 | DEBUG(std::cerr << std::string(Indent*4, ' ') |
| 97 | << " [BU] Self Inlining: " << F.getName() << "\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 99 | // Handle self recursion by resolving the arguments and return value |
Chris Lattner | 460ea29 | 2002-11-07 07:06:20 +0000 | [diff] [blame] | 100 | Graph->mergeInGraph(Call, *Graph, DSGraph::StripAllocaBit); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 101 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 102 | // Erase the entry in the callees vector |
| 103 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 613692c | 2002-10-17 04:24:08 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 105 | } else if (!FI.isExternal()) { |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 106 | DEBUG(std::cerr << std::string(Indent*4, ' ') |
| 107 | << " [BU] In " << F.getName() << " inlining: " |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 108 | << FI.getName() << "\n"); |
Vikram S. Adve | c44e9bf | 2002-07-18 16:13:52 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 110 | // Get the data structure graph for the called function, closing it |
| 111 | // if possible (which is only impossible in the case of mutual |
| 112 | // recursion... |
| 113 | // |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 114 | DSGraph &GI = calculateGraph(FI, Indent+1); // Graph to inline |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 115 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 116 | DEBUG(std::cerr << std::string(Indent*4, ' ') |
| 117 | << " [BU] Got graph for " << FI.getName() |
| 118 | << " in: " << F.getName() << "[" << GI.getGraphSize() << "+" |
| 119 | << GI.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 076c1f9 | 2002-11-07 06:31:54 +0000 | [diff] [blame] | 121 | // Handle self recursion by resolving the arguments and return value |
Chris Lattner | 70925b0 | 2002-11-08 22:27:25 +0000 | [diff] [blame] | 122 | Graph->mergeInGraph(Call, GI, DSGraph::StripAllocaBit | |
| 123 | DSGraph::DontCloneCallNodes); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 124 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 125 | // Erase the entry in the Callees vector |
| 126 | Callees.erase(Callees.begin()+c--); |
| 127 | |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 128 | } else if (FI.getName() == "printf" || FI.getName() == "sscanf" || |
| 129 | FI.getName() == "fprintf" || FI.getName() == "open" || |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 130 | FI.getName() == "sprintf" || FI.getName() == "fputs") { |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 131 | // FIXME: These special cases (eg printf) should go away when we can |
| 132 | // define functions that take a variable number of arguments. |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 9267329 | 2002-11-02 00:13:20 +0000 | [diff] [blame] | 134 | // FIXME: at the very least, this should update mod/ref info |
Chris Lattner | 9eee58d | 2002-07-19 18:11:43 +0000 | [diff] [blame] | 135 | // Erase the entry in the globals vector |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 136 | Callees.erase(Callees.begin()+c--); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 140 | if (Callees.empty()) { // Inlined all of the function calls? |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 141 | // Erase the call if it is resolvable... |
| 142 | FCs.erase(FCs.begin()+i--); // Don't skip a the next call... |
| 143 | Inlined = true; |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 144 | } else if (Callees.size() != |
| 145 | Call.getCallee().getNode()->getGlobals().size()) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 146 | // Was able to inline SOME, but not all of the functions. Construct a |
| 147 | // new global node here. |
| 148 | // |
| 149 | assert(0 && "Unimpl!"); |
| 150 | Inlined = true; |
| 151 | } |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 152 | |
| 153 | // If we just inlined a function that had call nodes, chances are that |
| 154 | // the call nodes are redundant with ones we already have. Eliminate |
| 155 | // those call nodes now. |
| 156 | // |
| 157 | if (FCs.size() > OldNumCalls) |
| 158 | Graph->removeTriviallyDeadNodes(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 160 | |
| 161 | if (FCs.size() > 200) { |
| 162 | std::cerr << "Aborted inlining fn: '" << F.getName() << "'!" |
| 163 | << std::endl; |
| 164 | Graph->maskIncompleteMarkers(); |
| 165 | Graph->markIncompleteNodes(); |
| 166 | Graph->removeDeadNodes(); |
| 167 | Graph->writeGraphToFile(std::cerr, "crap."+F.getName()); |
| 168 | exit(1); |
| 169 | return *Graph; |
| 170 | } |
| 171 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Recompute the Incomplete markers. If there are any function calls left |
| 175 | // now that are complete, we must loop! |
| 176 | if (Inlined) { |
| 177 | Graph->maskIncompleteMarkers(); |
| 178 | Graph->markIncompleteNodes(); |
Chris Lattner | f40f0a3 | 2002-11-09 22:07:02 +0000 | [diff] [blame] | 179 | Graph->removeDeadNodes(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 182 | } while (Inlined && !FCs.empty()); |
| 183 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 184 | DEBUG(std::cerr << std::string(Indent*4, ' ') |
| 185 | << "[BU] Done inlining: " << F.getName() << " [" |
| 186 | << Graph->getGraphSize() << "+" << Graph->getAuxFunctionCalls().size() |
Chris Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 187 | << "]\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 188 | |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 189 | //Graph->writeGraphToFile(std::cerr, "bu_" + F.getName()); |
| 190 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 191 | return *Graph; |
| 192 | } |