blob: 989f9bbcb9a5801d08728a8e30a240bfe1edf562 [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));
Chris Lattnera13d6ce2002-04-01 22:20:48 +000032 }
33 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000034}
35
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000036static void ResolveNodesTo(const PointerVal &FromPtr,
37 const PointerValSet &ToVals) {
38 assert(FromPtr.Index == 0 &&
39 "Resolved node return pointer should be index 0!");
Chris Lattnercc0c1b22002-04-04 19:21:06 +000040 DSNode *N = FromPtr.Node;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000041
Chris Lattner1120c8b2002-03-28 17:56:03 +000042 // Make everything that pointed to the shadow node also point to the values in
43 // ToVals...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000044 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000045 for (unsigned i = 0, e = ToVals.size(); i != e; ++i)
Chris Lattnercc0c1b22002-04-04 19:21:06 +000046 copyEdgesFromTo(ToVals[i], N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000047
Chris Lattner1120c8b2002-03-28 17:56:03 +000048 // Make everything that pointed to the shadow node now also point to the
49 // values it is equivalent to...
Chris Lattnercc0c1b22002-04-04 19:21:06 +000050 const vector<PointerValSet*> &PVSToUpdate(N->getReferrers());
Chris Lattner1120c8b2002-03-28 17:56:03 +000051 for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i)
52 PVSToUpdate[i]->add(ToVals);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000053}
54
55
56// ResolveNodeTo - The specified node is now known to point to the set of values
57// in ToVals, instead of the old shadow node subgraph that it was pointing to.
58//
59static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) {
60 assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!");
61
Chris Lattner1120c8b2002-03-28 17:56:03 +000062 const PointerValSet &PVS = Node->getLink(0);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000063
Chris Lattner1120c8b2002-03-28 17:56:03 +000064 // Only resolve the first pointer, although there many be many pointers here.
65 // The problem is that the inlined function might return one of the arguments
66 // to the function, and if so, extra values can be added to the arg or call
67 // node that point to what the other one got resolved to. Since these will
68 // be added to the end of the PVS pointed in, we just ignore them.
69 //
70 ResolveNodesTo(PVS[0], 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 Lattnerbb2a28f2002-03-26 22:39:06 +0000103 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000104
Chris Lattnercc0c1b22002-04-04 19:21:06 +0000105 if (NumInlines++ == 100) { // CUTE hack huh?
Chris Lattner1120c8b2002-03-28 17:56:03 +0000106 cerr << "Infinite (?) recursion halted\n";
107 return;
108 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000109
Chris Lattner1120c8b2002-03-28 17:56:03 +0000110 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000111
Chris Lattner1120c8b2002-03-28 17:56:03 +0000112 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000113
Chris Lattner1120c8b2002-03-28 17:56:03 +0000114 // Find out if we have already incorporated this node... if so, it will be
115 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000116 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000117
118#if 0
119 cerr << "\nSearching for: " << (void*)CN->getCall() << ": ";
120 for (unsigned X = 0; X != CN->getArgs().size(); ++X) {
121 cerr << " " << X << " is\n";
122 CN->getArgs().first[X].print(cerr);
123 }
124#endif
125
126 const vector<PointerValSet> &Args = CN->getArgs();
127 PointerValSet *CMI = 0;
128 for (unsigned i = 0, e = CallMap.size(); i != e; ++i) {
129#if 0
130 cerr << "Found: " << (void*)CallMap[i].first.second << ": ";
131 for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) {
132 cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr);
133 }
134#endif
135
136 // Look to see if the function call takes a superset of the values we are
137 // providing as input
138 //
139 CallDescriptor &CD = CallMap[i].first;
140 if (CD.second == CN->getCall() && CD.first.size() == Args.size()) {
141 bool FoundMismatch = false;
142 for (unsigned j = 0, je = Args.size(); j != je; ++j) {
143 PointerValSet ArgSet = CD.first[j];
144 if (ArgSet.add(Args[j])) {
145 FoundMismatch = true; break;
146 }
147 }
148
149 if (!FoundMismatch) { CMI = &CallMap[i].second; break; }
150 }
151 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000152
153 // Hold the set of values that correspond to the incorporated methods
154 // return set.
155 //
156 PointerValSet RetVals;
157
Chris Lattner26cfa662002-03-31 07:13:27 +0000158 if (CMI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000159 // We have already inlined an identical function call!
Chris Lattner26cfa662002-03-31 07:13:27 +0000160 RetVals = *CMI;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000161 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000162 // Get the datastructure graph for the new method. Note that we are not
163 // allowed to modify this graph because it will be the cached graph that
164 // is returned by other users that want the local datastructure graph for
165 // a method.
166 //
167 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
168
Chris Lattner1120c8b2002-03-28 17:56:03 +0000169 // StartNode - The first node of the incorporated graph, last node of the
170 // preexisting data structure graph...
171 //
172 unsigned StartArgNode = ArgNodes.size();
173 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000174
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000175 // Incorporate a copy of the called function graph into the current graph,
176 // allowing us to do local transformations to local graph to link
177 // arguments to call values, and call node to return value...
178 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000179 RetVals = cloneFunctionIntoSelf(NewFunction, false);
180 CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()),
181 RetVals));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000182
Chris Lattner1120c8b2002-03-28 17:56:03 +0000183 // If the call node has arguments, process them now!
184 if (CN->getNumArgs()) {
185 // The ArgNodes of the incorporated graph should be the nodes starting
186 // at StartNode, ordered the same way as the call arguments. The arg
187 // nodes are seperated by a single shadow node, but that shadow node
188 // might get eliminated in the process of optimization.
189 //
190 for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) {
191 // Get the arg node of the incorporated method...
192 ArgDSNode *ArgNode = ArgNodes[StartArgNode];
193
194 // Now we make all of the nodes inside of the incorporated method
195 // point to the real arguments values, not to the shadow nodes for the
196 // argument.
197 //
198 ResolveNodeTo(ArgNode, CN->getArgValues(i));
199
200 // Remove the argnode from the set of nodes in this method...
201 ArgNodes.erase(ArgNodes.begin()+StartArgNode);
202
203 // ArgNode is no longer useful, delete now!
204 delete ArgNode;
205 }
206 }
207
208 // Loop through the nodes, deleting alloca nodes in the inlined function.
209 // Since the memory has been released, we cannot access their pointer
210 // fields (with defined results at least), so it is not possible to use
211 // any pointers to the alloca. Drop them now, and remove the alloca's
212 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000213 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000214 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
215 if (AllocNodes[i]->isAllocaNode()) {
216 AllocDSNode *NDS = AllocNodes[i];
217 NDS->removeAllIncomingEdges(); // These edges are invalid now
218 delete NDS; // Node is dead
219 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
220 --i; // Don't skip the next node
221 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000222 }
223
224 // If the function returns a pointer value... Resolve values pointing to
225 // the shadow nodes pointed to by CN to now point the values in RetVals...
226 //
227 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
228
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000229 // Now the call node is completely destructable. Eliminate it now.
230 delete CN;
231
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000232 bool Changed = true;
233 while (Changed) {
234 // Eliminate shadow nodes that are not distinguishable from some other
235 // node in the graph...
236 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000237 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000238
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000239 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000240 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000241 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000242
243 //if (F == Func) return; // Only do one self inlining
244
245 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000246 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000247 }
248}