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 |
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()) |
| 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 | 3d16290 | 2003-06-30 04:53:08 +0000 | [diff] [blame^] | 48 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 49 | E = DSInfo.end(); I != E; ++I) { |
| 50 | I->second->getReturnNodes().erase(I->first); |
| 51 | if (I->second->getReturnNodes().empty()) |
| 52 | delete I->second; |
| 53 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 54 | |
| 55 | // Empty map so next time memory is released, data structures are not |
| 56 | // re-deleted. |
| 57 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 58 | delete GlobalsGraph; |
| 59 | GlobalsGraph = 0; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 62 | #if 0 |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 63 | /// ResolveCallSite - This method is used to link the actual arguments together |
| 64 | /// with the formal arguments for a function call in the top-down closure. This |
| 65 | /// method assumes that the call site arguments have been mapped into nodes |
| 66 | /// local to the specified graph. |
| 67 | /// |
| 68 | void TDDataStructures::ResolveCallSite(DSGraph &Graph, |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 69 | const DSCallSite &CallSite) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 70 | // Resolve all of the function formal arguments... |
| 71 | Function &F = Graph.getFunction(); |
| 72 | Function::aiterator AI = F.abegin(); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 73 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 74 | for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 75 | // Advance the argument iterator to the first pointer argument... |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 76 | while (!DS::isPointerType(AI->getType())) ++AI; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 78 | // TD ...Merge the formal arg scalar with the actual arg node |
| 79 | DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI); |
Chris Lattner | 99a2284 | 2002-10-21 15:04:18 +0000 | [diff] [blame] | 80 | assert(NodeForFormal.getNode() && "Pointer argument has no dest node!"); |
| 81 | NodeForFormal.mergeWith(CallSite.getPtrArg(i)); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Merge returned node in the caller with the "return" node in callee |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 85 | if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode()) |
| 86 | Graph.getRetNode().mergeWith(CallSite.getRetVal()); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 87 | } |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 88 | #endif |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 90 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 91 | DSGraph *&G = DSInfo[&F]; |
| 92 | if (G == 0) { // Not created yet? Clone BU graph... |
| 93 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F)); |
| 94 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 95 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 96 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 97 | } |
| 98 | return *G; |
| 99 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 100 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 101 | |
| 102 | /// FunctionHasCompleteArguments - This function returns true if it is safe not |
| 103 | /// to mark arguments to the function complete. |
| 104 | /// |
| 105 | /// FIXME: Need to check if all callers have been found, or rather if a |
| 106 | /// funcpointer escapes! |
| 107 | /// |
| 108 | static bool FunctionHasCompleteArguments(Function &F) { |
| 109 | return F.hasInternalLinkage(); |
| 110 | } |
| 111 | |
| 112 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 113 | void TDDataStructures::calculateGraph(Function &F) { |
| 114 | // 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] | 115 | // into an infinite loop with mutually recursive functions. |
| 116 | // |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 117 | if (GraphDone.count(&F)) return; |
| 118 | GraphDone.insert(&F); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 120 | // Get the current functions graph... |
| 121 | DSGraph &Graph = getOrCreateDSGraph(F); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 123 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
| 124 | Graph.maskIncompleteMarkers(); |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 125 | unsigned Flags = FunctionHasCompleteArguments(F) ? |
| 126 | DSGraph::IgnoreFormalArgs : DSGraph::MarkFormalArgs; |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 127 | Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals); |
| 128 | Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
| 129 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 130 | const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls(); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 131 | if (CallSites.empty()) { |
| 132 | DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n"); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 133 | } else { |
| 134 | // Loop over all of the call sites, building a multi-map from Callees to |
| 135 | // DSCallSite*'s. With this map we can then loop over each callee, cloning |
| 136 | // this graph once into it, then resolving arguments. |
| 137 | // |
| 138 | std::multimap<Function*, const DSCallSite*> CalleeSites; |
| 139 | for (unsigned i = 0, e = CallSites.size(); i != e; ++i) { |
| 140 | const DSCallSite &CS = CallSites[i]; |
| 141 | if (CS.isDirectCall()) { |
| 142 | if (!CS.getCalleeFunc()->isExternal()) // If it's not external |
| 143 | CalleeSites.insert(std::make_pair(CS.getCalleeFunc(), &CS));// Keep it |
| 144 | } else { |
| 145 | const std::vector<GlobalValue*> &Callees = |
| 146 | CS.getCalleeNode()->getGlobals(); |
Chris Lattner | ce2d132 | 2002-11-08 22:26:43 +0000 | [diff] [blame] | 147 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 148 | // Loop over all of the functions that this call may invoke... |
| 149 | for (unsigned c = 0, e = Callees.size(); c != e; ++c) |
| 150 | if (Function *F = dyn_cast<Function>(Callees[c]))// If this is a fn... |
| 151 | if (!F->isExternal()) // If it's not extern |
| 152 | CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it! |
| 153 | } |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 155 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 156 | // Now that we have information about all of the callees, propagate the |
| 157 | // current graph into the callees. |
| 158 | // |
| 159 | DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into " |
| 160 | << CalleeSites.size() << " callees.\n"); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 161 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 162 | // Loop over all the callees... |
| 163 | for (std::multimap<Function*, const DSCallSite*>::iterator |
| 164 | I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ) |
| 165 | if (I->first == &F) { // Bottom-up pass takes care of self loops! |
| 166 | ++I; |
| 167 | } else { |
| 168 | // For each callee... |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 169 | Function &Callee = *I->first; |
| 170 | DSGraph &CG = getOrCreateDSGraph(Callee); // Get the callee's graph... |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 171 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 172 | DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee.getName() |
| 173 | << "'\n"); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 174 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 175 | // Clone our current graph into the callee... |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 176 | DSGraph::ScalarMapTy OldValMap; |
| 177 | DSGraph::NodeMapTy OldNodeMap; |
| 178 | DSGraph::ReturnNodesTy ReturnNodes; |
| 179 | CG.cloneInto(Graph, OldValMap, ReturnNodes, OldNodeMap, |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 180 | DSGraph::StripModRefBits | |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 181 | DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes | |
| 182 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 183 | OldValMap.clear(); // We don't care about the ValMap |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 184 | ReturnNodes.clear(); // We don't care about return values either |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 185 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 186 | // Loop over all of the invocation sites of the callee, resolving |
| 187 | // arguments to our graph. This loop may iterate multiple times if the |
| 188 | // current function calls this callee multiple times with different |
| 189 | // signatures. |
| 190 | // |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 191 | for (; I != E && I->first == &Callee; ++I) { |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 192 | // Map call site into callee graph |
| 193 | DSCallSite NewCS(*I->second, OldNodeMap); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 194 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 195 | // Resolve the return values... |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 196 | NewCS.getRetVal().mergeWith(CG.getReturnNodeFor(Callee)); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 197 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 198 | // Resolve all of the arguments... |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 199 | Function::aiterator AI = Callee.abegin(); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 200 | for (unsigned i = 0, e = NewCS.getNumPtrArgs(); |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 201 | i != e && AI != Callee.aend(); ++i, ++AI) { |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 202 | // Advance the argument iterator to the first pointer argument... |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 203 | while (AI != Callee.aend() && !DS::isPointerType(AI->getType())) |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 204 | ++AI; |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 205 | if (AI == Callee.aend()) break; |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 206 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 207 | // Add the link from the argument scalar to the provided value |
| 208 | DSNodeHandle &NH = CG.getNodeForValue(AI); |
| 209 | assert(NH.getNode() && "Pointer argument without scalarmap entry?"); |
| 210 | NH.mergeWith(NewCS.getPtrArg(i)); |
| 211 | } |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 213 | |
| 214 | // Done with the nodemap... |
| 215 | OldNodeMap.clear(); |
| 216 | |
| 217 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 218 | CG.removeTriviallyDeadNodes(); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 219 | CG.maskIncompleteMarkers(); |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 220 | CG.markIncompleteNodes(DSGraph::MarkFormalArgs |DSGraph::IgnoreGlobals); |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 221 | CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 224 | DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName() |
| 225 | << " [" << Graph.getGraphSize() << "+" |
| 226 | << Graph.getFunctionCalls().size() << "]\n"); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 227 | |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 228 | // Loop over all the callees... making sure they are all resolved now... |
| 229 | Function *LastFunc = 0; |
| 230 | for (std::multimap<Function*, const DSCallSite*>::iterator |
| 231 | I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I) |
| 232 | if (I->first != LastFunc) { // Only visit each callee once... |
| 233 | LastFunc = I->first; |
| 234 | calculateGraph(*I->first); |
| 235 | } |
| 236 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 238 | |