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 | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 86 | typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor; |
| 87 | map<CallDescriptor, PointerValSet> CallMap; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 89 | unsigned NumInlines = 0; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 90 | |
| 91 | // Loop over the resolvable call nodes... |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 92 | vector<CallDSNode*>::iterator NI; |
| 93 | NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode); |
| 94 | while (NI != CallNodes.end()) { |
| 95 | CallDSNode *CN = *NI; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 96 | Function *F = CN->getCall()->getCalledFunction(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 98 | if (NumInlines++ == 30) { // CUTE hack huh? |
| 99 | cerr << "Infinite (?) recursion halted\n"; |
| 100 | return; |
| 101 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 103 | CallNodes.erase(NI); // Remove the call node from the graph |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 105 | unsigned CallNodeOffset = NI-CallNodes.begin(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 107 | // Find out if we have already incorporated this node... if so, it will be |
| 108 | // in the CallMap... |
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 | CallDescriptor FDesc(CN->getArgs(), CN->getCall()); |
| 111 | map<CallDescriptor, PointerValSet>::iterator CMI = CallMap.find(FDesc); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 112 | |
| 113 | // Hold the set of values that correspond to the incorporated methods |
| 114 | // return set. |
| 115 | // |
| 116 | PointerValSet RetVals; |
| 117 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 118 | if (CMI != CallMap.end()) { |
| 119 | // We have already inlined an identical function call! |
| 120 | RetVals = CMI->second; |
| 121 | } else { |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 122 | // Get the datastructure graph for the new method. Note that we are not |
| 123 | // allowed to modify this graph because it will be the cached graph that |
| 124 | // is returned by other users that want the local datastructure graph for |
| 125 | // a method. |
| 126 | // |
| 127 | const FunctionDSGraph &NewFunction = DS.getDSGraph(F); |
| 128 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 129 | // StartNode - The first node of the incorporated graph, last node of the |
| 130 | // preexisting data structure graph... |
| 131 | // |
| 132 | unsigned StartArgNode = ArgNodes.size(); |
| 133 | unsigned StartAllocNode = AllocNodes.size(); |
Chris Lattner | ea4af65 | 2002-03-27 19:44:33 +0000 | [diff] [blame] | 134 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 135 | // Incorporate a copy of the called function graph into the current graph, |
| 136 | // allowing us to do local transformations to local graph to link |
| 137 | // arguments to call values, and call node to return value... |
| 138 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 139 | RetVals = cloneFunctionIntoSelf(NewFunction, F == Func); |
| 140 | CallMap[FDesc] = RetVals; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 142 | // If the call node has arguments, process them now! |
| 143 | if (CN->getNumArgs()) { |
| 144 | // The ArgNodes of the incorporated graph should be the nodes starting |
| 145 | // at StartNode, ordered the same way as the call arguments. The arg |
| 146 | // nodes are seperated by a single shadow node, but that shadow node |
| 147 | // might get eliminated in the process of optimization. |
| 148 | // |
| 149 | for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) { |
| 150 | // Get the arg node of the incorporated method... |
| 151 | ArgDSNode *ArgNode = ArgNodes[StartArgNode]; |
| 152 | |
| 153 | // Now we make all of the nodes inside of the incorporated method |
| 154 | // point to the real arguments values, not to the shadow nodes for the |
| 155 | // argument. |
| 156 | // |
| 157 | ResolveNodeTo(ArgNode, CN->getArgValues(i)); |
| 158 | |
| 159 | // Remove the argnode from the set of nodes in this method... |
| 160 | ArgNodes.erase(ArgNodes.begin()+StartArgNode); |
| 161 | |
| 162 | // ArgNode is no longer useful, delete now! |
| 163 | delete ArgNode; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Loop through the nodes, deleting alloca nodes in the inlined function. |
| 168 | // Since the memory has been released, we cannot access their pointer |
| 169 | // fields (with defined results at least), so it is not possible to use |
| 170 | // any pointers to the alloca. Drop them now, and remove the alloca's |
| 171 | // since they are dead (we just removed all links to them). |
Chris Lattner | ea4af65 | 2002-03-27 19:44:33 +0000 | [diff] [blame] | 172 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 173 | for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i) |
| 174 | if (AllocNodes[i]->isAllocaNode()) { |
| 175 | AllocDSNode *NDS = AllocNodes[i]; |
| 176 | NDS->removeAllIncomingEdges(); // These edges are invalid now |
| 177 | delete NDS; // Node is dead |
| 178 | AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array |
| 179 | --i; // Don't skip the next node |
| 180 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // If the function returns a pointer value... Resolve values pointing to |
| 184 | // the shadow nodes pointed to by CN to now point the values in RetVals... |
| 185 | // |
| 186 | if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals); |
| 187 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 188 | // Now the call node is completely destructable. Eliminate it now. |
| 189 | delete CN; |
| 190 | |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 191 | bool Changed = true; |
| 192 | while (Changed) { |
| 193 | // Eliminate shadow nodes that are not distinguishable from some other |
| 194 | // node in the graph... |
| 195 | // |
Chris Lattner | 7d093d4 | 2002-03-28 19:16:48 +0000 | [diff] [blame] | 196 | Changed = UnlinkUndistinguishableNodes(); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 197 | |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 198 | // Eliminate shadow nodes that are now extraneous due to linking... |
Chris Lattner | 7d093d4 | 2002-03-28 19:16:48 +0000 | [diff] [blame] | 199 | Changed |= RemoveUnreachableNodes(); |
Chris Lattner | df8af1c | 2002-03-27 00:53:57 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 201 | |
| 202 | //if (F == Func) return; // Only do one self inlining |
| 203 | |
| 204 | // Move on to the next call node... |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 205 | NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 206 | } |
| 207 | } |