f573a0be1a639e05d30caf1d6aee9b81b34d47c5
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructure.cpp
1 //===- DataStructure.cpp - Implement the core data structure analysis -----===//
2 //
3 // This file implements the core data structure functionality.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Module.h"
8 #include "llvm/DerivedTypes.h"
9 #include "Support/STLExtras.h"
10 #include "Support/StatisticReporter.h"
11 #include "Support/STLExtras.h"
12 #include <algorithm>
13 #include <set>
14 #include "llvm/Analysis/DataStructure.h"
15
16 using std::vector;
17
18 static RegisterAnalysis<LocalDataStructures>
19 X("datastructure", "Local Data Structure Analysis");
20 AnalysisID LocalDataStructures::ID(AnalysisID::create<LocalDataStructures>());
21
22 //===----------------------------------------------------------------------===//
23 // DSNode Implementation
24 //===----------------------------------------------------------------------===//
25
26 DSNode::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
38 // DSNode copy constructor... do not copy over the referrers list!
39 DSNode::DSNode(const DSNode &N)
40   : Ty(N.Ty), Links(N.Links), Globals(N.Globals), NodeType(N.NodeType) {
41 }
42
43 void DSNode::removeReferrer(DSNodeHandle *H) {
44   // Search backwards, because we depopulate the list from the back for
45   // efficiency (because it's a vector).
46   vector<DSNodeHandle*>::reverse_iterator I =
47     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
52 // 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 //
55 void DSNode::addGlobal(GlobalValue *GV) {
56   // Keep the list sorted.
57   vector<GlobalValue*>::iterator I =
58     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   }
65 }
66
67
68 // addEdgeTo - Add an edge from the current node to the specified node.  This
69 // can cause merging of nodes in the graph.
70 //
71 void 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 //
88 void 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
105   // Merge the node types
106   NodeType |= N->NodeType;
107   N->NodeType = 0;   // N is now a dead node.
108
109   // Merge the globals list...
110   if (!N->Globals.empty()) {
111     // Save the current globals off to the side...
112     vector<GlobalValue*> OldGlobals(Globals);
113
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   }
127 }
128
129 //===----------------------------------------------------------------------===//
130 // DSGraph Implementation
131 //===----------------------------------------------------------------------===//
132
133 DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) {
134   std::map<const DSNode*, DSNode*> NodeMap; // ignored
135   RetNode = cloneInto(G, ValueMap, NodeMap, false);
136 }
137
138 DSGraph::~DSGraph() {
139   FunctionCalls.clear();
140   OrigFunctionCalls.clear();
141   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
154 // dump - Allow inspection of graph in a debugger.
155 void DSGraph::dump() const { print(std::cerr); }
156
157
158 // 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 //
164 DSNode *DSGraph::cloneInto(const DSGraph &G, 
165                            std::map<Value*, DSNodeHandle> &OldValMap,
166                            std::map<const DSNode*, DSNode*> &OldNodeMap,
167                            bool StripLocals) {
168
169   assert(OldNodeMap.size()==0 && "Return argument OldNodeMap should be empty");
170
171   OldNodeMap[0] = 0;  // Null pointer maps to null
172
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);
180     OldNodeMap[Old] = New;
181   }
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)
186       Nodes[i]->setLink(j, OldNodeMap[Nodes[i]->getLink(j)]);
187
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)
197     OldValMap[I->first] = OldNodeMap[I->second];
198
199   // 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   }
208
209   // Copy the list of unresolved callers
210   PendingCallers.insert(PendingCallers.end(),
211                         G.PendingCallers.begin(), G.PendingCallers.end());
212
213   // Return the returned node pointer...
214   return OldNodeMap[G.RetNode];
215 }
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 //
224 static 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 //
247 void 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) {
255     vector<DSNodeHandle> &Args = FunctionCalls[i];
256     // Then the return value is certainly incomplete!
257     markIncompleteNode(Args[0]);
258
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
266   // Mark all of the nodes pointed to by global or cast nodes as incomplete...
267   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
268     if (Nodes[i]->NodeType & (DSNode::GlobalNode | DSNode::CastNode)) {
269       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 //
279 bool 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
304 // 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.
308 //
309 void DSGraph::removeTriviallyDeadNodes() {
310   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
316   // Remove trivially identical function calls
317   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
328 // markAlive - Simple graph traverser that recursively walks the graph marking
329 // stuff to be alive.
330 //
331 static 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 //
346 void 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
390 // 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 //
393 void DSGraph::maskNodeTypes(unsigned char Mask) {
394   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
395     Nodes[i]->NodeType &= Mask;
396 }
397
398
399 //===----------------------------------------------------------------------===//
400 // LocalDataStructures Implementation
401 //===----------------------------------------------------------------------===//
402
403 // releaseMemory - If the pass pipeline is done with this pass, we can release
404 // our memory... here...
405 //
406 void LocalDataStructures::releaseMemory() {
407   for (std::map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
408          E = DSInfo.end(); I != E; ++I)
409     delete I->second;
410
411   // Empty map so next time memory is released, data structures are not
412   // re-deleted.
413   DSInfo.clear();
414 }
415
416 bool LocalDataStructures::run(Module &M) {
417   // Calculate all of the graphs...
418   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
419     if (!I->isExternal())
420       DSInfo.insert(std::make_pair(&*I, new DSGraph(*I)));
421
422   return false;
423 }