blob: 493b282b8f6ca95ed1a179b1a0d1a3b4dc008835 [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) {
21 assert(Val.Index == 0 && "copyEdgesFromTo:index != 0 TODO");
22
23 const vector<PointerValSet*> &PVSToUpdate(Val.Node->getReferrers());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000024 for (unsigned i = 0, e = PVSToUpdate.size(); i != e; ++i)
Chris Lattner1120c8b2002-03-28 17:56:03 +000025 PVSToUpdate[i]->add(N); // TODO: support index
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000026}
27
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000028static void ResolveNodesTo(const PointerVal &FromPtr,
29 const PointerValSet &ToVals) {
30 assert(FromPtr.Index == 0 &&
31 "Resolved node return pointer should be index 0!");
Chris Lattner1120c8b2002-03-28 17:56:03 +000032 assert(isa<ShadowDSNode>(FromPtr.Node) &&
33 "Resolved node should be a shadow!");
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000034 ShadowDSNode *Shadow = cast<ShadowDSNode>(FromPtr.Node);
Chris Lattner1120c8b2002-03-28 17:56:03 +000035 assert(Shadow->isCriticalNode() && "Shadow node should be a critical node!");
Chris Lattnerea4af652002-03-27 19:44:33 +000036 Shadow->resetCriticalMark();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000037
Chris Lattner1120c8b2002-03-28 17:56:03 +000038 // Make everything that pointed to the shadow node also point to the values in
39 // ToVals...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000040 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000041 for (unsigned i = 0, e = ToVals.size(); i != e; ++i)
42 copyEdgesFromTo(ToVals[i], Shadow);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000043
Chris Lattner1120c8b2002-03-28 17:56:03 +000044 // 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 Lattnerbb2a28f2002-03-26 22:39:06 +000049}
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//
55static void ResolveNodeTo(DSNode *Node, const PointerValSet &ToVals) {
56 assert(Node->getNumLinks() == 1 && "Resolved node can only be a scalar!!");
57
Chris Lattner1120c8b2002-03-28 17:56:03 +000058 const PointerValSet &PVS = Node->getLink(0);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000059
Chris Lattner1120c8b2002-03-28 17:56:03 +000060 // 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 Lattnerbb2a28f2002-03-26 22:39:06 +000067}
68
69// isResolvableCallNode - Return true if node is a call node and it is a call
70// node that we can inline...
71//
Chris Lattner1120c8b2002-03-28 17:56:03 +000072static bool isResolvableCallNode(CallDSNode *CN) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000073 // 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//
85void FunctionDSGraph::computeClosure(const DataStructure &DS) {
Chris Lattner26cfa662002-03-31 07:13:27 +000086 // Note that this cannot be a real vector because the keys will be changing
87 // as nodes are eliminated!
88 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000089 typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor;
Chris Lattner26cfa662002-03-31 07:13:27 +000090 vector<pair<CallDescriptor, PointerValSet> > CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000091
Chris Lattner1120c8b2002-03-28 17:56:03 +000092 unsigned NumInlines = 0;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000093
94 // Loop over the resolvable call nodes...
Chris Lattner1120c8b2002-03-28 17:56:03 +000095 vector<CallDSNode*>::iterator NI;
96 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
97 while (NI != CallNodes.end()) {
98 CallDSNode *CN = *NI;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000099 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000100
Chris Lattnerbab4a902002-04-01 00:12:58 +0000101 if (NumInlines++ == 40) { // CUTE hack huh?
Chris Lattner1120c8b2002-03-28 17:56:03 +0000102 cerr << "Infinite (?) recursion halted\n";
103 return;
104 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000105
Chris Lattner1120c8b2002-03-28 17:56:03 +0000106 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000107
Chris Lattner1120c8b2002-03-28 17:56:03 +0000108 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000109
Chris Lattner1120c8b2002-03-28 17:56:03 +0000110 // Find out if we have already incorporated this node... if so, it will be
111 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000112 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000113
114#if 0
115 cerr << "\nSearching for: " << (void*)CN->getCall() << ": ";
116 for (unsigned X = 0; X != CN->getArgs().size(); ++X) {
117 cerr << " " << X << " is\n";
118 CN->getArgs().first[X].print(cerr);
119 }
120#endif
121
122 const vector<PointerValSet> &Args = CN->getArgs();
123 PointerValSet *CMI = 0;
124 for (unsigned i = 0, e = CallMap.size(); i != e; ++i) {
125#if 0
126 cerr << "Found: " << (void*)CallMap[i].first.second << ": ";
127 for (unsigned X = 0; X != CallMap[i].first.first.size(); ++X) {
128 cerr << " " << X << " is\n"; CallMap[i].first.first[X].print(cerr);
129 }
130#endif
131
132 // Look to see if the function call takes a superset of the values we are
133 // providing as input
134 //
135 CallDescriptor &CD = CallMap[i].first;
136 if (CD.second == CN->getCall() && CD.first.size() == Args.size()) {
137 bool FoundMismatch = false;
138 for (unsigned j = 0, je = Args.size(); j != je; ++j) {
139 PointerValSet ArgSet = CD.first[j];
140 if (ArgSet.add(Args[j])) {
141 FoundMismatch = true; break;
142 }
143 }
144
145 if (!FoundMismatch) { CMI = &CallMap[i].second; break; }
146 }
147 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000148
149 // Hold the set of values that correspond to the incorporated methods
150 // return set.
151 //
152 PointerValSet RetVals;
153
Chris Lattner26cfa662002-03-31 07:13:27 +0000154 if (CMI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000155 // We have already inlined an identical function call!
Chris Lattner26cfa662002-03-31 07:13:27 +0000156 RetVals = *CMI;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000157 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000158 // Get the datastructure graph for the new method. Note that we are not
159 // allowed to modify this graph because it will be the cached graph that
160 // is returned by other users that want the local datastructure graph for
161 // a method.
162 //
163 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
164
Chris Lattner1120c8b2002-03-28 17:56:03 +0000165 // StartNode - The first node of the incorporated graph, last node of the
166 // preexisting data structure graph...
167 //
168 unsigned StartArgNode = ArgNodes.size();
169 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000170
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000171 // Incorporate a copy of the called function graph into the current graph,
172 // allowing us to do local transformations to local graph to link
173 // arguments to call values, and call node to return value...
174 //
Chris Lattner26cfa662002-03-31 07:13:27 +0000175 RetVals = cloneFunctionIntoSelf(NewFunction, false);
176 CallMap.push_back(make_pair(CallDescriptor(CN->getArgs(), CN->getCall()),
177 RetVals));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000178
Chris Lattner1120c8b2002-03-28 17:56:03 +0000179 // If the call node has arguments, process them now!
180 if (CN->getNumArgs()) {
181 // The ArgNodes of the incorporated graph should be the nodes starting
182 // at StartNode, ordered the same way as the call arguments. The arg
183 // nodes are seperated by a single shadow node, but that shadow node
184 // might get eliminated in the process of optimization.
185 //
186 for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) {
187 // Get the arg node of the incorporated method...
188 ArgDSNode *ArgNode = ArgNodes[StartArgNode];
189
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.
193 //
194 ResolveNodeTo(ArgNode, CN->getArgValues(i));
195
196 // Remove the argnode from the set of nodes in this method...
197 ArgNodes.erase(ArgNodes.begin()+StartArgNode);
198
199 // ArgNode is no longer useful, delete now!
200 delete ArgNode;
201 }
202 }
203
204 // Loop through the nodes, deleting alloca nodes in the inlined function.
205 // Since the memory has been released, we cannot access their pointer
206 // fields (with defined results at least), so it is not possible to use
207 // any pointers to the alloca. Drop them now, and remove the alloca's
208 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000209 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000210 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
211 if (AllocNodes[i]->isAllocaNode()) {
212 AllocDSNode *NDS = AllocNodes[i];
213 NDS->removeAllIncomingEdges(); // These edges are invalid now
214 delete NDS; // Node is dead
215 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
216 --i; // Don't skip the next node
217 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000218 }
219
220 // If the function returns a pointer value... Resolve values pointing to
221 // the shadow nodes pointed to by CN to now point the values in RetVals...
222 //
223 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
224
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000225 // Now the call node is completely destructable. Eliminate it now.
226 delete CN;
227
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000228 bool Changed = true;
229 while (Changed) {
230 // Eliminate shadow nodes that are not distinguishable from some other
231 // node in the graph...
232 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000233 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000234
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000235 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000236 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000237 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000238
239 //if (F == Func) return; // Only do one self inlining
240
241 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000242 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000243 }
244}