blob: a86d6e50e32c371a050e0077e4f513bc0ae753f1 [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 Lattner1120c8b2002-03-28 17:56:03 +000086 typedef pair<vector<PointerValSet>, CallInst *> CallDescriptor;
87 map<CallDescriptor, PointerValSet> CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000088
Chris Lattner1120c8b2002-03-28 17:56:03 +000089 unsigned NumInlines = 0;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000090
91 // Loop over the resolvable call nodes...
Chris Lattner1120c8b2002-03-28 17:56:03 +000092 vector<CallDSNode*>::iterator NI;
93 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
94 while (NI != CallNodes.end()) {
95 CallDSNode *CN = *NI;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000096 Function *F = CN->getCall()->getCalledFunction();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000097
Chris Lattner1120c8b2002-03-28 17:56:03 +000098 if (NumInlines++ == 30) { // CUTE hack huh?
99 cerr << "Infinite (?) recursion halted\n";
100 return;
101 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000102
Chris Lattner1120c8b2002-03-28 17:56:03 +0000103 CallNodes.erase(NI); // Remove the call node from the graph
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000104
Chris Lattner1120c8b2002-03-28 17:56:03 +0000105 unsigned CallNodeOffset = NI-CallNodes.begin();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000106
Chris Lattner1120c8b2002-03-28 17:56:03 +0000107 // Find out if we have already incorporated this node... if so, it will be
108 // in the CallMap...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000109 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000110 CallDescriptor FDesc(CN->getArgs(), CN->getCall());
111 map<CallDescriptor, PointerValSet>::iterator CMI = CallMap.find(FDesc);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000112
113 // Hold the set of values that correspond to the incorporated methods
114 // return set.
115 //
116 PointerValSet RetVals;
117
Chris Lattner1120c8b2002-03-28 17:56:03 +0000118 if (CMI != CallMap.end()) {
119 // We have already inlined an identical function call!
120 RetVals = CMI->second;
121 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000122 // Get the datastructure graph for the new method. Note that we are not
123 // allowed to modify this graph because it will be the cached graph that
124 // is returned by other users that want the local datastructure graph for
125 // a method.
126 //
127 const FunctionDSGraph &NewFunction = DS.getDSGraph(F);
128
Chris Lattner1120c8b2002-03-28 17:56:03 +0000129 // StartNode - The first node of the incorporated graph, last node of the
130 // preexisting data structure graph...
131 //
132 unsigned StartArgNode = ArgNodes.size();
133 unsigned StartAllocNode = AllocNodes.size();
Chris Lattnerea4af652002-03-27 19:44:33 +0000134
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000135 // Incorporate a copy of the called function graph into the current graph,
136 // allowing us to do local transformations to local graph to link
137 // arguments to call values, and call node to return value...
138 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000139 RetVals = cloneFunctionIntoSelf(NewFunction, F == Func);
140 CallMap[FDesc] = RetVals;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000141
Chris Lattner1120c8b2002-03-28 17:56:03 +0000142 // If the call node has arguments, process them now!
143 if (CN->getNumArgs()) {
144 // The ArgNodes of the incorporated graph should be the nodes starting
145 // at StartNode, ordered the same way as the call arguments. The arg
146 // nodes are seperated by a single shadow node, but that shadow node
147 // might get eliminated in the process of optimization.
148 //
149 for (unsigned i = 0, e = CN->getNumArgs(); i != e; ++i) {
150 // Get the arg node of the incorporated method...
151 ArgDSNode *ArgNode = ArgNodes[StartArgNode];
152
153 // Now we make all of the nodes inside of the incorporated method
154 // point to the real arguments values, not to the shadow nodes for the
155 // argument.
156 //
157 ResolveNodeTo(ArgNode, CN->getArgValues(i));
158
159 // Remove the argnode from the set of nodes in this method...
160 ArgNodes.erase(ArgNodes.begin()+StartArgNode);
161
162 // ArgNode is no longer useful, delete now!
163 delete ArgNode;
164 }
165 }
166
167 // Loop through the nodes, deleting alloca nodes in the inlined function.
168 // Since the memory has been released, we cannot access their pointer
169 // fields (with defined results at least), so it is not possible to use
170 // any pointers to the alloca. Drop them now, and remove the alloca's
171 // since they are dead (we just removed all links to them).
Chris Lattnerea4af652002-03-27 19:44:33 +0000172 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000173 for (unsigned i = StartAllocNode; i != AllocNodes.size(); ++i)
174 if (AllocNodes[i]->isAllocaNode()) {
175 AllocDSNode *NDS = AllocNodes[i];
176 NDS->removeAllIncomingEdges(); // These edges are invalid now
177 delete NDS; // Node is dead
178 AllocNodes.erase(AllocNodes.begin()+i); // Remove slot in Nodes array
179 --i; // Don't skip the next node
180 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000181 }
182
183 // If the function returns a pointer value... Resolve values pointing to
184 // the shadow nodes pointed to by CN to now point the values in RetVals...
185 //
186 if (CN->getNumLinks()) ResolveNodeTo(CN, RetVals);
187
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000188 // Now the call node is completely destructable. Eliminate it now.
189 delete CN;
190
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000191 bool Changed = true;
192 while (Changed) {
193 // Eliminate shadow nodes that are not distinguishable from some other
194 // node in the graph...
195 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000196 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000197
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000198 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000199 Changed |= RemoveUnreachableNodes();
Chris Lattnerdf8af1c2002-03-27 00:53:57 +0000200 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000201
202 //if (F == Func) return; // Only do one self inlining
203
204 // Move on to the next call node...
Chris Lattner1120c8b2002-03-28 17:56:03 +0000205 NI = std::find_if(CallNodes.begin(), CallNodes.end(), isResolvableCallNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000206 }
207}