blob: 67309e8c77d18e9c08d1df2321d8f5355c7893b0 [file] [log] [blame]
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00001//===- 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 Lattner0ac54292002-04-09 19:08:28 +000011#include "llvm/Function.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000012#include "llvm/iOther.h"
13#include "Support/STLExtras.h"
14#include <algorithm>
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000015
Chris Lattner1120c8b2002-03-28 17:56:03 +000016// Make all of the pointers that point to Val also point to N.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000017//
Chris Lattner1120c8b2002-03-28 17:56:03 +000018static void copyEdgesFromTo(PointerVal Val, DSNode *N) {
Chris Lattnera13d6ce2002-04-01 22:20:48 +000019 unsigned ValIdx = Val.Index;
20 unsigned NLinks = N->getNumLinks();
Chris Lattner1120c8b2002-03-28 17:56:03 +000021
Chris Lattnera13d6ce2002-04-01 22:20:48 +000022 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 Lattnera13d6ce2002-04-01 22:20:48 +000030 }
31 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000032}
33
Chris Lattner212be2e2002-04-16 03:44:03 +000034static void ResolveNodesTo(const PointerValSet &FromVals,
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000035 const PointerValSet &ToVals) {
Chris Lattner212be2e2002-04-16 03:44:03 +000036 // 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 Lattnerbb2a28f2002-03-26 22:39:06 +000045 assert(FromPtr.Index == 0 &&
46 "Resolved node return pointer should be index 0!");
Chris Lattnercc0c1b22002-04-04 19:21:06 +000047 DSNode *N = FromPtr.Node;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000048
Chris Lattner1120c8b2002-03-28 17:56:03 +000049 // Make everything that pointed to the shadow node also point to the values in
50 // ToVals...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000051 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000052 for (unsigned i = 0, e = ToVals.size(); i != e; ++i)
Chris Lattnercc0c1b22002-04-04 19:21:06 +000053 copyEdgesFromTo(ToVals[i], N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000054
Chris Lattner1120c8b2002-03-28 17:56:03 +000055 // Make everything that pointed to the shadow node now also point to the
56 // values it is equivalent to...
Chris Lattnercc0c1b22002-04-04 19:21:06 +000057 const vector<PointerValSet*> &PVSToUpdate(N->getReferrers());
Chris Lattner1120c8b2002-03-28 17:56:03 +000058 for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i)
59 PVSToUpdate[i]->add(ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000060}
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//
66static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) {
67 assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!");
68
Chris Lattner1120c8b2002-03-28 17:56:03 +000069 const PointerValSet &PVS = Node->getLink(0);
Chris Lattner212be2e2002-04-16 03:44:03 +000070 ResolveNodesTo(PVS, ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000071}
72
73// isResolvableCallNode - Return true if node is a call node and it is a call
74// node that we can inline...
75//
Chris Lattner1120c8b2002-03-28 17:56:03 +000076static bool isResolvableCallNode(CallDSNode *CN) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000077 // 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//
89void FunctionDSGraph::computeClosure(const DataStructure &DS) {
Chris Lattner26cfa662002-03-31 07:13:27 +000090 // Note that this cannot be a real vector because the keys will be changing
91 // as nodes are eliminated!
92 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000093 typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor;
Chris Lattner26cfa662002-03-31 07:13:27 +000094 vector<pair<CallDescriptor, PointerValSet> > CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000095
Chris Lattner1120c8b2002-03-28 17:56:03 +000096 unsigned NumInlines = 0;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000097
98 // Loop over the resolvable call nodes...
Chris Lattner1120c8b2002-03-28 17:56:03 +000099 vector<CallDSNode*>::iterator NI;
100 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
101 while (NI != CallNodes.end()) {
102 CallDSNode *CN = *NI;
Chris Lattner7650b942002-04-16 20:39:59 +0000103 // FIXME: This should work based on the pointer val set of the first arg
104 // link (which is the function to call)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000105 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000106
Chris Lattnercc0c1b22002-04-04 19:21:06 +0000107 if (NumInlines++ == 100) { // CUTE hack huh?
Chris Lattner1120c8b2002-03-28 17:56:03 +0000108 cerr << "Infinite (?) recursion halted\n";
109 return;
110 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000111
Chris Lattner1120c8b2002-03-28 17:56:03 +0000112 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000113
Chris Lattner1120c8b2002-03-28 17:56:03 +0000114 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000115
Chris Lattner1120c8b2002-03-28 17:56:03 +0000116 // Find out if we have already incorporated this node... if so, it will be
117 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000118 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000119
120#if 0
121 cerr << "\nSearching for: " << (void*)CN->getCall() << ": ";
122 for (unsigned X = 0; X != CN->getArgs().size(); ++X) {
123 cerr << " " << X << " is\n";
124 CN->getArgs().first[X].print(cerr);
125 }
126#endif
127
128 const vector<PointerValSet> &Args = CN->getArgs();
129 PointerValSet *CMI = 0;
130 for (unsigned i = 0, e = CallMap.size(); i != e; ++i) {
131#if 0
132 cerr << "Found: " << (void*)CallMap[i].first.second << ": ";
133 for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) {
134 cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr);
135 }
136#endif
137
138 // Look to see if the function call takes a superset of the values we are
139 // providing as input
140 //
141 CallDescriptor &CD = CallMap[i].first;
142 if (CD.second == CN->getCall() && CD.first.size() == Args.size()) {
143 bool FoundMismatch = false;
144 for (unsigned j = 0, je = Args.size(); j != je; ++j) {
145 PointerValSet ArgSet = CD.first[j];
146 if (ArgSet.add(Args[j])) {
147 FoundMismatch = true; break;
148 }
149 }
150
151 if (!FoundMismatch) { CMI = &CallMap[i].second; break; }
152 }
153 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000154
155 // Hold the set of values that correspond to the incorporated methods
156 // return set.
157 //
158 PointerValSet RetVals;
159
Chris Lattner26cfa662002-03-31 07:13:27 +0000160 if (CMI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000161 // We have already inlined an identical function call!
Chris Lattner26cfa662002-03-31 07:13:27 +0000162 RetVals = *CMI;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000163 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000164 // Get the datastructure graph for the new method. Note that we are not
165 // allowed to modify this graph because it will be the cached graph that
166 // is returned by other users that want the local datastructure graph for
167 // a method.
168 //
169 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
170
Chris Lattner1120c8b2002-03-28 17:56:03 +0000171 // StartNode - The first node of the incorporated graph, last node of the
172 // preexisting data structure graph...
173 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000174 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000175
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000176 // Incorporate a copy of the called function graph into the current graph,
177 // allowing us to do local transformations to local graph to link
178 // arguments to call values, and call node to return value...
179 //
Chris Lattner212be2e2002-04-16 03:44:03 +0000180 vector<PointerValSet> Args;
181 RetVals = cloneFunctionIntoSelf(NewFunction, false, Args);
Chris Lattner26cfa662002-03-31 07:13:27 +0000182 CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()),
183 RetVals));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000184
Chris Lattner1120c8b2002-03-28 17:56:03 +0000185 // If the call node has arguments, process them now!
Chris Lattner7650b942002-04-16 20:39:59 +0000186 assert(Args.size() == CN->getNumArgs()-1 &&
Chris Lattner212be2e2002-04-16 03:44:03 +0000187 "Call node doesn't match function?");
188
189 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
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.
Chris Lattner7650b942002-04-16 20:39:59 +0000193 ResolveNodesTo(Args[i], CN->getArgValues(i+1));
Chris Lattner1120c8b2002-03-28 17:56:03 +0000194 }
195
196 // Loop through the nodes, deleting alloca nodes in the inlined function.
197 // Since the memory has been released, we cannot access their pointer
198 // fields (with defined results at least), so it is not possible to use
199 // any pointers to the alloca. Drop them now, and remove the alloca's
200 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000201 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000202 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
203 if (AllocNodes[i]->isAllocaNode()) {
204 AllocDSNode *NDS = AllocNodes[i];
205 NDS->removeAllIncomingEdges(); // These edges are invalid now
206 delete NDS; // Node is dead
207 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
208 --i; // Don't skip the next node
209 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000210 }
211
212 // If the function returns a pointer value... Resolve values pointing to
213 // the shadow nodes pointed to by CN to now point the values in RetVals...
214 //
215 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
216
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000217 // Now the call node is completely destructable. Eliminate it now.
218 delete CN;
219
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000220 bool Changed = true;
221 while (Changed) {
222 // Eliminate shadow nodes that are not distinguishable from some other
223 // node in the graph...
224 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000225 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000226
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000227 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000228 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000229 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000230
231 //if (F == Func) return; // Only do one self inlining
232
233 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000234 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000235 }
Chris Lattner7650b942002-04-16 20:39:59 +0000236
237 // Drop references to globals...
238 CallMap.clear();
239
240 bool Changed = true;
241 while (Changed) {
242 // Eliminate shadow nodes that are not distinguishable from some other
243 // node in the graph...
244 //
245 Changed = UnlinkUndistinguishableNodes();
246
247 // Eliminate shadow nodes that are now extraneous due to linking...
248 Changed |= RemoveUnreachableNodes();
249 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000250}