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