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" |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/DSGraph.h" |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
| 13 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 14 | #include "Support/Statistic.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 |
| 18 | Y("tddatastructure", "Top-down Data Structure Analysis Closure"); |
| 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>(); |
| 26 | GlobalsGraph = new DSGraph(); |
| 27 | |
| 28 | // Calculate top-down from main... |
| 29 | if (Function *F = M.getMainFunction()) |
| 30 | calculateGraph(*F); |
| 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) |
| 34 | if (!I->isExternal()) |
| 35 | calculateGraph(*I); |
| 36 | |
| 37 | GraphDone.clear(); // Free temporary memory... |
| 38 | return false; |
| 39 | } |
| 40 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 41 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 42 | // our memory... here... |
| 43 | // |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 44 | // FIXME: This should be releaseMemory and will work fine, except that LoadVN |
| 45 | // has no way to extend the lifetime of the pass, which screws up ds-aa. |
| 46 | // |
| 47 | void TDDataStructures::releaseMyMemory() { |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 48 | for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 49 | E = DSInfo.end(); I != E; ++I) |
| 50 | delete I->second; |
| 51 | |
| 52 | // Empty map so next time memory is released, data structures are not |
| 53 | // re-deleted. |
| 54 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 55 | delete GlobalsGraph; |
| 56 | GlobalsGraph = 0; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 59 | /// ResolveCallSite - This method is used to link the actual arguments together |
| 60 | /// with the formal arguments for a function call in the top-down closure. This |
| 61 | /// method assumes that the call site arguments have been mapped into nodes |
| 62 | /// local to the specified graph. |
| 63 | /// |
| 64 | void TDDataStructures::ResolveCallSite(DSGraph &Graph, |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 65 | const DSCallSite &CallSite) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 66 | // Resolve all of the function formal arguments... |
| 67 | Function &F = Graph.getFunction(); |
| 68 | Function::aiterator AI = F.abegin(); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 69 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 70 | for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 71 | // Advance the argument iterator to the first pointer argument... |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 72 | while (!DS::isPointerType(AI->getType())) ++AI; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 74 | // TD ...Merge the formal arg scalar with the actual arg node |
| 75 | DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI); |
Chris Lattner | 99a2284 | 2002-10-21 15:04:18 +0000 | [diff] [blame] | 76 | assert(NodeForFormal.getNode() && "Pointer argument has no dest node!"); |
| 77 | NodeForFormal.mergeWith(CallSite.getPtrArg(i)); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Merge returned node in the caller with the "return" node in callee |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 81 | if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode()) |
| 82 | Graph.getRetNode().mergeWith(CallSite.getRetVal()); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 85 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 86 | DSGraph *&G = DSInfo[&F]; |
| 87 | if (G == 0) { // Not created yet? Clone BU graph... |
| 88 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F)); |
| 89 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 90 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 91 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 92 | } |
| 93 | return *G; |
| 94 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 96 | void TDDataStructures::calculateGraph(Function &F) { |
| 97 | // Make sure this graph has not already been calculated, and that we don't get |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 98 | // into an infinite loop with mutually recursive functions. |
| 99 | // |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 100 | if (GraphDone.count(&F)) return; |
| 101 | GraphDone.insert(&F); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 103 | // Get the current functions graph... |
| 104 | DSGraph &Graph = getOrCreateDSGraph(F); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 106 | const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls(); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 107 | if (CallSites.empty()) { |
| 108 | DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n"); |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 109 | return; // If no call sites, there is nothing more to do here |
Chris Lattner | 4bdb9b7 | 2002-10-22 16:01:03 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | ce2d132 | 2002-11-08 22:26:43 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 112 | // Loop over all of the call sites, building a multi-map from Callees to |
| 113 | // DSCallSite*'s. With this map we can then loop over each callee, cloning |
| 114 | // this graph once into it, then resolving arguments. |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 115 | // |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 116 | std::multimap<Function*, const DSCallSite*> CalleeSites; |
| 117 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { |
| 118 | const DSCallSite &CS = CallSites[i]; |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame^] | 119 | if (CS.isDirectCall()) { |
| 120 | if (!CS.getCalleeFunc()->isExternal()) // If it's not external |
| 121 | CalleeSites.insert(std::make_pair(CS.getCalleeFunc(), &CS)); // Keep it |
| 122 | } else { |
| 123 | const std::vector<GlobalValue*> &Callees = |
| 124 | CS.getCalleeNode()->getGlobals(); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame^] | 126 | // Loop over all of the functions that this call may invoke... |
| 127 | for (unsigned c = 0, e = Callees.size(); c != e; ++c) |
| 128 | if (Function *F = dyn_cast<Function>(Callees[c])) // If this is a fn... |
| 129 | if (!F->isExternal()) // If it's not extern |
| 130 | CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it! |
| 131 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame^] | 134 | // Now that we have information about all of the callees, propagate the |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 135 | // current graph into the callees. |
| 136 | // |
| 137 | DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into " |
| 138 | << CalleeSites.size() << " callees.\n"); |
| 139 | |
| 140 | // Loop over all the callees... |
| 141 | for (std::multimap<Function*, const DSCallSite*>::iterator |
| 142 | I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ) |
| 143 | if (I->first == &F) { // Bottom-up pass takes care of self loops! |
| 144 | ++I; |
| 145 | } else { |
| 146 | // For each callee... |
| 147 | Function *Callee = I->first; |
| 148 | DSGraph &CG = getOrCreateDSGraph(*Callee); // Get the callee's graph... |
| 149 | |
| 150 | DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee->getName() |
| 151 | << "'\n"); |
| 152 | |
| 153 | // Clone our current graph into the callee... |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 154 | hash_map<Value*, DSNodeHandle> OldValMap; |
| 155 | hash_map<const DSNode*, DSNodeHandle> OldNodeMap; |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 156 | CG.cloneInto(Graph, OldValMap, OldNodeMap, |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 157 | DSGraph::StripModRefBits | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 158 | DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes); |
| 159 | OldValMap.clear(); // We don't care about the ValMap |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 161 | // Loop over all of the invocation sites of the callee, resolving |
| 162 | // arguments to our graph. This loop may iterate multiple times if the |
| 163 | // current function calls this callee multiple times with different |
| 164 | // signatures. |
| 165 | // |
| 166 | for (; I != E && I->first == Callee; ++I) { |
| 167 | // Map call site into callee graph |
| 168 | DSCallSite NewCS(*I->second, OldNodeMap); |
| 169 | |
| 170 | // Resolve the return values... |
| 171 | NewCS.getRetVal().mergeWith(CG.getRetNode()); |
| 172 | |
| 173 | // Resolve all of the arguments... |
| 174 | Function::aiterator AI = Callee->abegin(); |
Chris Lattner | 0ecc426 | 2002-11-11 21:36:05 +0000 | [diff] [blame] | 175 | for (unsigned i = 0, e = NewCS.getNumPtrArgs(); |
| 176 | i != e && AI != Callee->aend(); ++i, ++AI) { |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 177 | // Advance the argument iterator to the first pointer argument... |
| 178 | while (!DS::isPointerType(AI->getType())) { |
| 179 | ++AI; |
| 180 | #ifndef NDEBUG |
| 181 | if (AI == Callee->aend()) |
| 182 | std::cerr << "Bad call to Function: " << Callee->getName()<< "\n"; |
| 183 | #endif |
| 184 | assert(AI != Callee->aend() && |
| 185 | "# Args provided is not # Args required!"); |
| 186 | } |
| 187 | |
| 188 | // Add the link from the argument scalar to the provided value |
| 189 | DSNodeHandle &NH = CG.getNodeForValue(AI); |
| 190 | assert(NH.getNode() && "Pointer argument without scalarmap entry?"); |
| 191 | NH.mergeWith(NewCS.getPtrArg(i)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Done with the nodemap... |
| 196 | OldNodeMap.clear(); |
| 197 | |
| 198 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
| 199 | CG.maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 200 | CG.markIncompleteNodes(F.hasInternalLinkage() ? DSGraph::IgnoreFormalArgs: |
| 201 | DSGraph::MarkFormalArgs |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 202 | /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 203 | CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | 4bdb9b7 | 2002-10-22 16:01:03 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 206 | DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName() |
| 207 | << " [" << Graph.getGraphSize() << "+" |
| 208 | << Graph.getFunctionCalls().size() << "]\n"); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 210 | |
| 211 | // Loop over all the callees... making sure they are all resolved now... |
| 212 | Function *LastFunc = 0; |
| 213 | for (std::multimap<Function*, const DSCallSite*>::iterator |
| 214 | I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I) |
| 215 | if (I->first != LastFunc) { // Only visit each callee once... |
| 216 | LastFunc = I->first; |
| 217 | calculateGraph(*I->first); |
| 218 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 220 | |