Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 1 | //===- ComputeClosure.cpp - Implement interprocedural closing of graphs ---===// |
| 2 | // |
| 3 | // Compute the interprocedural closure of a data structure graph |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | // DEBUG_IP_CLOSURE - Define this to debug the act of linking up graphs |
| 8 | //#define DEBUG_IP_CLOSURE 1 |
| 9 | |
| 10 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | 0ac5429 | 2002-04-09 19:08:28 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 12 | #include "llvm/iOther.h" |
| 13 | #include "Support/STLExtras.h" |
| 14 | #include <algorithm> |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 16 | // Make all of the pointers that point to Val also point to N. |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 17 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 18 | static void copyEdgesFromTo(PointerVal Val, DSNode *N) { |
Chris Lattner | a13d6ce | 2002-04-01 22:20:48 +0000 | [diff] [blame] | 19 | unsigned ValIdx = Val.Index; |
| 20 | unsigned NLinks = N->getNumLinks(); |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 21 | |
Chris Lattner | a13d6ce | 2002-04-01 22:20:48 +0000 | [diff] [blame] | 22 | const vector<PointerValSet*> &PVSsToUpdate(Val.Node->getReferrers()); |
| 23 | for (unsigned i = 0, e = PVSsToUpdate.size(); i != e; ++i) { |
| 24 | // Loop over all of the pointers pointing to Val... |
| 25 | PointerValSet &PVS = *PVSsToUpdate[i]; |
| 26 | for (unsigned j = 0, je = PVS.size(); j != je; ++j) { |
| 27 | if (PVS[j].Node == Val.Node && PVS[j].Index >= ValIdx && |
| 28 | PVS[j].Index < ValIdx+NLinks) |
| 29 | PVS.add(PointerVal(N, PVS[j].Index-ValIdx)); |
Chris Lattner | a13d6ce | 2002-04-01 22:20:48 +0000 | [diff] [blame] | 30 | } |
| 31 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Chris Lattner | 212be2e | 2002-04-16 03:44:03 +0000 | [diff] [blame^] | 34 | static void ResolveNodesTo(const PointerValSet &FromVals, |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 35 | const PointerValSet &ToVals) { |
Chris Lattner | 212be2e | 2002-04-16 03:44:03 +0000 | [diff] [blame^] | 36 | // Only resolve the first pointer, although there many be many pointers here. |
| 37 | // The problem is that the inlined function might return one of the arguments |
| 38 | // to the function, and if so, extra values can be added to the arg or call |
| 39 | // node that point to what the other one got resolved to. Since these will |
| 40 | // be added to the end of the PVS pointed in, we just ignore them. |
| 41 | // |
| 42 | assert(!FromVals.empty() && "From should have at least a shadow node!"); |
| 43 | const PointerVal &FromPtr = FromVals[0]; |
| 44 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 45 | assert(FromPtr.Index == 0 && |
| 46 | "Resolved node return pointer should be index 0!"); |
Chris Lattner | cc0c1b2 | 2002-04-04 19:21:06 +0000 | [diff] [blame] | 47 | DSNode *N = FromPtr.Node; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 49 | // Make everything that pointed to the shadow node also point to the values in |
| 50 | // ToVals... |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 51 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 52 | for (unsigned i = 0, e = ToVals.size(); i != e; ++i) |
Chris Lattner | cc0c1b2 | 2002-04-04 19:21:06 +0000 | [diff] [blame] | 53 | copyEdgesFromTo(ToVals[i], N); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 55 | // Make everything that pointed to the shadow node now also point to the |
| 56 | // values it is equivalent to... |
Chris Lattner | cc0c1b2 | 2002-04-04 19:21:06 +0000 | [diff] [blame] | 57 | const vector<PointerValSet*> &PVSToUpdate(N->getReferrers()); |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 58 | for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i) |
| 59 | PVSToUpdate[i]->add(ToVals); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | // ResolveNodeTo - The specified node is now known to point to the set of values |
| 64 | // in ToVals, instead of the old shadow node subgraph that it was pointing to. |
| 65 | // |
| 66 | static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) { |
| 67 | assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!"); |
| 68 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 69 | const PointerValSet &PVS = Node->getLink(0); |
Chris Lattner | 212be2e | 2002-04-16 03:44:03 +0000 | [diff] [blame^] | 70 | ResolveNodesTo(PVS, ToVals); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // isResolvableCallNode - Return true if node is a call node and it is a call |
| 74 | // node that we can inline... |
| 75 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 76 | static bool isResolvableCallNode(CallDSNode *CN) { |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 77 | // Only operate on call nodes with direct method calls |
| 78 | Function *F = CN->getCall()->getCalledFunction(); |
| 79 | if (F == 0) return false; |
| 80 | |
| 81 | // Only work on call nodes with direct calls to methods with bodies. |
| 82 | return !F->isExternal(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // computeClosure - Replace all of the resolvable call nodes with the contents |
| 87 | // of their corresponding method data structure graph... |
| 88 | // |
| 89 | void FunctionDSGraph::computeClosure(const DataStructure &DS) { |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 90 | // Note that this cannot be a real vector because the keys will be changing |
| 91 | // as nodes are eliminated! |
| 92 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 93 | typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor; |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 94 | vector<pair<CallDescriptor, PointerValSet> > CallMap; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 96 | unsigned NumInlines = 0; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 97 | |
| 98 | // Loop over the resolvable call nodes... |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 99 | vector<CallDSNode*>::iterator NI; |
| 100 | NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode); |
| 101 | while (NI != CallNodes.end()) { |
| 102 | CallDSNode *CN = *NI; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 103 | Function *F = CN->getCall()->getCalledFunction(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 104 | |
Chris Lattner | cc0c1b2 | 2002-04-04 19:21:06 +0000 | [diff] [blame] | 105 | if (NumInlines++ == 100) { // CUTE hack huh? |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 106 | cerr << "Infinite (?) recursion halted\n"; |
| 107 | return; |
| 108 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 110 | CallNodes.erase(NI); // Remove the call node from the graph |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 112 | unsigned CallNodeOffset = NI-CallNodes.begin(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 114 | // Find out if we have already incorporated this node... if so, it will be |
| 115 | // in the CallMap... |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 116 | // |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 117 | |
| 118 | #if 0 |
| 119 | cerr << "\nSearching for: " << (void*)CN->getCall() << ": "; |
| 120 | for (unsigned X = 0; X != CN->getArgs().size(); ++X) { |
| 121 | cerr << " " << X << " is\n"; |
| 122 | CN->getArgs().first[X].print(cerr); |
| 123 | } |
| 124 | #endif |
| 125 | |
| 126 | const vector<PointerValSet> &Args = CN->getArgs(); |
| 127 | PointerValSet *CMI = 0; |
| 128 | for (unsigned i = 0, e = CallMap.size(); i != e; ++i) { |
| 129 | #if 0 |
| 130 | cerr << "Found: " << (void*)CallMap[i].first.second << ": "; |
| 131 | for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) { |
| 132 | cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | // Look to see if the function call takes a superset of the values we are |
| 137 | // providing as input |
| 138 | // |
| 139 | CallDescriptor &CD = CallMap[i].first; |
| 140 | if (CD.second == CN->getCall() && CD.first.size() == Args.size()) { |
| 141 | bool FoundMismatch = false; |
| 142 | for (unsigned j = 0, je = Args.size(); j != je; ++j) { |
| 143 | PointerValSet ArgSet = CD.first[j]; |
| 144 | if (ArgSet.add(Args[j])) { |
| 145 | FoundMismatch = true; break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!FoundMismatch) { CMI = &CallMap[i].second; break; } |
| 150 | } |
| 151 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 152 | |
| 153 | // Hold the set of values that correspond to the incorporated methods |
| 154 | // return set. |
| 155 | // |
| 156 | PointerValSet RetVals; |
| 157 | |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 158 | if (CMI) { |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 159 | // We have already inlined an identical function call! |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 160 | RetVals = *CMI; |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 161 | } else { |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 162 | // Get the datastructure graph for the new method. Note that we are not |
| 163 | // allowed to modify this graph because it will be the cached graph that |
| 164 | // is returned by other users that want the local datastructure graph for |
| 165 | // a method. |
| 166 | // |
| 167 | const FunctionDSGraph &NewFunction = DS.getDSGraph(F); |
| 168 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 169 | // StartNode - The first node of the incorporated graph, last node of the |
| 170 | // preexisting data structure graph... |
| 171 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 172 | unsigned StartAllocNode = AllocNodes.size(); |
Chris Lattner | ea4af65 | 2002-03-27 19:44:33 +0000 | [diff] [blame] | 173 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 174 | // Incorporate a copy of the called function graph into the current graph, |
| 175 | // allowing us to do local transformations to local graph to link |
| 176 | // arguments to call values, and call node to return value... |
| 177 | // |
Chris Lattner | 212be2e | 2002-04-16 03:44:03 +0000 | [diff] [blame^] | 178 | vector<PointerValSet> Args; |
| 179 | RetVals = cloneFunctionIntoSelf(NewFunction, false, Args); |
Chris Lattner | 26cfa66 | 2002-03-31 07:13:27 +0000 | [diff] [blame] | 180 | CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()), |
| 181 | RetVals)); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 183 | // If the call node has arguments, process them now! |
Chris Lattner | 212be2e | 2002-04-16 03:44:03 +0000 | [diff] [blame^] | 184 | assert(Args.size() == CN->getNumArgs() && |
| 185 | "Call node doesn't match function?"); |
| 186 | |
| 187 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 188 | // Now we make all of the nodes inside of the incorporated method |
| 189 | // point to the real arguments values, not to the shadow nodes for the |
| 190 | // argument. |
| 191 | ResolveNodesTo(Args[i], CN->getArgValues(i)); |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Loop through the nodes, deleting alloca nodes in the inlined function. |
| 195 | // Since the memory has been released, we cannot access their pointer |
| 196 | // fields (with defined results at least), so it is not possible to use |
| 197 | // any pointers to the alloca. Drop them now, and remove the alloca's |
| 198 | // since they are dead (we just removed all links to them). |
Chris Lattner | ea4af65 | 2002-03-27 19:44:33 +0000 | [diff] [blame] | 199 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 200 | for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i) |
| 201 | if (AllocNodes[i]->isAllocaNode()) { |
| 202 | AllocDSNode *NDS = AllocNodes[i]; |
| 203 | NDS->removeAllIncomingEdges(); // These edges are invalid now |
| 204 | delete NDS; // Node is dead |
| 205 | AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array |
| 206 | --i; // Don't skip the next node |
| 207 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // If the function returns a pointer value... Resolve values pointing to |
| 211 | // the shadow nodes pointed to by CN to now point the values in RetVals... |
| 212 | // |
| 213 | if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals); |
| 214 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 215 | // Now the call node is completely destructable. Eliminate it now. |
| 216 | delete CN; |
| 217 | |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 218 | bool Changed = true; |
| 219 | while (Changed) { |
| 220 | // Eliminate shadow nodes that are not distinguishable from some other |
| 221 | // node in the graph... |
| 222 | // |
Chris Lattner | 7d093d4 | 2002-03-28 19:16:48 +0000 | [diff] [blame] | 223 | Changed = UnlinkUndistinguishableNodes(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 224 | |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 225 | // Eliminate shadow nodes that are now extraneous due to linking... |
Chris Lattner | 7d093d4 | 2002-03-28 19:16:48 +0000 | [diff] [blame] | 226 | Changed |= RemoveUnreachableNodes(); |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 228 | |
| 229 | //if (F == Func) return; // Only do one self inlining |
| 230 | |
| 231 | // Move on to the next call node... |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 232 | NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 233 | } |
| 234 | } |