blob: f573a0be1a639e05d30caf1d6aee9b81b34d47c5 [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"
Chris Lattnere2219762002-07-18 18:22:40 +000011#include "Support/STLExtras.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000012#include <algorithm>
Chris Lattnere2219762002-07-18 18:22:40 +000013#include <set>
Chris Lattner0d9bab82002-07-18 00:12:30 +000014#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000015
Chris Lattnere2219762002-07-18 18:22:40 +000016using std::vector;
17
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterAnalysis<LocalDataStructures>
19X("datastructure", "Local Data Structure Analysis");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000020AnalysisID LocalDataStructures::ID(AnalysisID::create<LocalDataStructures>());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000021
22//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +000023// DSNode Implementation
24//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000025
Chris Lattnerc68c31b2002-07-10 22:38:08 +000026DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
27 // If this node has any fields, allocate them now, but leave them null.
28 switch (T->getPrimitiveID()) {
29 case Type::PointerTyID: Links.resize(1); break;
30 case Type::ArrayTyID: Links.resize(1); break;
31 case Type::StructTyID:
32 Links.resize(cast<StructType>(T)->getNumContainedTypes());
33 break;
34 default: break;
35 }
36}
37
Chris Lattner0d9bab82002-07-18 00:12:30 +000038// DSNode copy constructor... do not copy over the referrers list!
39DSNode::DSNode(const DSNode &N)
40 : Ty(N.Ty), Links(N.Links), Globals(N.Globals), NodeType(N.NodeType) {
41}
42
Chris Lattnerc68c31b2002-07-10 22:38:08 +000043void DSNode::removeReferrer(DSNodeHandle *H) {
44 // Search backwards, because we depopulate the list from the back for
45 // efficiency (because it's a vector).
Chris Lattnere2219762002-07-18 18:22:40 +000046 vector<DSNodeHandle*>::reverse_iterator I =
Chris Lattnerc68c31b2002-07-10 22:38:08 +000047 std::find(Referrers.rbegin(), Referrers.rend(), H);
48 assert(I != Referrers.rend() && "Referrer not pointing to node!");
49 Referrers.erase(I.base()-1);
50}
51
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000052// addGlobal - Add an entry for a global value to the Globals list. This also
53// marks the node with the 'G' flag if it does not already have it.
54//
55void DSNode::addGlobal(GlobalValue *GV) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000056 // Keep the list sorted.
Chris Lattnere2219762002-07-18 18:22:40 +000057 vector<GlobalValue*>::iterator I =
Chris Lattner0d9bab82002-07-18 00:12:30 +000058 std::lower_bound(Globals.begin(), Globals.end(), GV);
59
60 if (I == Globals.end() || *I != GV) {
61 assert(GV->getType()->getElementType() == Ty);
62 Globals.insert(I, GV);
63 NodeType |= GlobalNode;
64 }
Chris Lattnerf9ae4c52002-07-11 20:32:22 +000065}
66
67
Chris Lattnerc68c31b2002-07-10 22:38:08 +000068// addEdgeTo - Add an edge from the current node to the specified node. This
69// can cause merging of nodes in the graph.
70//
71void DSNode::addEdgeTo(unsigned LinkNo, DSNode *N) {
72 assert(LinkNo < Links.size() && "LinkNo out of range!");
73 if (N == 0 || Links[LinkNo] == N) return; // Nothing to do
74 if (Links[LinkNo] == 0) { // No merging to perform
75 Links[LinkNo] = N;
76 return;
77 }
78
79 // Merge the two nodes...
80 Links[LinkNo]->mergeWith(N);
81}
82
83
84// mergeWith - Merge this node into the specified node, moving all links to and
85// from the argument node into the current node. The specified node may be a
86// null pointer (in which case, nothing happens).
87//
88void DSNode::mergeWith(DSNode *N) {
89 if (N == 0 || N == this) return; // Noop
90 assert(N->Ty == Ty && N->Links.size() == Links.size() &&
91 "Cannot merge nodes of two different types!");
92
93 // Remove all edges pointing at N, causing them to point to 'this' instead.
94 while (!N->Referrers.empty())
95 *N->Referrers.back() = this;
96
97 // Make all of the outgoing links of N now be outgoing links of this. This
98 // can cause recursive merging!
99 //
100 for (unsigned i = 0, e = Links.size(); i != e; ++i) {
101 addEdgeTo(i, N->Links[i]);
102 N->Links[i] = 0; // Reduce unneccesary edges in graph. N is dead
103 }
104
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000105 // Merge the node types
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000106 NodeType |= N->NodeType;
107 N->NodeType = 0; // N is now a dead node.
Chris Lattnerf9ae4c52002-07-11 20:32:22 +0000108
109 // Merge the globals list...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000110 if (!N->Globals.empty()) {
111 // Save the current globals off to the side...
Chris Lattnere2219762002-07-18 18:22:40 +0000112 vector<GlobalValue*> OldGlobals(Globals);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000113
114 // Resize the globals vector to be big enough to hold both of them...
115 Globals.resize(Globals.size()+N->Globals.size());
116
117 // Merge the two sorted globals lists together...
118 std::merge(OldGlobals.begin(), OldGlobals.end(),
119 N->Globals.begin(), N->Globals.end(), Globals.begin());
120
121 // Erase duplicate entries from the globals list...
122 Globals.erase(std::unique(Globals.begin(), Globals.end()), Globals.end());
123
124 // Delete the globals from the old node...
125 N->Globals.clear();
126 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000127}
128
129//===----------------------------------------------------------------------===//
130// DSGraph Implementation
131//===----------------------------------------------------------------------===//
132
Chris Lattner0d9bab82002-07-18 00:12:30 +0000133DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000134 std::map<const DSNode*, DSNode*> NodeMap; // ignored
135 RetNode = cloneInto(G, ValueMap, NodeMap, false);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000136}
137
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000138DSGraph::~DSGraph() {
139 FunctionCalls.clear();
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000140 OrigFunctionCalls.clear();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000141 ValueMap.clear();
142 RetNode = 0;
143
144#ifndef NDEBUG
145 // Drop all intra-node references, so that assertions don't fail...
146 std::for_each(Nodes.begin(), Nodes.end(),
147 std::mem_fun(&DSNode::dropAllReferences));
148#endif
149
150 // Delete all of the nodes themselves...
151 std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>);
152}
153
Chris Lattner0d9bab82002-07-18 00:12:30 +0000154// dump - Allow inspection of graph in a debugger.
155void DSGraph::dump() const { print(std::cerr); }
156
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000157
Chris Lattner0d9bab82002-07-18 00:12:30 +0000158// cloneInto - Clone the specified DSGraph into the current graph, returning the
159// Return node of the graph. The translated ValueMap for the old function is
160// filled into the OldValMap member. If StripLocals is set to true, Scalar and
161// Alloca markers are removed from the graph, as the graph is being cloned into
162// a calling function's graph.
163//
164DSNode *DSGraph::cloneInto(const DSGraph &G,
165 std::map<Value*, DSNodeHandle> &OldValMap,
Chris Lattnere2219762002-07-18 18:22:40 +0000166 std::map<const DSNode*, DSNode*> &OldNodeMap,
Chris Lattner0d9bab82002-07-18 00:12:30 +0000167 bool StripLocals) {
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000168
169 assert(OldNodeMap.size()==0 && "Return argument OldNodeMap should be empty");
170
171 OldNodeMap[0] = 0; // Null pointer maps to null
Chris Lattner0d9bab82002-07-18 00:12:30 +0000172
173 unsigned FN = Nodes.size(); // FirstNode...
174
175 // Duplicate all of the nodes, populating the node map...
176 Nodes.reserve(FN+G.Nodes.size());
177 for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) {
178 DSNode *Old = G.Nodes[i], *New = new DSNode(*Old);
179 Nodes.push_back(New);
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000180 OldNodeMap[Old] = New;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000181 }
182
183 // Rewrite the links in the nodes to point into the current graph now.
184 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
185 for (unsigned j = 0, e = Nodes[i]->getNumLinks(); j != e; ++j)
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000186 Nodes[i]->setLink(j, OldNodeMap[Nodes[i]->getLink(j)]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000187
188 // If we are inlining this graph into the called function graph, remove local
189 // markers.
190 if (StripLocals)
191 for (unsigned i = FN, e = Nodes.size(); i != e; ++i)
192 Nodes[i]->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode);
193
194 // Copy the value map...
195 for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(),
196 E = G.ValueMap.end(); I != E; ++I)
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000197 OldValMap[I->first] = OldNodeMap[I->second];
Chris Lattner0d9bab82002-07-18 00:12:30 +0000198
Chris Lattnere2219762002-07-18 18:22:40 +0000199 // Copy the function calls list...
200 unsigned FC = FunctionCalls.size(); // FirstCall
201 FunctionCalls.reserve(FC+G.FunctionCalls.size());
202 for (unsigned i = 0, e = G.FunctionCalls.size(); i != e; ++i) {
203 FunctionCalls.push_back(std::vector<DSNodeHandle>());
204 FunctionCalls[FC+i].reserve(G.FunctionCalls[i].size());
205 for (unsigned j = 0, e = G.FunctionCalls[i].size(); j != e; ++j)
206 FunctionCalls[FC+i].push_back(OldNodeMap[G.FunctionCalls[i][j]]);
207 }
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000208
209 // Copy the list of unresolved callers
210 PendingCallers.insert(PendingCallers.end(),
211 G.PendingCallers.begin(), G.PendingCallers.end());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000212
213 // Return the returned node pointer...
Vikram S. Adve6aa0d622002-07-18 16:12:08 +0000214 return OldNodeMap[G.RetNode];
Chris Lattner0d9bab82002-07-18 00:12:30 +0000215}
216
217
218// markIncompleteNodes - Mark the specified node as having contents that are not
219// known with the current analysis we have performed. Because a node makes all
220// of the nodes it can reach imcomplete if the node itself is incomplete, we
221// must recursively traverse the data structure graph, marking all reachable
222// nodes as incomplete.
223//
224static void markIncompleteNode(DSNode *N) {
225 // Stop recursion if no node, or if node already marked...
226 if (N == 0 || (N->NodeType & DSNode::Incomplete)) return;
227
228 // Actually mark the node
229 N->NodeType |= DSNode::Incomplete;
230
231 // Recusively process children...
232 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
233 markIncompleteNode(N->getLink(i));
234}
235
236
237// markIncompleteNodes - Traverse the graph, identifying nodes that may be
238// modified by other functions that have not been resolved yet. This marks
239// nodes that are reachable through three sources of "unknownness":
240//
241// Global Variables, Function Calls, and Incoming Arguments
242//
243// For any node that may have unknown components (because something outside the
244// scope of current analysis may have modified it), the 'Incomplete' flag is
245// added to the NodeType.
246//
247void DSGraph::markIncompleteNodes() {
248 // Mark any incoming arguments as incomplete...
249 for (Function::aiterator I = Func.abegin(), E = Func.aend(); I != E; ++I)
250 if (isa<PointerType>(I->getType()))
251 markIncompleteNode(ValueMap[I]->getLink(0));
252
253 // Mark stuff passed into functions calls as being incomplete...
254 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
Chris Lattnere2219762002-07-18 18:22:40 +0000255 vector<DSNodeHandle> &Args = FunctionCalls[i];
256 // Then the return value is certainly incomplete!
257 markIncompleteNode(Args[0]);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000258
259 // The call does not make the function argument incomplete...
260
261 // All arguments to the function call are incomplete though!
262 for (unsigned i = 2, e = Args.size(); i != e; ++i)
263 markIncompleteNode(Args[i]);
264 }
265
Chris Lattner055dc2c2002-07-18 15:54:42 +0000266 // Mark all of the nodes pointed to by global or cast nodes as incomplete...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000267 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner055dc2c2002-07-18 15:54:42 +0000268 if (Nodes[i]->NodeType & (DSNode::GlobalNode | DSNode::CastNode)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000269 DSNode *N = Nodes[i];
270 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
271 markIncompleteNode(N->getLink(i));
272 }
273}
274
275// isNodeDead - This method checks to see if a node is dead, and if it isn't, it
276// checks to see if there are simple transformations that it can do to make it
277// dead.
278//
279bool DSGraph::isNodeDead(DSNode *N) {
280 // Is it a trivially dead shadow node...
281 if (N->getReferrers().empty() && N->NodeType == 0)
282 return true;
283
284 // Is it a function node or some other trivially unused global?
285 if ((N->NodeType & ~DSNode::GlobalNode) == 0 &&
286 N->getNumLinks() == 0 &&
287 N->getReferrers().size() == N->getGlobals().size()) {
288
289 // Remove the globals from the valuemap, so that the referrer count will go
290 // down to zero.
291 while (!N->getGlobals().empty()) {
292 GlobalValue *GV = N->getGlobals().back();
293 N->getGlobals().pop_back();
294 ValueMap.erase(GV);
295 }
296 assert(N->getReferrers().empty() && "Referrers should all be gone now!");
297 return true;
298 }
299
300 return false;
301}
302
303
Chris Lattnere2219762002-07-18 18:22:40 +0000304// removeTriviallyDeadNodes - After the graph has been constructed, this method
305// removes all unreachable nodes that are created because they got merged with
306// other nodes in the graph. These nodes will all be trivially unreachable, so
307// we don't have to perform any non-trivial analysis here.
Chris Lattner0d9bab82002-07-18 00:12:30 +0000308//
Chris Lattnere2219762002-07-18 18:22:40 +0000309void DSGraph::removeTriviallyDeadNodes() {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000310 for (unsigned i = 0; i != Nodes.size(); ++i)
311 if (isNodeDead(Nodes[i])) { // This node is dead!
312 delete Nodes[i]; // Free memory...
313 Nodes.erase(Nodes.begin()+i--); // Remove from node list...
314 }
315
Chris Lattnere2219762002-07-18 18:22:40 +0000316 // Remove trivially identical function calls
Chris Lattner0d9bab82002-07-18 00:12:30 +0000317 unsigned NumFns = FunctionCalls.size();
318 std::sort(FunctionCalls.begin(), FunctionCalls.end());
319 FunctionCalls.erase(std::unique(FunctionCalls.begin(), FunctionCalls.end()),
320 FunctionCalls.end());
321
322 DEBUG(if (NumFns != FunctionCalls.size())
323 std::cerr << "Merged " << (NumFns-FunctionCalls.size())
324 << " call nodes in " << Func.getName() << "\n";);
325}
326
327
Chris Lattnere2219762002-07-18 18:22:40 +0000328// markAlive - Simple graph traverser that recursively walks the graph marking
329// stuff to be alive.
330//
331static void markAlive(DSNode *N, std::set<DSNode*> &Alive) {
332 if (N == 0 || Alive.count(N)) return;
333
334 Alive.insert(N);
335 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i)
336 markAlive(N->getLink(i), Alive);
337}
338
339
340// removeDeadNodes - Use a more powerful reachability analysis to eliminate
341// subgraphs that are unreachable. This often occurs because the data
342// structure doesn't "escape" into it's caller, and thus should be eliminated
343// from the caller's graph entirely. This is only appropriate to use when
344// inlining graphs.
345//
346void DSGraph::removeDeadNodes() {
347 // Reduce the amount of work we have to do...
348 removeTriviallyDeadNodes();
349
350 // FIXME: Merge nontrivially identical call nodes...
351
352 // Alive - a set that holds all nodes found to be reachable/alive.
353 std::set<DSNode*> Alive;
354
355 // Mark all nodes reachable by call nodes as alive...
356 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i)
357 for (unsigned j = 0, e = FunctionCalls[i].size(); j != e; ++j)
358 markAlive(FunctionCalls[i][j], Alive);
359
360 for (unsigned i = 0, e = OrigFunctionCalls.size(); i != e; ++i)
361 for (unsigned j = 0, e = OrigFunctionCalls[i].size(); j != e; ++j)
362 markAlive(OrigFunctionCalls[i][j], Alive);
363
364 // Mark all nodes reachable by scalar, global, or incomplete nodes as
365 // reachable...
366 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
367 if (Nodes[i]->NodeType & (DSNode::ScalarNode | DSNode::GlobalNode))
368 markAlive(Nodes[i], Alive);
369
370 // Loop over all unreachable nodes, dropping their references...
371 std::vector<DSNode*> DeadNodes;
372 DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed.
373 for (unsigned i = 0; i != Nodes.size(); ++i)
374 if (!Alive.count(Nodes[i])) {
375 DSNode *N = Nodes[i];
376 Nodes.erase(Nodes.begin()+i--); // Erase node from alive list.
377 DeadNodes.push_back(N); // Add node to our list of dead nodes
378 N->dropAllReferences(); // Drop all outgoing edges
379 }
380
381 // The return value is alive as well...
382 markAlive(RetNode, Alive);
383
384 // Delete all dead nodes...
385 std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>);
386}
387
388
389
Chris Lattner0d9bab82002-07-18 00:12:30 +0000390// maskNodeTypes - Apply a mask to all of the node types in the graph. This
391// is useful for clearing out markers like Scalar or Incomplete.
392//
393void DSGraph::maskNodeTypes(unsigned char Mask) {
394 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
395 Nodes[i]->NodeType &= Mask;
396}
397
398
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000399//===----------------------------------------------------------------------===//
400// LocalDataStructures Implementation
401//===----------------------------------------------------------------------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000402
403// releaseMemory - If the pass pipeline is done with this pass, we can release
404// our memory... here...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000405//
406void LocalDataStructures::releaseMemory() {
407 for (std::map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
408 E = DSInfo.end(); I != E; ++I)
409 delete I->second;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000410
411 // Empty map so next time memory is released, data structures are not
412 // re-deleted.
413 DSInfo.clear();
414}
415
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000416bool LocalDataStructures::run(Module &M) {
417 // Calculate all of the graphs...
418 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner0d9bab82002-07-18 00:12:30 +0000419 if (!I->isExternal())
420 DSInfo.insert(std::make_pair(&*I, new DSGraph(*I)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000421
422 return false;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000423}