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