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 | |
| 19 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 20 | // our memory... here... |
| 21 | // |
| 22 | void TDDataStructures::releaseMemory() { |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 23 | BUMaps.clear(); |
Chris Lattner | 13ec72a | 2002-10-21 13:31:48 +0000 | [diff] [blame] | 24 | for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 25 | E = DSInfo.end(); I != E; ++I) |
| 26 | delete I->second; |
| 27 | |
| 28 | // Empty map so next time memory is released, data structures are not |
| 29 | // re-deleted. |
| 30 | DSInfo.clear(); |
| 31 | } |
| 32 | |
| 33 | // run - Calculate the top down data structure graphs for each function in the |
| 34 | // program. |
| 35 | // |
| 36 | bool TDDataStructures::run(Module &M) { |
| 37 | // Simply calculate the graphs for each function... |
| 38 | for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I) |
| 39 | if (!I->isExternal()) |
| 40 | calculateGraph(*I); |
| 41 | return false; |
| 42 | } |
| 43 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 44 | /// ResolveCallSite - This method is used to link the actual arguments together |
| 45 | /// with the formal arguments for a function call in the top-down closure. This |
| 46 | /// method assumes that the call site arguments have been mapped into nodes |
| 47 | /// local to the specified graph. |
| 48 | /// |
| 49 | void TDDataStructures::ResolveCallSite(DSGraph &Graph, |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 50 | const DSCallSite &CallSite) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 51 | // Resolve all of the function formal arguments... |
| 52 | Function &F = Graph.getFunction(); |
| 53 | Function::aiterator AI = F.abegin(); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 54 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 55 | for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) { |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 56 | // Advance the argument iterator to the first pointer argument... |
| 57 | while (!DataStructureAnalysis::isPointerType(AI->getType())) ++AI; |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 59 | // TD ...Merge the formal arg scalar with the actual arg node |
| 60 | DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI); |
Chris Lattner | 99a2284 | 2002-10-21 15:04:18 +0000 | [diff] [blame] | 61 | assert(NodeForFormal.getNode() && "Pointer argument has no dest node!"); |
| 62 | NodeForFormal.mergeWith(CallSite.getPtrArg(i)); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Merge returned node in the caller with the "return" node in callee |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 66 | if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode()) |
| 67 | Graph.getRetNode().mergeWith(CallSite.getRetVal()); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 70 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 71 | DSGraph &TDDataStructures::calculateGraph(Function &F) { |
| 72 | // Make sure this graph has not already been calculated, or that we don't get |
| 73 | // into an infinite loop with mutually recursive functions. |
| 74 | // |
| 75 | DSGraph *&Graph = DSInfo[&F]; |
| 76 | if (Graph) return *Graph; |
| 77 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 78 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
| 79 | DSGraph &BUGraph = BU.getDSGraph(F); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 80 | |
| 81 | // Copy the BU graph, keeping a mapping from the BUGraph to the current Graph |
| 82 | std::map<const DSNode*, DSNode*> BUNodeMap; |
| 83 | Graph = new DSGraph(BUGraph, BUNodeMap); |
| 84 | |
| 85 | // Convert the mapping from a node-to-node map into a node-to-nodehandle map |
| 86 | BUMaps[&F].insert(BUNodeMap.begin(), BUNodeMap.end()); |
| 87 | BUNodeMap.clear(); // We are done with the temporary map. |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 13ec72a | 2002-10-21 13:31:48 +0000 | [diff] [blame] | 89 | const std::vector<DSCallSite> *CallSitesP = BU.getCallSites(F); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 90 | if (CallSitesP == 0) { |
| 91 | DEBUG(std::cerr << " [TD] No callers for: " << F.getName() << "\n"); |
| 92 | return *Graph; // If no call sites, the graph is the same as the BU graph! |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 95 | // Loop over all call sites of this function, merging each one into this |
| 96 | // graph. |
| 97 | // |
| 98 | DEBUG(std::cerr << " [TD] Inlining callers for: " << F.getName() << "\n"); |
Chris Lattner | 13ec72a | 2002-10-21 13:31:48 +0000 | [diff] [blame] | 99 | const std::vector<DSCallSite> &CallSites = *CallSitesP; |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 100 | for (unsigned c = 0, ce = CallSites.size(); c != ce; ++c) { |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 101 | const DSCallSite &CallSite = CallSites[c]; |
| 102 | Function &Caller = *CallSite.getResolvingCaller(); |
| 103 | assert(&Caller && !Caller.isExternal() && |
| 104 | "Externals function cannot 'call'!"); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 105 | |
| 106 | DEBUG(std::cerr << "\t [TD] Inlining caller #" << c << " '" |
| 107 | << Caller.getName() << "' into callee: " << F.getName() << "\n"); |
| 108 | |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 109 | if (&Caller == &F) { |
| 110 | // Self-recursive call: this can happen after a cycle of calls is inlined. |
| 111 | ResolveCallSite(*Graph, CallSite); |
| 112 | } else { |
| 113 | |
Chris Lattner | 99a2284 | 2002-10-21 15:04:18 +0000 | [diff] [blame] | 114 | // Recursively compute the graph for the Caller. It should be fully |
| 115 | // resolved except if there is mutual recursion... |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 116 | // |
| 117 | DSGraph &CG = calculateGraph(Caller); // Graph to inline |
| 118 | |
| 119 | DEBUG(std::cerr << "\t\t[TD] Got graph for " << Caller.getName() |
| 120 | << " in: " << F.getName() << "\n"); |
| 121 | |
| 122 | // These two maps keep track of where scalars in the old graph _used_ |
| 123 | // to point to, and of new nodes matching nodes of the old graph. |
| 124 | std::map<Value*, DSNodeHandle> OldValMap; |
| 125 | std::map<const DSNode*, DSNode*> OldNodeMap; |
| 126 | |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 127 | // Translate call site from having links into the BU graph |
| 128 | DSCallSite CallSiteInCG(CallSite, BUMaps[&Caller]); |
| 129 | |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 130 | // Clone the Caller's graph into the current graph, keeping |
| 131 | // track of where scalars in the old graph _used_ to point... |
| 132 | // Do this here because it only needs to happens once for each Caller! |
| 133 | // Strip scalars but not allocas since they are alive in callee. |
| 134 | // |
| 135 | DSNodeHandle RetVal = Graph->cloneInto(CG, OldValMap, OldNodeMap, |
| 136 | /*StripScalars*/ true, |
| 137 | /*StripAllocas*/ false, |
| 138 | /*CopyCallers*/ true, |
| 139 | /*CopyOrigCalls*/false); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 140 | ResolveCallSite(*Graph, DSCallSite(CallSiteInCG, OldNodeMap)); |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 143 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 144 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 145 | #if 0 |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 146 | Graph->maskIncompleteMarkers(); |
Chris Lattner | 19db049 | 2002-10-17 04:57:28 +0000 | [diff] [blame] | 147 | Graph->markIncompleteNodes(/*markFormals*/ !F.hasInternalLinkage() |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 148 | /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/); |
| 149 | Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false); |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame^] | 150 | #endif |
Chris Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 151 | DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " [" |
| 152 | << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size() |
| 153 | << "]\n"); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 154 | |
| 155 | return *Graph; |
| 156 | } |