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