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