blob: 6466f0ba1acbeb38302c1a7cba04af1e19f98355 [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"
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 Lattner1120c8b2002-03-28 17:56:03 +000018// Make all of the pointers that point to Val also point to N.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000019//
Chris Lattner1120c8b2002-03-28 17:56:03 +000020static void copyEdgesFromTo(PointerVal Val, DSNode *N) {
Chris Lattnera13d6ce2002-04-01 22:20:48 +000021 unsigned ValIdx = Val.Index;
22 unsigned NLinks = N->getNumLinks();
Chris Lattner1120c8b2002-03-28 17:56:03 +000023
Chris Lattnera13d6ce2002-04-01 22:20:48 +000024 const vector<PointerValSet*> &PVSsToUpdate(Val.Node->getReferrers());
25 for (unsigned i = 0, e = PVSsToUpdate.size(); i != e; ++i) {
26 // Loop over all of the pointers pointing to Val...
27 PointerValSet &PVS = *PVSsToUpdate[i];
28 for (unsigned j = 0, je = PVS.size(); j != je; ++j) {
29 if (PVS[j].Node == Val.Node && PVS[j].Index >= ValIdx &&
30 PVS[j].Index < ValIdx+NLinks)
31 PVS.add(PointerVal(N, PVS[j].Index-ValIdx));
32
33 //PVS.add(PointerVal(N, Val.Index)); // TODO: support index
34 }
35 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000036}
37
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000038static void ResolveNodesTo(const PointerVal &FromPtr,
39 const PointerValSet &ToVals) {
40 assert(FromPtr.Index == 0 &&
41 "Resolved node return pointer should be index 0!");
Chris Lattner1120c8b2002-03-28 17:56:03 +000042 assert(isa<ShadowDSNode>(FromPtr.Node) &&
43 "Resolved node should be a shadow!");
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000044 ShadowDSNode *Shadow = cast<ShadowDSNode>(FromPtr.Node);
Chris Lattner1120c8b2002-03-28 17:56:03 +000045 assert(Shadow->isCriticalNode() && "Shadow node should be a critical node!");
Chris Lattnerea4af652002-03-27 19:44:33 +000046 Shadow->resetCriticalMark();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000047
Chris Lattner1120c8b2002-03-28 17:56:03 +000048 // Make everything that pointed to the shadow node also point to the values in
49 // ToVals...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000050 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000051 for (unsigned i = 0, e = ToVals.size(); i != e; ++i)
52 copyEdgesFromTo(ToVals[i], Shadow);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000053
Chris Lattner1120c8b2002-03-28 17:56:03 +000054 // Make everything that pointed to the shadow node now also point to the
55 // values it is equivalent to...
56 const vector<PointerValSet*> &PVSToUpdate(Shadow->getReferrers());
57 for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i)
58 PVSToUpdate[i]->add(ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000059}
60
61
62// ResolveNodeTo - The specified node is now known to point to the set of values
63// in ToVals, instead of the old shadow node subgraph that it was pointing to.
64//
65static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) {
66 assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!");
67
Chris Lattner1120c8b2002-03-28 17:56:03 +000068 const PointerValSet &PVS = Node->getLink(0);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000069
Chris Lattner1120c8b2002-03-28 17:56:03 +000070 // Only resolve the first pointer, although there many be many pointers here.
71 // The problem is that the inlined function might return one of the arguments
72 // to the function, and if so, extra values can be added to the arg or call
73 // node that point to what the other one got resolved to. Since these will
74 // be added to the end of the PVS pointed in, we just ignore them.
75 //
76 ResolveNodesTo(PVS[0], ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000077}
78
79// isResolvableCallNode - Return true if node is a call node and it is a call
80// node that we can inline...
81//
Chris Lattner1120c8b2002-03-28 17:56:03 +000082static bool isResolvableCallNode(CallDSNode *CN) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000083 // Only operate on call nodes with direct method calls
84 Function *F = CN->getCall()->getCalledFunction();
85 if (F == 0) return false;
86
87 // Only work on call nodes with direct calls to methods with bodies.
88 return !F->isExternal();
89}
90
91
92// computeClosure - Replace all of the resolvable call nodes with the contents
93// of their corresponding method data structure graph...
94//
95void FunctionDSGraph::computeClosure(const DataStructure &DS) {
Chris Lattner26cfa662002-03-31 07:13:27 +000096 // Note that this cannot be a real vector because the keys will be changing
97 // as nodes are eliminated!
98 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000099 typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor;
Chris Lattner26cfa662002-03-31 07:13:27 +0000100 vector<pair<CallDescriptor, PointerValSet> > CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000101
Chris Lattner1120c8b2002-03-28 17:56:03 +0000102 unsigned NumInlines = 0;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000103
104 // Loop over the resolvable call nodes...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000105 vector<CallDSNode*>::iterator NI;
106 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
107 while (NI != CallNodes.end()) {
108 CallDSNode *CN = *NI;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000109 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000110
Chris Lattnerbab4a902002-04-01 00:12:58 +0000111 if (NumInlines++ == 40) { // CUTE hack huh?
Chris Lattner1120c8b2002-03-28 17:56:03 +0000112 cerr << "Infinite (?) recursion halted\n";
113 return;
114 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000115
Chris Lattner1120c8b2002-03-28 17:56:03 +0000116 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000117
Chris Lattner1120c8b2002-03-28 17:56:03 +0000118 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000119
Chris Lattner1120c8b2002-03-28 17:56:03 +0000120 // Find out if we have already incorporated this node... if so, it will be
121 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000122 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000123
124#if 0
125 cerr << "\nSearching for: " << (void*)CN->getCall() << ": ";
126 for (unsigned X = 0; X != CN->getArgs().size(); ++X) {
127 cerr << " " << X << " is\n";
128 CN->getArgs().first[X].print(cerr);
129 }
130#endif
131
132 const vector<PointerValSet> &Args = CN->getArgs();
133 PointerValSet *CMI = 0;
134 for (unsigned i = 0, e = CallMap.size(); i != e; ++i) {
135#if 0
136 cerr << "Found: " << (void*)CallMap[i].first.second << ": ";
137 for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) {
138 cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr);
139 }
140#endif
141
142 // Look to see if the function call takes a superset of the values we are
143 // providing as input
144 //
145 CallDescriptor &CD = CallMap[i].first;
146 if (CD.second == CN->getCall() && CD.first.size() == Args.size()) {
147 bool FoundMismatch = false;
148 for (unsigned j = 0, je = Args.size(); j != je; ++j) {
149 PointerValSet ArgSet = CD.first[j];
150 if (ArgSet.add(Args[j])) {
151 FoundMismatch = true; break;
152 }
153 }
154
155 if (!FoundMismatch) { CMI = &CallMap[i].second; break; }
156 }
157 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000158
159 // Hold the set of values that correspond to the incorporated methods
160 // return set.
161 //
162 PointerValSet RetVals;
163
Chris Lattner26cfa662002-03-31 07:13:27 +0000164 if (CMI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000165 // We have already inlined an identical function call!
Chris Lattner26cfa662002-03-31 07:13:27 +0000166 RetVals = *CMI;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000167 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000168 // Get the datastructure graph for the new method. Note that we are not
169 // allowed to modify this graph because it will be the cached graph that
170 // is returned by other users that want the local datastructure graph for
171 // a method.
172 //
173 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
174
Chris Lattner1120c8b2002-03-28 17:56:03 +0000175 // StartNode - The first node of the incorporated graph, last node of the
176 // preexisting data structure graph...
177 //
178 unsigned StartArgNode = ArgNodes.size();
179 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000180
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000181 // Incorporate a copy of the called function graph into the current graph,
182 // allowing us to do local transformations to local graph to link
183 // arguments to call values, and call node to return value...
184 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000185 RetVals = cloneFunctionIntoSelf(NewFunction, false);
186 CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()),
187 RetVals));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000188
Chris Lattner1120c8b2002-03-28 17:56:03 +0000189 // If the call node has arguments, process them now!
190 if (CN->getNumArgs()) {
191 // The ArgNodes of the incorporated graph should be the nodes starting
192 // at StartNode, ordered the same way as the call arguments. The arg
193 // nodes are seperated by a single shadow node, but that shadow node
194 // might get eliminated in the process of optimization.
195 //
196 for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) {
197 // Get the arg node of the incorporated method...
198 ArgDSNode *ArgNode = ArgNodes[StartArgNode];
199
200 // Now we make all of the nodes inside of the incorporated method
201 // point to the real arguments values, not to the shadow nodes for the
202 // argument.
203 //
204 ResolveNodeTo(ArgNode, CN->getArgValues(i));
205
206 // Remove the argnode from the set of nodes in this method...
207 ArgNodes.erase(ArgNodes.begin()+StartArgNode);
208
209 // ArgNode is no longer useful, delete now!
210 delete ArgNode;
211 }
212 }
213
214 // Loop through the nodes, deleting alloca nodes in the inlined function.
215 // Since the memory has been released, we cannot access their pointer
216 // fields (with defined results at least), so it is not possible to use
217 // any pointers to the alloca. Drop them now, and remove the alloca's
218 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000219 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000220 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
221 if (AllocNodes[i]->isAllocaNode()) {
222 AllocDSNode *NDS = AllocNodes[i];
223 NDS->removeAllIncomingEdges(); // These edges are invalid now
224 delete NDS; // Node is dead
225 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
226 --i; // Don't skip the next node
227 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000228 }
229
230 // If the function returns a pointer value... Resolve values pointing to
231 // the shadow nodes pointed to by CN to now point the values in RetVals...
232 //
233 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
234
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000235 // Now the call node is completely destructable. Eliminate it now.
236 delete CN;
237
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000238 bool Changed = true;
239 while (Changed) {
240 // Eliminate shadow nodes that are not distinguishable from some other
241 // node in the graph...
242 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000243 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000244
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000245 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000246 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000247 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000248
249 //if (F == Func) return; // Only do one self inlining
250
251 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000252 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000253 }
254}