blob: a3234d19048f2f74ff22892c7b48628d684994a7 [file] [log] [blame]
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00001//===- ShadowNodeEliminate.cpp - Optimize away shadow nodes ---------------===//
2//
3// This file contains two shadow node optimizations:
4// 1. UnlinkUndistinguishableShadowNodes - Often, after unification, shadow
5// 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//
10// 2. RemoveUnreachableShadowNodes - Remove shadow nodes that are not
11// reachable from some other node in the graph. Unreachable shadow nodes
12// are left lying around because other transforms don't go to the trouble
13// or removing them, since this pass exists.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/DataStructure.h"
18#include "llvm/Value.h"
19#include "Support/STLExtras.h"
20#include <algorithm>
21
Chris Lattner9d42a1d2002-03-27 00:55:13 +000022// NodesAreEquivalent - Check to see if the nodes are equivalent in all ways
23// except node type. Since we know N1 is a shadow node, N2 is allowed to be
24// any type.
25//
26static bool NodesAreEquivalent(const ShadowDSNode *N1, const DSNode *N2) {
27 assert(N1 != N2 && "A node is always equivalent to itself!");
28
29 // Perform simple, fast checks first...
30 if (N1->getType() != N2->getType()) return false;
31 assert(N1->getNumLinks() == N2->getNumLinks() && "Same type, diff # links?");
32
33 // The shadow node is considered equivalent if it has a subset of the incoming
34 // edges that N2 does...
35 if (N1->getReferrers().size() > N2->getReferrers().size()) return false;
36
37 // Check to see if the referring (incoming) pointers are all the same...
38 std::vector<PointerValSet*> N1R = N1->getReferrers();
39 std::vector<PointerValSet*> N2R = N2->getReferrers();
40 sort(N1R.begin(), N1R.end());
41 sort(N2R.begin(), N2R.end());
42
43 // The nodes are equivalent if the incoming edges to N1 are a subset of N2.
44 unsigned i1 = 0, e1 = N1R.size();
45 unsigned i2 = 0, e2 = N2R.size();
46 for (; i1 != e1 && i2 < e2; ++i1, ++i2) {
47 while (N1R[i1] > N2R[i2] && i2 < e2)
48 ++i2;
49
50 if (N1R[i1] < N2R[i2]) return false; // Error case...
51 }
52
53 return i1 == e1 && i2 <= e2;
54}
55
56// IndistinguishableShadowNode - A shadow node is indistinguishable if some
57// other node (shadow or otherwise) has exactly the same incoming and outgoing
58// links to it (or if there are no edges coming in, in which it is trivially
59// dead).
60//
61static bool IndistinguishableShadowNode(const ShadowDSNode *SN) {
62 if (SN->getReferrers().empty()) return true; // Node is trivially dead
63
64 // Pick a random referrer... Ptr is the things that the referrer points to.
65 // Since SN is in the Ptr set, look through the set seeing if there are any
66 // other nodes that are exactly equilivant to SN (with the exception of node
67 // type), but are not SN. If anything exists, then SN is indistinguishable.
68 //
69 const PointerValSet &Ptr = *SN->getReferrers()[0];
70
71 for (unsigned i = 0, e = Ptr.size(); i != e; ++i)
72 if (Ptr[i].Index == 0 && Ptr[i].Node != cast<DSNode>(SN) &&
73 NodesAreEquivalent(SN, Ptr[i].Node))
74 return true;
75
76 // Otherwise, nothing found, perhaps next time....
77 return false;
78}
79
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000080// removeEdgesTo - Erase all edges in the graph that point to the specified node
81static void removeEdgesTo(DSNode *Node) {
82 while (!Node->getReferrers().empty()) {
83 PointerValSet *PVS = Node->getReferrers().back();
84 PVS->removePointerTo(Node);
85 }
86}
87
88// UnlinkUndistinguishableShadowNodes - Eliminate shadow nodes that are not
89// distinguishable from some other node in the graph...
90//
Chris Lattner9d42a1d2002-03-27 00:55:13 +000091bool FunctionDSGraph::UnlinkUndistinguishableShadowNodes() {
92 bool Changed = false;
93 // Loop over all of the shadow nodes, checking to see if they are
94 // indistinguishable from some other node. If so, eliminate the node!
95 //
96 for (vector<ShadowDSNode*>::iterator I = ShadowNodes.begin();
97 I != ShadowNodes.end(); )
98 if (IndistinguishableShadowNode(*I)) {
99 cerr << "Found Indistinguishable Shadow Node:\n";
100 (*I)->print(cerr);
101 removeEdgesTo(*I);
102 // Don't need to dropAllRefs, because nothing can poitn to it now
103 delete *I;
104
105 I = ShadowNodes.erase(I);
106 Changed = true;
107 } else {
108 ++I;
109 }
110 return Changed;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000111}
112
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000113static void MarkReferredNodesReachable(DSNode *N, vector<ShadowDSNode*> &Nodes,
114 vector<bool> &Reachable);
115
116static inline void MarkReferredNodeSetReachable(const PointerValSet &PVS,
117 vector<ShadowDSNode*> &Nodes,
118 vector<bool> &Reachable) {
119 for (unsigned i = 0, e = PVS.size(); i != e; ++i)
120 if (ShadowDSNode *Shad = dyn_cast<ShadowDSNode>(PVS[i].Node))
121 MarkReferredNodesReachable(Shad, Nodes, Reachable);
122}
123
124static void MarkReferredNodesReachable(DSNode *N, vector<ShadowDSNode*> &Nodes,
125 vector<bool> &Reachable) {
126 assert(Nodes.size() == Reachable.size());
127 ShadowDSNode *Shad = dyn_cast<ShadowDSNode>(N);
128
129 if (Shad) {
130 vector<ShadowDSNode*>::iterator I =
131 std::find(Nodes.begin(), Nodes.end(), Shad);
132 unsigned i = I-Nodes.begin();
133 if (Reachable[i]) return; // Recursion detected, abort...
134 Reachable[i] = true;
135 }
136
137 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
138 MarkReferredNodeSetReachable(N->getLink(i), Nodes, Reachable);
139
140 const std::vector<PointerValSet> *Links = N->getAuxLinks();
141 if (Links)
142 for (unsigned i = 0, e = Links->size(); i != e; ++i)
143 MarkReferredNodeSetReachable((*Links)[i], Nodes, Reachable);
144}
145
Chris Lattner9d42a1d2002-03-27 00:55:13 +0000146bool FunctionDSGraph::RemoveUnreachableShadowNodes() {
147 bool Changed = false;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000148 while (1) {
149
150 // Reachable - Contains true if there is an edge from a nonshadow node to
151 // the numbered node...
152 //
153 vector<bool> Reachable(ShadowNodes.size());
154
155 // Mark all shadow nodes that have edges from other nodes as reachable.
156 // Recursively mark any shadow nodes pointed to by the newly live shadow
157 // nodes as also alive.
158 //
159 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
160 // Loop over all of the nodes referred and mark them live if they are
161 // shadow nodes...
162 MarkReferredNodesReachable(Nodes[i], ShadowNodes, Reachable);
163
164 // Mark all nodes in the return set as being reachable...
165 MarkReferredNodeSetReachable(RetNode, ShadowNodes, Reachable);
166
167 // Mark all nodes in the value map as being reachable...
168 for (std::map<Value*, PointerValSet>::iterator I = ValueMap.begin(),
169 E = ValueMap.end(); I != E; ++I)
170 MarkReferredNodeSetReachable(I->second, ShadowNodes, Reachable);
171
172
173 // At this point, all reachable shadow nodes have a true value in the
174 // Reachable vector. This means that any shadow nodes without an entry in
175 // the reachable vector are not reachable and should be removed. This is
176 // a two part process, because we must drop all references before we delete
177 // the shadow nodes [in case cycles exist].
178 //
179 vector<ShadowDSNode*> DeadNodes;
180 for (unsigned i = 0; i != ShadowNodes.size(); ++i)
181 if (!Reachable[i]) {
182 // Track all unreachable nodes...
183#if 0
184 cerr << "Unreachable node eliminated:\n";
185 ShadowNodes[i]->print(cerr);
186#endif
187 DeadNodes.push_back(ShadowNodes[i]);
188 ShadowNodes[i]->dropAllReferences(); // Drop references to other nodes
189 Reachable.erase(Reachable.begin()+i); // Remove from reachable...
190 ShadowNodes.erase(ShadowNodes.begin()+i); // Remove node entry
191 --i; // Don't skip the next node.
192 }
193
Chris Lattner9d42a1d2002-03-27 00:55:13 +0000194 if (DeadNodes.empty()) return Changed; // No more dead nodes...
195
196 Changed = true;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000197
198 // All dead nodes are in the DeadNodes vector... delete them now.
199 for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
200 }
201}