blob: 8a4ee822cf73ea7064142cbe53cec66e38e25210 [file] [log] [blame]
Chris Lattner7d093d42002-03-28 19:16:48 +00001//===- EliminateNodes.cpp - Prune unneccesary nodes in the graph ----------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00002//
Chris Lattner7d093d42002-03-28 19:16:48 +00003// This file contains two node optimizations:
4// 1. UnlinkUndistinguishableNodes - Often, after unification, shadow
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00005// nodes are left around that should not exist anymore. An example is when
6// a shadow gets unified with a 'new' node, the following graph gets
7// generated: %X -> Shadow, %X -> New. Since all of the edges to the
8// shadow node also all go to the New node, we can eliminate the shadow.
9//
Chris Lattner7d093d42002-03-28 19:16:48 +000010// 2. RemoveUnreachableNodes - Remove shadow and allocation nodes that are not
11// reachable from some other node in the graph. Unreachable nodes are left
12// lying around often because a method only refers to some allocations with
13// scalar values or an alloca, then when it is inlined, these references
14// disappear and the nodes become homeless and prunable.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000015//
16//===----------------------------------------------------------------------===//
17
Chris Lattner44cf3902002-03-31 07:14:52 +000018#include "llvm/Analysis/DataStructureGraph.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000019#include "llvm/Value.h"
20#include "Support/STLExtras.h"
21#include <algorithm>
22
Chris Lattner2ba3a722002-03-27 19:48:03 +000023//#define DEBUG_NODE_ELIMINATE 1
24
Chris Lattner0b7c85c2002-03-31 19:57:44 +000025static void DestroyFirstNodeOfPair(DSNode *N1, DSNode *N2) {
26#ifdef DEBUG_NODE_ELIMINATE
27 cerr << "Found Indistinguishable Node:\n";
28 N1->print(cerr);
29#endif
30
31 // The nodes can be merged. Make sure that N2 contains all of the
32 // outgoing edges (fields) that N1 does...
33 //
34 assert(N1->getNumLinks() == N2->getNumLinks() &&
35 "Same type, diff # fields?");
36 for (unsigned i = 0, e = N1->getNumLinks(); i != e; ++i)
37 N2->getLink(i).add(N1->getLink(i));
38
39 // Now make sure that all of the nodes that point to N1 also point to the node
40 // that we are merging it with...
41 //
42 const std::vector<PointerValSet*> &Refs = N1->getReferrers();
43 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
44 PointerValSet &PVS = *Refs[i];
45
46 bool RanOnce = false;
47 for (unsigned j = 0, je = PVS.size(); j != je; ++j)
48 if (PVS[j].Node == N1) {
49 RanOnce = true;
50 PVS.add(PointerVal(N2, PVS[j].Index));
51 }
52
53 assert(RanOnce && "Node on user set but cannot find the use!");
54 }
55
Chris Lattneree7eafa2002-04-27 02:28:41 +000056 N1->mergeInto(N2);
Chris Lattner0b7c85c2002-03-31 19:57:44 +000057 N1->removeAllIncomingEdges();
58 delete N1;
59}
Chris Lattner1120c8b2002-03-28 17:56:03 +000060
61// isIndistinguishableNode - A node is indistinguishable if some other node
62// has exactly the same incoming links to it and if the node considers itself
63// to be the same as the other node...
Chris Lattner9d42a1d2002-03-27 00:55:13 +000064//
Chris Lattner7d093d42002-03-28 19:16:48 +000065static bool isIndistinguishableNode(DSNode *DN) {
Chris Lattner1120c8b2002-03-28 17:56:03 +000066 if (DN->getReferrers().empty()) { // No referrers...
Chris Lattner7650b942002-04-16 20:39:59 +000067 if (isa<ShadowDSNode>(DN) || isa<AllocDSNode>(DN)) {
68 delete DN;
Chris Lattner1120c8b2002-03-28 17:56:03 +000069 return true; // Node is trivially dead
Chris Lattner7650b942002-04-16 20:39:59 +000070 } else
Chris Lattner1120c8b2002-03-28 17:56:03 +000071 return false;
72 }
73
Chris Lattner9d42a1d2002-03-27 00:55:13 +000074 // Pick a random referrer... Ptr is the things that the referrer points to.
Chris Lattner1120c8b2002-03-28 17:56:03 +000075 // Since DN is in the Ptr set, look through the set seeing if there are any
76 // other nodes that are exactly equilivant to DN (with the exception of node
77 // type), but are not DN. If anything exists, then DN is indistinguishable.
Chris Lattner9d42a1d2002-03-27 00:55:13 +000078 //
Chris Lattner44cf3902002-03-31 07:14:52 +000079
80 DSNode *IndFrom = 0;
Chris Lattner1120c8b2002-03-28 17:56:03 +000081 const std::vector<PointerValSet*> &Refs = DN->getReferrers();
82 for (unsigned R = 0, RE = Refs.size(); R != RE; ++R) {
83 const PointerValSet &Ptr = *Refs[R];
Chris Lattner9d42a1d2002-03-27 00:55:13 +000084
Chris Lattner1120c8b2002-03-28 17:56:03 +000085 for (unsigned i = 0, e = Ptr.size(); i != e; ++i) {
86 DSNode *N2 = Ptr[i].Node;
87 if (Ptr[i].Index == 0 && N2 != cast<DSNode>(DN) &&
88 DN->getType() == N2->getType() && DN->isEquivalentTo(N2)) {
89
Chris Lattner44cf3902002-03-31 07:14:52 +000090 IndFrom = N2;
91 R = RE-1;
92 break;
Chris Lattner1120c8b2002-03-28 17:56:03 +000093 }
94 }
95 }
Chris Lattner9d42a1d2002-03-27 00:55:13 +000096
Chris Lattner44cf3902002-03-31 07:14:52 +000097 // If we haven't found an equivalent node to merge with, see if one of the
98 // nodes pointed to by this node is equivalent to this one...
99 //
100 if (IndFrom == 0) {
101 unsigned NumOutgoing = DN->getNumOutgoingLinks();
102 for (DSNode::iterator I = DN->begin(), E = DN->end(); I != E; ++I) {
103 DSNode *Linked = *I;
104 if (Linked != DN && Linked->getNumOutgoingLinks() == NumOutgoing &&
105 DN->getType() == Linked->getType() && DN->isEquivalentTo(Linked)) {
106#if 0
107 // Make sure the leftover node contains links to everything we do...
108 for (unsigned i = 0, e = DN->getNumLinks(); i != e; ++i)
109 Linked->getLink(i).add(DN->getLink(i));
110#endif
111
112 IndFrom = Linked;
113 break;
114 }
115 }
116 }
117
118
119 // If DN is indistinguishable from some other node, merge them now...
120 if (IndFrom == 0)
121 return false; // Otherwise, nothing found, perhaps next time....
122
Chris Lattner0b7c85c2002-03-31 19:57:44 +0000123 DestroyFirstNodeOfPair(DN, IndFrom);
Chris Lattner44cf3902002-03-31 07:14:52 +0000124 return true;
Chris Lattner9d42a1d2002-03-27 00:55:13 +0000125}
126
Chris Lattner1120c8b2002-03-28 17:56:03 +0000127template<typename NodeTy>
Chris Lattner44cf3902002-03-31 07:14:52 +0000128static bool removeIndistinguishableNodes(std::vector<NodeTy*> &Nodes) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000129 bool Changed = false;
130 std::vector<NodeTy*>::iterator I = Nodes.begin();
131 while (I != Nodes.end()) {
132 if (isIndistinguishableNode(*I)) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000133 I = Nodes.erase(I);
134 Changed = true;
135 } else {
136 ++I;
137 }
138 }
139 return Changed;
140}
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000141
Chris Lattner44cf3902002-03-31 07:14:52 +0000142template<typename NodeTy>
143static bool removeIndistinguishableNodePairs(std::vector<NodeTy*> &Nodes) {
144 bool Changed = false;
145 std::vector<NodeTy*>::iterator I = Nodes.begin();
146 while (I != Nodes.end()) {
147 NodeTy *N1 = *I++;
148 for (std::vector<NodeTy*>::iterator I2 = I, I2E = Nodes.end();
149 I2 != I2E; ++I2) {
150 NodeTy *N2 = *I2;
151 if (N1->isEquivalentTo(N2)) {
Chris Lattner0b7c85c2002-03-31 19:57:44 +0000152 DestroyFirstNodeOfPair(N1, N2);
Chris Lattner44cf3902002-03-31 07:14:52 +0000153 --I;
154 I = Nodes.erase(I);
155 Changed = true;
156 break;
157 }
158 }
159 }
160 return Changed;
161}
162
163
164
Chris Lattner7d093d42002-03-28 19:16:48 +0000165// UnlinkUndistinguishableNodes - Eliminate shadow nodes that are not
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000166// distinguishable from some other node in the graph...
167//
Chris Lattner7d093d42002-03-28 19:16:48 +0000168bool FunctionDSGraph::UnlinkUndistinguishableNodes() {
Chris Lattner9d42a1d2002-03-27 00:55:13 +0000169 // Loop over all of the shadow nodes, checking to see if they are
170 // indistinguishable from some other node. If so, eliminate the node!
171 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000172 return
Chris Lattner44cf3902002-03-31 07:14:52 +0000173 removeIndistinguishableNodes(AllocNodes) |
174 removeIndistinguishableNodes(ShadowNodes) |
Chris Lattnerf30185f2002-04-01 00:13:56 +0000175 removeIndistinguishableNodePairs(CallNodes) |
Chris Lattner44cf3902002-03-31 07:14:52 +0000176 removeIndistinguishableNodePairs(GlobalNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000177}
178
Chris Lattner1120c8b2002-03-28 17:56:03 +0000179static void MarkReferredNodesReachable(DSNode *N,
180 vector<ShadowDSNode*> &ShadowNodes,
181 vector<bool> &ReachableShadowNodes,
182 vector<AllocDSNode*> &AllocNodes,
183 vector<bool> &ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000184
185static inline void MarkReferredNodeSetReachable(const PointerValSet &PVS,
Chris Lattner1120c8b2002-03-28 17:56:03 +0000186 vector<ShadowDSNode*> &ShadowNodes,
187 vector<bool> &ReachableShadowNodes,
188 vector<AllocDSNode*> &AllocNodes,
189 vector<bool> &ReachableAllocNodes) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000190 for (unsigned i = 0, e = PVS.size(); i != e; ++i)
Chris Lattnercdae0b22002-03-28 18:38:38 +0000191 if (isa<ShadowDSNode>(PVS[i].Node) || isa<AllocDSNode>(PVS[i].Node))
Chris Lattner1120c8b2002-03-28 17:56:03 +0000192 MarkReferredNodesReachable(PVS[i].Node, ShadowNodes, ReachableShadowNodes,
193 AllocNodes, ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000194}
195
Chris Lattner1120c8b2002-03-28 17:56:03 +0000196static void MarkReferredNodesReachable(DSNode *N,
197 vector<ShadowDSNode*> &ShadowNodes,
198 vector<bool> &ReachableShadowNodes,
199 vector<AllocDSNode*> &AllocNodes,
200 vector<bool> &ReachableAllocNodes) {
201 assert(ShadowNodes.size() == ReachableShadowNodes.size());
202 assert(AllocNodes.size() == ReachableAllocNodes.size());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000203
Chris Lattner1120c8b2002-03-28 17:56:03 +0000204 if (ShadowDSNode *Shad = dyn_cast<ShadowDSNode>(N)) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000205 vector<ShadowDSNode*>::iterator I =
Chris Lattner1120c8b2002-03-28 17:56:03 +0000206 std::find(ShadowNodes.begin(), ShadowNodes.end(), Shad);
207 unsigned i = I-ShadowNodes.begin();
208 if (ReachableShadowNodes[i]) return; // Recursion detected, abort...
209 ReachableShadowNodes[i] = true;
210 } else if (AllocDSNode *Alloc = dyn_cast<AllocDSNode>(N)) {
211 vector<AllocDSNode*>::iterator I =
212 std::find(AllocNodes.begin(), AllocNodes.end(), Alloc);
213 unsigned i = I-AllocNodes.begin();
214 if (ReachableAllocNodes[i]) return; // Recursion detected, abort...
215 ReachableAllocNodes[i] = true;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000216 }
217
218 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
Chris Lattner1120c8b2002-03-28 17:56:03 +0000219 MarkReferredNodeSetReachable(N->getLink(i),
220 ShadowNodes, ReachableShadowNodes,
221 AllocNodes, ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000222
223 const std::vector<PointerValSet> *Links = N->getAuxLinks();
224 if (Links)
225 for (unsigned i = 0, e = Links->size(); i != e; ++i)
Chris Lattner1120c8b2002-03-28 17:56:03 +0000226 MarkReferredNodeSetReachable((*Links)[i],
227 ShadowNodes, ReachableShadowNodes,
228 AllocNodes, ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000229}
230
Chris Lattner4dc1f822002-03-28 19:33:00 +0000231void FunctionDSGraph::MarkEscapeableNodesReachable(
232 vector<bool> &ReachableShadowNodes,
233 vector<bool> &ReachableAllocNodes) {
234 // Mark all shadow nodes that have edges from other nodes as reachable.
235 // Recursively mark any shadow nodes pointed to by the newly live shadow
236 // nodes as also alive.
237 //
Chris Lattner4dc1f822002-03-28 19:33:00 +0000238 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
239 MarkReferredNodesReachable(GlobalNodes[i],
240 ShadowNodes, ReachableShadowNodes,
241 AllocNodes, ReachableAllocNodes);
242
243 for (unsigned i = 0, e = CallNodes.size(); i != e; ++i)
244 MarkReferredNodesReachable(CallNodes[i],
245 ShadowNodes, ReachableShadowNodes,
246 AllocNodes, ReachableAllocNodes);
247
248 // Mark all nodes in the return set as being reachable...
249 MarkReferredNodeSetReachable(RetNode,
250 ShadowNodes, ReachableShadowNodes,
251 AllocNodes, ReachableAllocNodes);
252}
253
Chris Lattner7d093d42002-03-28 19:16:48 +0000254bool FunctionDSGraph::RemoveUnreachableNodes() {
Chris Lattner9d42a1d2002-03-27 00:55:13 +0000255 bool Changed = false;
Chris Lattner212be2e2002-04-16 03:44:03 +0000256 bool LocalChange = true;
257
258 while (LocalChange) {
259 LocalChange = false;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000260 // Reachable*Nodes - Contains true if there is an edge from a reachable
261 // node to the numbered node...
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000262 //
Chris Lattner1120c8b2002-03-28 17:56:03 +0000263 vector<bool> ReachableShadowNodes(ShadowNodes.size());
264 vector<bool> ReachableAllocNodes (AllocNodes.size());
Chris Lattner4dc1f822002-03-28 19:33:00 +0000265
266 MarkEscapeableNodesReachable(ReachableShadowNodes, ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000267
268 // Mark all nodes in the value map as being reachable...
269 for (std::map<Value*, PointerValSet>::iterator I = ValueMap.begin(),
270 E = ValueMap.end(); I != E; ++I)
Chris Lattner1120c8b2002-03-28 17:56:03 +0000271 MarkReferredNodeSetReachable(I->second,
272 ShadowNodes, ReachableShadowNodes,
273 AllocNodes, ReachableAllocNodes);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000274
275 // At this point, all reachable shadow nodes have a true value in the
276 // Reachable vector. This means that any shadow nodes without an entry in
277 // the reachable vector are not reachable and should be removed. This is
278 // a two part process, because we must drop all references before we delete
279 // the shadow nodes [in case cycles exist].
280 //
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000281 for (unsigned i = 0; i != ShadowNodes.size(); ++i)
Chris Lattner1120c8b2002-03-28 17:56:03 +0000282 if (!ReachableShadowNodes[i]) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000283 // Track all unreachable nodes...
Chris Lattner2ba3a722002-03-27 19:48:03 +0000284#if DEBUG_NODE_ELIMINATE
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000285 cerr << "Unreachable node eliminated:\n";
286 ShadowNodes[i]->print(cerr);
287#endif
Chris Lattner1120c8b2002-03-28 17:56:03 +0000288 ShadowNodes[i]->removeAllIncomingEdges();
289 delete ShadowNodes[i];
290
291 // Remove from reachable...
292 ReachableShadowNodes.erase(ReachableShadowNodes.begin()+i);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000293 ShadowNodes.erase(ShadowNodes.begin()+i); // Remove node entry
294 --i; // Don't skip the next node.
Chris Lattner212be2e2002-04-16 03:44:03 +0000295 LocalChange = Changed = true;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000296 }
297
Chris Lattner1120c8b2002-03-28 17:56:03 +0000298 for (unsigned i = 0; i != AllocNodes.size(); ++i)
299 if (!ReachableAllocNodes[i]) {
300 // Track all unreachable nodes...
301#if DEBUG_NODE_ELIMINATE
302 cerr << "Unreachable node eliminated:\n";
303 AllocNodes[i]->print(cerr);
304#endif
305 AllocNodes[i]->removeAllIncomingEdges();
306 delete AllocNodes[i];
307
308 // Remove from reachable...
309 ReachableAllocNodes.erase(ReachableAllocNodes.begin()+i);
310 AllocNodes.erase(AllocNodes.begin()+i); // Remove node entry
311 --i; // Don't skip the next node.
Chris Lattner212be2e2002-04-16 03:44:03 +0000312 LocalChange = Changed = true;
Chris Lattner1120c8b2002-03-28 17:56:03 +0000313 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000314 }
Chris Lattner212be2e2002-04-16 03:44:03 +0000315
Chris Lattner7650b942002-04-16 20:39:59 +0000316 // Loop over the global nodes, removing nodes that have no edges into them or
317 // out of them.
318 //
Chris Lattner212be2e2002-04-16 03:44:03 +0000319 for (std::vector<GlobalDSNode*>::iterator I = GlobalNodes.begin();
320 I != GlobalNodes.end(); )
Chris Lattner7650b942002-04-16 20:39:59 +0000321 if ((*I)->getReferrers().empty()) {
322 GlobalDSNode *GDN = *I;
323 bool NoLinks = true; // Make sure there are no outgoing links...
324 for (unsigned i = 0, e = GDN->getNumLinks(); i != e; ++i)
325 if (!GDN->getLink(i).empty()) {
326 NoLinks = false;
327 break;
328 }
329 if (NoLinks) {
330 delete GDN;
331 I = GlobalNodes.erase(I); // Remove the node...
332 Changed = true;
333 } else {
334 ++I;
335 }
Chris Lattner212be2e2002-04-16 03:44:03 +0000336 } else {
337 ++I;
338 }
Chris Lattner7650b942002-04-16 20:39:59 +0000339
Chris Lattner212be2e2002-04-16 03:44:03 +0000340 return Changed;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000341}
Chris Lattner4dc1f822002-03-28 19:33:00 +0000342
343
344
345
346// getEscapingAllocations - Add all allocations that escape the current
347// function to the specified vector.
348//
349void FunctionDSGraph::getEscapingAllocations(vector<AllocDSNode*> &Allocs) {
350 vector<bool> ReachableShadowNodes(ShadowNodes.size());
351 vector<bool> ReachableAllocNodes (AllocNodes.size());
352
353 MarkEscapeableNodesReachable(ReachableShadowNodes, ReachableAllocNodes);
354
355 for (unsigned i = 0, e = AllocNodes.size(); i != e; ++i)
356 if (ReachableAllocNodes[i])
357 Allocs.push_back(AllocNodes[i]);
358}
359
360// getNonEscapingAllocations - Add all allocations that do not escape the
361// current function to the specified vector.
362//
363void FunctionDSGraph::getNonEscapingAllocations(vector<AllocDSNode*> &Allocs) {
364 vector<bool> ReachableShadowNodes(ShadowNodes.size());
365 vector<bool> ReachableAllocNodes (AllocNodes.size());
366
367 MarkEscapeableNodesReachable(ReachableShadowNodes, ReachableAllocNodes);
368
369 for (unsigned i = 0, e = AllocNodes.size(); i != e; ++i)
370 if (!ReachableAllocNodes[i])
371 Allocs.push_back(AllocNodes[i]);
372}