blob: 800c82ad9823e927f8d9790feed8e58907f05477 [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- DataStructure.cpp - Implement the core data structure analysis -----===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00002//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00003// This file implements the core data structure functionality.
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00004//
5//===----------------------------------------------------------------------===//
6
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00007#include "llvm/Module.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +00008#include "llvm/DerivedTypes.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009#include "Support/STLExtras.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000010#include "Support/StatisticReporter.h"
11#include <algorithm>
12#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000013
14AnalysisID LocalDataStructures::ID(AnalysisID::create<LocalDataStructures>());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000015
16//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017// DSNode Implementation
18//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000019
Chris Lattnerc68c31b2002-07-10 22:38:08 +000020DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
21 // If this node has any fields, allocate them now, but leave them null.
22 switch (T->getPrimitiveID()) {
23 case Type::PointerTyID: Links.resize(1); break;
24 case Type::ArrayTyID: Links.resize(1); break;
25 case Type::StructTyID:
26 Links.resize(cast<StructType>(T)->getNumContainedTypes());
27 break;
28 default: break;
29 }
30}
31
Chris Lattner0d9bab82002-07-18 00:12:30 +000032// DSNode copy constructor... do not copy over the referrers list!
33DSNode::DSNode(const DSNode &N)
34 : Ty(N.Ty), Links(N.Links), Globals(N.Globals), NodeType(N.NodeType) {
35}
36
Chris Lattnerc68c31b2002-07-10 22:38:08 +000037void DSNode::removeReferrer(DSNodeHandle *H) {
38 // Search backwards, because we depopulate the list from the back for
39 // efficiency (because it's a vector).
40 std::vector<DSNodeHandle*>::reverse_iterator I =
41 std::find(Referrers.rbegin(), Referrers.rend(), H);
42 assert(I != Referrers.rend() && "Referrer not pointing to node!");
43 Referrers.erase(I.base()-1);
44}
45
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000046// addGlobal - Add an entry for a global value to the Globals list. This also
47// marks the node with the 'G' flag if it does not already have it.
48//
49void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000050 // Keep the list sorted.
51 std::vector<GlobalValue*>::iterator I =
52 std::lower_bound(Globals.begin(), Globals.end(), GV);
53
54 if (I == Globals.end() || *I != GV) {
55 assert(GV->getType()->getElementType() == Ty);
56 Globals.insert(I, GV);
57 NodeType |= GlobalNode;
58 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000059}
60
61
Chris Lattnerc68c31b2002-07-10 22:38:08 +000062// addEdgeTo - Add an edge from the current node to the specified node. This
63// can cause merging of nodes in the graph.
64//
65void DSNode::addEdgeTo(unsigned LinkNo, DSNode *N) {
66 assert(LinkNo < Links.size() && "LinkNo out of range!");
67 if (N == 0 || Links[LinkNo] == N) return; // Nothing to do
68 if (Links[LinkNo] == 0) { // No merging to perform
69 Links[LinkNo] = N;
70 return;
71 }
72
73 // Merge the two nodes...
74 Links[LinkNo]->mergeWith(N);
75}
76
77
78// mergeWith - Merge this node into the specified node, moving all links to and
79// from the argument node into the current node. The specified node may be a
80// null pointer (in which case, nothing happens).
81//
82void DSNode::mergeWith(DSNode *N) {
83 if (N == 0 || N == this) return; // Noop
84 assert(N->Ty == Ty && N->Links.size() == Links.size() &&
85 "Cannot merge nodes of two different types!");
86
87 // Remove all edges pointing at N, causing them to point to 'this' instead.
88 while (!N->Referrers.empty())
89 *N->Referrers.back() = this;
90
91 // Make all of the outgoing links of N now be outgoing links of this. This
92 // can cause recursive merging!
93 //
94 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
95 addEdgeTo(i, N->Links[i]);
96 N->Links[i] = 0; // Reduce unneccesary edges in graph. N is dead
97 }
98
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000099 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000100 NodeType |= N->NodeType;
101 N->NodeType = 0; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000102
103 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000104 if (!N->Globals.empty()) {
105 // Save the current globals off to the side...
106 std::vector<GlobalValue*> OldGlobals(Globals);
107
108 // Resize the globals vector to be big enough to hold both of them...
109 Globals.resize(Globals.size()+N->Globals.size());
110
111 // Merge the two sorted globals lists together...
112 std::merge(OldGlobals.begin(), OldGlobals.end(),
113 N->Globals.begin(), N->Globals.end(), Globals.begin());
114
115 // Erase duplicate entries from the globals list...
116 Globals.erase(std::unique(Globals.begin(), Globals.end()), Globals.end());
117
118 // Delete the globals from the old node...
119 N->Globals.clear();
120 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000121}
122
123//===----------------------------------------------------------------------===//
124// DSGraph Implementation
125//===----------------------------------------------------------------------===//
126
Chris Lattner0d9bab82002-07-18 00:12:30 +0000127DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
128 RetNode = cloneInto(G, ValueMap, false);
129}
130
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000131DSGraph::~DSGraph() {
132 FunctionCalls.clear();
133 ValueMap.clear();
134 RetNode = 0;
135
136#ifndef NDEBUG
137 // Drop all intra-node references, so that assertions don't fail...
138 std::for_each(Nodes.begin(), Nodes.end(),
139 std::mem_fun(&DSNode::dropAllReferences));
140#endif
141
142 // Delete all of the nodes themselves...
143 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
144}
145
Chris Lattner0d9bab82002-07-18 00:12:30 +0000146// dump - Allow inspection of graph in a debugger.
147void DSGraph::dump() const { print(std::cerr); }
148
149// cloneInto - Clone the specified DSGraph into the current graph, returning the
150// Return node of the graph. The translated ValueMap for the old function is
151// filled into the OldValMap member. If StripLocals is set to true, Scalar and
152// Alloca markers are removed from the graph, as the graph is being cloned into
153// a calling function's graph.
154//
155DSNode *DSGraph::cloneInto(const DSGraph &G,
156 std::map<Value*, DSNodeHandle> &OldValMap,
157 bool StripLocals) {
158 std::map<const DSNode*, DSNode*> NodeMap;
159 NodeMap[0] = 0; // Null pointer maps to null
160
161 unsigned FN = Nodes.size(); // FirstNode...
162
163 // Duplicate all of the nodes, populating the node map...
164 Nodes.reserve(FN+G.Nodes.size());
165 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
166 DSNode *Old = G.Nodes[i], *New = new DSNode(*Old);
167 Nodes.push_back(New);
168 NodeMap[Old] = New;
169 }
170
171 // Rewrite the links in the nodes to point into the current graph now.
172 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
173 for (unsigned j = 0, e = Nodes[i]->getNumLinks(); j != e; ++j)
174 Nodes[i]->setLink(j, NodeMap[Nodes[i]->getLink(j)]);
175
176 // If we are inlining this graph into the called function graph, remove local
177 // markers.
178 if (StripLocals)
179 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
180 Nodes[i]->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
181
182 // Copy the value map...
183 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(),
184 E = G.ValueMap.end(); I != E; ++I)
185 OldValMap[I->first] = NodeMap[I->second];
186
187 // Copy the function calls list...
188 unsigned FC = FunctionCalls.size(); // FirstCall
189 FunctionCalls.reserve(FC+G.FunctionCalls.size());
190 for (unsigned i = 0, e = G.FunctionCalls.size(); i != e; ++i) {
191 FunctionCalls.push_back(std::vector<DSNodeHandle>());
192 FunctionCalls[FC+i].reserve(G.FunctionCalls[i].size());
193 for (unsigned j = 0, e = G.FunctionCalls[i].size(); j != e; ++j)
194 FunctionCalls[FC+i].push_back(NodeMap[G.FunctionCalls[i][j]]);
195 }
196
197 // Return the returned node pointer...
198 return NodeMap[G.RetNode];
199}
200
201
202// markIncompleteNodes - Mark the specified node as having contents that are not
203// known with the current analysis we have performed. Because a node makes all
204// of the nodes it can reach imcomplete if the node itself is incomplete, we
205// must recursively traverse the data structure graph, marking all reachable
206// nodes as incomplete.
207//
208static void markIncompleteNode(DSNode *N) {
209 // Stop recursion if no node, or if node already marked...
210 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
211
212 // Actually mark the node
213 N->NodeType |= DSNode::Incomplete;
214
215 // Recusively process children...
216 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
217 markIncompleteNode(N->getLink(i));
218}
219
220
221// markIncompleteNodes - Traverse the graph, identifying nodes that may be
222// modified by other functions that have not been resolved yet. This marks
223// nodes that are reachable through three sources of "unknownness":
224//
225// Global Variables, Function Calls, and Incoming Arguments
226//
227// For any node that may have unknown components (because something outside the
228// scope of current analysis may have modified it), the 'Incomplete' flag is
229// added to the NodeType.
230//
231void DSGraph::markIncompleteNodes() {
232 // Mark any incoming arguments as incomplete...
233 for (Function::aiterator I = Func.abegin(), E = Func.aend(); I != E; ++I)
234 if (isa<PointerType>(I->getType()))
235 markIncompleteNode(ValueMap[I]->getLink(0));
236
237 // Mark stuff passed into functions calls as being incomplete...
238 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
239 std::vector<DSNodeHandle> &Args = FunctionCalls[i];
240 if (Args[0]) // If the call returns a pointer...
241 // Then the return value is certainly incomplete!
242 markIncompleteNode(Args[0]);
243
244 // The call does not make the function argument incomplete...
245
246 // All arguments to the function call are incomplete though!
247 for (unsigned i = 2, e = Args.size(); i != e; ++i)
248 markIncompleteNode(Args[i]);
249 }
250
251 // Mark all of the nodes pointed to by global nodes as incomplete...
252 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
253 if (Nodes[i]->NodeType & DSNode::GlobalNode) {
254 DSNode *N = Nodes[i];
255 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
256 markIncompleteNode(N->getLink(i));
257 }
258}
259
260// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
261// checks to see if there are simple transformations that it can do to make it
262// dead.
263//
264bool DSGraph::isNodeDead(DSNode *N) {
265 // Is it a trivially dead shadow node...
266 if (N->getReferrers().empty() && N->NodeType == 0)
267 return true;
268
269 // Is it a function node or some other trivially unused global?
270 if ((N->NodeType & ~DSNode::GlobalNode) == 0 &&
271 N->getNumLinks() == 0 &&
272 N->getReferrers().size() == N->getGlobals().size()) {
273
274 // Remove the globals from the valuemap, so that the referrer count will go
275 // down to zero.
276 while (!N->getGlobals().empty()) {
277 GlobalValue *GV = N->getGlobals().back();
278 N->getGlobals().pop_back();
279 ValueMap.erase(GV);
280 }
281 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
282 return true;
283 }
284
285 return false;
286}
287
288
289// removeDeadNodes - After the graph has been constructed, this method removes
290// all unreachable nodes that are created because they got merged with other
291// nodes in the graph. These nodes will all be trivially unreachable, so we
292// don't have to perform any non-trivial analysis here.
293//
294void DSGraph::removeDeadNodes() {
295 for (unsigned i = 0; i != Nodes.size(); ++i)
296 if (isNodeDead(Nodes[i])) { // This node is dead!
297 delete Nodes[i]; // Free memory...
298 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
299 }
300
301 // Remove identical function calls
302 unsigned NumFns = FunctionCalls.size();
303 std::sort(FunctionCalls.begin(), FunctionCalls.end());
304 FunctionCalls.erase(std::unique(FunctionCalls.begin(), FunctionCalls.end()),
305 FunctionCalls.end());
306
307 DEBUG(if (NumFns != FunctionCalls.size())
308 std::cerr << "Merged " << (NumFns-FunctionCalls.size())
309 << " call nodes in " << Func.getName() << "\n";);
310}
311
312
313// maskNodeTypes - Apply a mask to all of the node types in the graph. This
314// is useful for clearing out markers like Scalar or Incomplete.
315//
316void DSGraph::maskNodeTypes(unsigned char Mask) {
317 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
318 Nodes[i]->NodeType &= Mask;
319}
320
321
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000322//===----------------------------------------------------------------------===//
323// LocalDataStructures Implementation
324//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000325
326// releaseMemory - If the pass pipeline is done with this pass, we can release
327// our memory... here...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000328//
329void LocalDataStructures::releaseMemory() {
330 for (std::map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
331 E = DSInfo.end(); I != E; ++I)
332 delete I->second;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000333
334 // Empty map so next time memory is released, data structures are not
335 // re-deleted.
336 DSInfo.clear();
337}
338
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000339bool LocalDataStructures::run(Module &M) {
340 // Calculate all of the graphs...
341 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000342 if (!I->isExternal())
343 DSInfo.insert(std::make_pair(&*I, new DSGraph(*I)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000344
345 return false;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000346}