blob: 5ea706800ba4d5cc859d095d277b25883e1f3d40 [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>
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000014
Chris Lattner1120c8b2002-03-28 17:56:03 +000015// Make all of the pointers that point to Val also point to N.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000016//
Chris Lattner1120c8b2002-03-28 17:56:03 +000017static void copyEdgesFromTo(PointerVal Val, DSNode *N) {
Chris Lattnera13d6ce2002-04-01 22:20:48 +000018 unsigned ValIdx = Val.Index;
19 unsigned NLinks = N->getNumLinks();
Chris Lattner1120c8b2002-03-28 17:56:03 +000020
Chris Lattnera13d6ce2002-04-01 22:20:48 +000021 const vector<PointerValSet*> &PVSsToUpdate(Val.Node->getReferrers());
22 for (unsigned i = 0, e = PVSsToUpdate.size(); i != e; ++i) {
23 // Loop over all of the pointers pointing to Val...
24 PointerValSet &PVS = *PVSsToUpdate[i];
25 for (unsigned j = 0, je = PVS.size(); j != je; ++j) {
26 if (PVS[j].Node == Val.Node && PVS[j].Index >= ValIdx &&
27 PVS[j].Index < ValIdx+NLinks)
28 PVS.add(PointerVal(N, PVS[j].Index-ValIdx));
Chris Lattnera13d6ce2002-04-01 22:20:48 +000029 }
30 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000031}
32
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000033static void ResolveNodesTo(const PointerVal &FromPtr,
34 const PointerValSet &ToVals) {
35 assert(FromPtr.Index == 0 &&
36 "Resolved node return pointer should be index 0!");
Chris Lattnercc0c1b22002-04-04 19:21:06 +000037 DSNode *N = FromPtr.Node;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000038
Chris Lattner1120c8b2002-03-28 17:56:03 +000039 // Make everything that pointed to the shadow node also point to the values in
40 // ToVals...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000041 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000042 for (unsigned i = 0, e = ToVals.size(); i != e; ++i)
Chris Lattnercc0c1b22002-04-04 19:21:06 +000043 copyEdgesFromTo(ToVals[i], N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000044
Chris Lattner1120c8b2002-03-28 17:56:03 +000045 // Make everything that pointed to the shadow node now also point to the
46 // values it is equivalent to...
Chris Lattnercc0c1b22002-04-04 19:21:06 +000047 const vector<PointerValSet*> &PVSToUpdate(N->getReferrers());
Chris Lattner1120c8b2002-03-28 17:56:03 +000048 for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i)
49 PVSToUpdate[i]->add(ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000050}
51
52
53// ResolveNodeTo - The specified node is now known to point to the set of values
54// in ToVals, instead of the old shadow node subgraph that it was pointing to.
55//
56static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) {
57 assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!");
58
Chris Lattner1120c8b2002-03-28 17:56:03 +000059 const PointerValSet &PVS = Node->getLink(0);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000060
Chris Lattner1120c8b2002-03-28 17:56:03 +000061 // Only resolve the first pointer, although there many be many pointers here.
62 // The problem is that the inlined function might return one of the arguments
63 // to the function, and if so, extra values can be added to the arg or call
64 // node that point to what the other one got resolved to. Since these will
65 // be added to the end of the PVS pointed in, we just ignore them.
66 //
67 ResolveNodesTo(PVS[0], ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000068}
69
70// isResolvableCallNode - Return true if node is a call node and it is a call
71// node that we can inline...
72//
Chris Lattner1120c8b2002-03-28 17:56:03 +000073static bool isResolvableCallNode(CallDSNode *CN) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000074 // Only operate on call nodes with direct method calls
75 Function *F = CN->getCall()->getCalledFunction();
76 if (F == 0) return false;
77
78 // Only work on call nodes with direct calls to methods with bodies.
79 return !F->isExternal();
80}
81
82
83// computeClosure - Replace all of the resolvable call nodes with the contents
84// of their corresponding method data structure graph...
85//
86void FunctionDSGraph::computeClosure(const DataStructure &DS) {
Chris Lattner26cfa662002-03-31 07:13:27 +000087 // Note that this cannot be a real vector because the keys will be changing
88 // as nodes are eliminated!
89 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000090 typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor;
Chris Lattner26cfa662002-03-31 07:13:27 +000091 vector<pair<CallDescriptor, PointerValSet> > CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000092
Chris Lattner1120c8b2002-03-28 17:56:03 +000093 unsigned NumInlines = 0;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000094
95 // Loop over the resolvable call nodes...
Chris Lattner1120c8b2002-03-28 17:56:03 +000096 vector<CallDSNode*>::iterator NI;
97 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
98 while (NI != CallNodes.end()) {
99 CallDSNode *CN = *NI;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000100 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000101
Chris Lattnercc0c1b22002-04-04 19:21:06 +0000102 if (NumInlines++ == 100) { // CUTE hack huh?
Chris Lattner1120c8b2002-03-28 17:56:03 +0000103 cerr << "Infinite (?) recursion halted\n";
104 return;
105 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000106
Chris Lattner1120c8b2002-03-28 17:56:03 +0000107 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000108
Chris Lattner1120c8b2002-03-28 17:56:03 +0000109 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000110
Chris Lattner1120c8b2002-03-28 17:56:03 +0000111 // Find out if we have already incorporated this node... if so, it will be
112 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000113 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000114
115#if 0
116 cerr << "\nSearching for: " << (void*)CN->getCall() << ": ";
117 for (unsigned X = 0; X != CN->getArgs().size(); ++X) {
118 cerr << " " << X << " is\n";
119 CN->getArgs().first[X].print(cerr);
120 }
121#endif
122
123 const vector<PointerValSet> &Args = CN->getArgs();
124 PointerValSet *CMI = 0;
125 for (unsigned i = 0, e = CallMap.size(); i != e; ++i) {
126#if 0
127 cerr << "Found: " << (void*)CallMap[i].first.second << ": ";
128 for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) {
129 cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr);
130 }
131#endif
132
133 // Look to see if the function call takes a superset of the values we are
134 // providing as input
135 //
136 CallDescriptor &CD = CallMap[i].first;
137 if (CD.second == CN->getCall() && CD.first.size() == Args.size()) {
138 bool FoundMismatch = false;
139 for (unsigned j = 0, je = Args.size(); j != je; ++j) {
140 PointerValSet ArgSet = CD.first[j];
141 if (ArgSet.add(Args[j])) {
142 FoundMismatch = true; break;
143 }
144 }
145
146 if (!FoundMismatch) { CMI = &CallMap[i].second; break; }
147 }
148 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000149
150 // Hold the set of values that correspond to the incorporated methods
151 // return set.
152 //
153 PointerValSet RetVals;
154
Chris Lattner26cfa662002-03-31 07:13:27 +0000155 if (CMI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000156 // We have already inlined an identical function call!
Chris Lattner26cfa662002-03-31 07:13:27 +0000157 RetVals = *CMI;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000158 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000159 // Get the datastructure graph for the new method. Note that we are not
160 // allowed to modify this graph because it will be the cached graph that
161 // is returned by other users that want the local datastructure graph for
162 // a method.
163 //
164 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
165
Chris Lattner1120c8b2002-03-28 17:56:03 +0000166 // StartNode - The first node of the incorporated graph, last node of the
167 // preexisting data structure graph...
168 //
169 unsigned StartArgNode = ArgNodes.size();
170 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000171
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000172 // Incorporate a copy of the called function graph into the current graph,
173 // allowing us to do local transformations to local graph to link
174 // arguments to call values, and call node to return value...
175 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000176 RetVals = cloneFunctionIntoSelf(NewFunction, false);
177 CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()),
178 RetVals));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000179
Chris Lattner1120c8b2002-03-28 17:56:03 +0000180 // If the call node has arguments, process them now!
181 if (CN->getNumArgs()) {
182 // The ArgNodes of the incorporated graph should be the nodes starting
183 // at StartNode, ordered the same way as the call arguments. The arg
184 // nodes are seperated by a single shadow node, but that shadow node
185 // might get eliminated in the process of optimization.
186 //
187 for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) {
188 // Get the arg node of the incorporated method...
189 ArgDSNode *ArgNode = ArgNodes[StartArgNode];
190
191 // Now we make all of the nodes inside of the incorporated method
192 // point to the real arguments values, not to the shadow nodes for the
193 // argument.
194 //
195 ResolveNodeTo(ArgNode, CN->getArgValues(i));
196
197 // Remove the argnode from the set of nodes in this method...
198 ArgNodes.erase(ArgNodes.begin()+StartArgNode);
199
200 // ArgNode is no longer useful, delete now!
201 delete ArgNode;
202 }
203 }
204
205 // Loop through the nodes, deleting alloca nodes in the inlined function.
206 // Since the memory has been released, we cannot access their pointer
207 // fields (with defined results at least), so it is not possible to use
208 // any pointers to the alloca. Drop them now, and remove the alloca's
209 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000210 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000211 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
212 if (AllocNodes[i]->isAllocaNode()) {
213 AllocDSNode *NDS = AllocNodes[i];
214 NDS->removeAllIncomingEdges(); // These edges are invalid now
215 delete NDS; // Node is dead
216 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
217 --i; // Don't skip the next node
218 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000219 }
220
221 // If the function returns a pointer value... Resolve values pointing to
222 // the shadow nodes pointed to by CN to now point the values in RetVals...
223 //
224 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
225
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000226 // Now the call node is completely destructable. Eliminate it now.
227 delete CN;
228
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000229 bool Changed = true;
230 while (Changed) {
231 // Eliminate shadow nodes that are not distinguishable from some other
232 // node in the graph...
233 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000234 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000235
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000236 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000237 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000238 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000239
240 //if (F == Func) return; // Only do one self inlining
241
242 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000243 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000244 }
245}