Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 1 | //===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===// |
| 2 | // |
| 3 | // This file implements the TDDataStructures class, which represents the |
| 4 | // Top-down Interprocedural closure of the data structure graph over the |
| 5 | // program. This is useful (but not strictly necessary?) for applications |
| 6 | // like pointer analysis. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/DataStructure.h" |
| 11 | #include "llvm/Module.h" |
| 12 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 13 | #include "Support/Statistic.h" |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 14 | #include "DSCallSiteIterator.h" |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 16 | namespace { |
| 17 | RegisterAnalysis<TDDataStructures> // Register the pass |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 18 | Y("tddatastructure", "Top-down Data Structure Analysis"); |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 19 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 20 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 21 | // run - Calculate the top down data structure graphs for each function in the |
| 22 | // program. |
| 23 | // |
| 24 | bool TDDataStructures::run(Module &M) { |
| 25 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 26 | GlobalsGraph = new DSGraph(BU.getGlobalsGraph()); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 27 | |
| 28 | // Calculate top-down from main... |
| 29 | if (Function *F = M.getMainFunction()) |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 30 | calculateGraphFrom(*F); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 31 | |
| 32 | // Next calculate the graphs for each function unreachable function... |
| 33 | for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I) |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 34 | if (!I->isExternal() && !DSInfo.count(&*I)) |
| 35 | calculateGraphFrom(*I); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 36 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 40 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 41 | // our memory... here... |
| 42 | // |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 43 | // FIXME: This should be releaseMemory and will work fine, except that LoadVN |
| 44 | // has no way to extend the lifetime of the pass, which screws up ds-aa. |
| 45 | // |
| 46 | void TDDataStructures::releaseMyMemory() { |
Chris Lattner | 3d16290 | 2003-06-30 04:53:08 +0000 | [diff] [blame] | 47 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 48 | E = DSInfo.end(); I != E; ++I) { |
| 49 | I->second->getReturnNodes().erase(I->first); |
| 50 | if (I->second->getReturnNodes().empty()) |
| 51 | delete I->second; |
| 52 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 53 | |
| 54 | // Empty map so next time memory is released, data structures are not |
| 55 | // re-deleted. |
| 56 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 57 | delete GlobalsGraph; |
| 58 | GlobalsGraph = 0; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 62 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 63 | DSGraph *&G = DSInfo[&F]; |
| 64 | if (G == 0) { // Not created yet? Clone BU graph... |
| 65 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F)); |
| 66 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 67 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 68 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 69 | } |
| 70 | return *G; |
| 71 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 72 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 73 | |
| 74 | /// FunctionHasCompleteArguments - This function returns true if it is safe not |
| 75 | /// to mark arguments to the function complete. |
| 76 | /// |
| 77 | /// FIXME: Need to check if all callers have been found, or rather if a |
| 78 | /// funcpointer escapes! |
| 79 | /// |
| 80 | static bool FunctionHasCompleteArguments(Function &F) { |
| 81 | return F.hasInternalLinkage(); |
| 82 | } |
| 83 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 84 | void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited, |
| 85 | std::vector<DSGraph*> &PostOrder, |
| 86 | const BUDataStructures::ActualCalleesTy &ActualCallees) { |
| 87 | if (F.isExternal()) return; |
| 88 | DSGraph &G = getOrCreateDSGraph(F); |
| 89 | if (Visited.count(&G)) return; |
| 90 | Visited.insert(&G); |
| 91 | |
| 92 | // Recursively traverse all of the callee graphs. |
| 93 | const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls(); |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 95 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) { |
| 96 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
| 97 | BUDataStructures::ActualCalleesTy::const_iterator> |
| 98 | IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst()); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 100 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 101 | I != IP.second; ++I) |
| 102 | ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees); |
| 103 | } |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 105 | PostOrder.push_back(&G); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | void TDDataStructures::calculateGraphFrom(Function &F) { |
| 111 | // We want to traverse the call graph in reverse post-order. To do this, we |
| 112 | // calculate a post-order traversal, then reverse it. |
| 113 | hash_set<DSGraph*> VisitedGraph; |
| 114 | std::vector<DSGraph*> PostOrder; |
| 115 | ComputePostOrder(F, VisitedGraph, PostOrder, |
| 116 | getAnalysis<BUDataStructures>().getActualCallees()); |
| 117 | VisitedGraph.clear(); // Release memory! |
| 118 | |
| 119 | // Visit each of the graphs in reverse post-order now! |
| 120 | while (!PostOrder.empty()) { |
| 121 | inlineGraphIntoCallees(*PostOrder.back()); |
| 122 | PostOrder.pop_back(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) { |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 128 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
| 129 | Graph.maskIncompleteMarkers(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 130 | unsigned Flags = true /* FIXME!! FunctionHasCompleteArguments(F)*/ ? |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 131 | DSGraph::IgnoreFormalArgs : DSGraph::MarkFormalArgs; |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 132 | Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals); |
| 133 | Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
| 134 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 135 | DSCallSiteIterator CalleeI = DSCallSiteIterator::begin_std(Graph); |
| 136 | DSCallSiteIterator CalleeE = DSCallSiteIterator::end_std(Graph); |
Chris Lattner | ce2d132 | 2002-11-08 22:26:43 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 138 | if (CalleeI == CalleeE) { |
| 139 | DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames() |
| 140 | << "\n"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Loop over all of the call sites, building a multi-map from Callees to |
| 145 | // DSCallSite*'s. With this map we can then loop over each callee, cloning |
| 146 | // this graph once into it, then resolving arguments. |
| 147 | // |
| 148 | std::multimap<std::pair<DSGraph*,Function*>, const DSCallSite*> CalleeSites; |
| 149 | for (; CalleeI != CalleeE; ++CalleeI) |
| 150 | if (!(*CalleeI)->isExternal()) { |
| 151 | // We should have already created the graph here... |
| 152 | if (!DSInfo.count(*CalleeI)) |
| 153 | std::cerr << "WARNING: TD pass, did not know about callee: '" |
| 154 | << (*CalleeI)->getName() << "'\n"; |
| 155 | |
| 156 | DSGraph &IG = getOrCreateDSGraph(**CalleeI); |
| 157 | if (&IG != &Graph) |
| 158 | CalleeSites.insert(std::make_pair(std::make_pair(&IG, *CalleeI), |
| 159 | &CalleeI.getCallSite())); |
| 160 | } |
| 161 | |
| 162 | // Now that we have information about all of the callees, propagate the |
| 163 | // current graph into the callees. |
| 164 | // |
| 165 | DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into " |
| 166 | << CalleeSites.size() << " callees.\n"); |
| 167 | |
| 168 | // Loop over all the callees... |
| 169 | for (std::multimap<std::pair<DSGraph*, Function*>, |
| 170 | const DSCallSite*>::iterator I = CalleeSites.begin(), |
| 171 | E = CalleeSites.end(); I != E; ) { |
| 172 | DSGraph &CG = *I->first.first; |
| 173 | |
| 174 | DEBUG(std::cerr << " [TD] Inlining graph into callee graph '" |
| 175 | << CG.getFunctionNames() << "'\n"); |
| 176 | |
| 177 | // Clone our current graph into the callee... |
| 178 | DSGraph::ScalarMapTy OldValMap; |
| 179 | DSGraph::NodeMapTy OldNodeMap; |
| 180 | DSGraph::ReturnNodesTy ReturnNodes; |
| 181 | CG.cloneInto(Graph, OldValMap, ReturnNodes, OldNodeMap, |
| 182 | DSGraph::StripModRefBits | |
| 183 | DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes | |
| 184 | DSGraph::DontCloneAuxCallNodes); |
| 185 | OldValMap.clear(); // We don't care about the ValMap |
| 186 | ReturnNodes.clear(); // We don't care about return values either |
| 187 | |
| 188 | // Loop over all of the invocation sites of the callee, resolving |
| 189 | // arguments to our graph. This loop may iterate multiple times if the |
| 190 | // current function calls this callee multiple times with different |
| 191 | // signatures. |
| 192 | // |
| 193 | for (; I != E && I->first.first == &CG; ++I) { |
| 194 | Function &Callee = *I->first.second; |
| 195 | DEBUG(std::cerr << "\t [TD] Merging args for callee '" |
| 196 | << Callee.getName() << "'\n"); |
| 197 | |
| 198 | // Map call site into callee graph |
| 199 | DSCallSite NewCS(*I->second, OldNodeMap); |
| 200 | |
| 201 | // Resolve the return values... |
| 202 | NewCS.getRetVal().mergeWith(CG.getReturnNodeFor(Callee)); |
| 203 | |
| 204 | // Resolve all of the arguments... |
| 205 | Function::aiterator AI = Callee.abegin(); |
| 206 | for (unsigned i = 0, e = NewCS.getNumPtrArgs(); |
| 207 | i != e && AI != Callee.aend(); ++i, ++AI) { |
| 208 | // Advance the argument iterator to the first pointer argument... |
| 209 | while (AI != Callee.aend() && !DS::isPointerType(AI->getType())) |
| 210 | ++AI; |
| 211 | if (AI == Callee.aend()) break; |
| 212 | |
| 213 | // Add the link from the argument scalar to the provided value |
| 214 | DSNodeHandle &NH = CG.getNodeForValue(AI); |
| 215 | assert(NH.getNode() && "Pointer argument without scalarmap entry?"); |
| 216 | NH.mergeWith(NewCS.getPtrArg(i)); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 220 | // Done with the nodemap... |
| 221 | OldNodeMap.clear(); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 223 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
| 224 | CG.removeTriviallyDeadNodes(); |
| 225 | //CG.maskIncompleteMarkers(); |
| 226 | //CG.markIncompleteNodes(DSGraph::MarkFormalArgs | DSGraph::IgnoreGlobals); |
| 227 | //CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame^] | 229 | |
| 230 | DEBUG(std::cerr << " [TD] Done inlining into callees for: " |
| 231 | << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+" |
| 232 | << Graph.getFunctionCalls().size() << "]\n"); |
| 233 | |
| 234 | #if 0 |
| 235 | // Loop over all the callees... making sure they are all resolved now... |
| 236 | Function *LastFunc = 0; |
| 237 | for (std::multimap<Function*, const DSCallSite*>::iterator |
| 238 | I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I) |
| 239 | if (I->first != LastFunc) { // Only visit each callee once... |
| 240 | LastFunc = I->first; |
| 241 | calculateGraph(*I->first); |
| 242 | } |
| 243 | #endif |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 244 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 245 | |