- Cleaned up the interface to AnalysisUsage to take analysis class names
[oota-llvm.git] / lib / Transforms / IPO / GlobalDCE.cpp
1 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
2 //
3 // This transform is designed to eliminate unreachable internal globals
4 // FIXME: GlobalDCE should update the callgraph, not destroy it!
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Transforms/IPO.h"
9 #include "llvm/Module.h"
10 #include "llvm/Function.h"
11 #include "llvm/GlobalVariable.h"
12 #include "llvm/Analysis/CallGraph.h"
13 #include "Support/DepthFirstIterator.h"
14 #include "Support/StatisticReporter.h"
15
16 static Statistic<> NumFunctions("globaldce\t- Number of functions removed");
17 static Statistic<> NumVariables("globaldce\t- Number of global variables removed");
18
19 static bool RemoveUnreachableFunctions(Module &M, CallGraph &CallGraph) {
20   // Calculate which functions are reachable from the external functions in the
21   // call graph.
22   //
23   std::set<CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
24                                           df_end(&CallGraph));
25
26   // Loop over the functions in the module twice.  The first time is used to
27   // drop references that functions have to each other before they are deleted.
28   // The second pass removes the functions that need to be removed.
29   //
30   std::vector<CallGraphNode*> FunctionsToDelete;   // Track unused functions
31   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
32     CallGraphNode *N = CallGraph[I];
33
34     if (!ReachableNodes.count(N)) {              // Not reachable??
35       I->dropAllReferences();
36       N->removeAllCalledFunctions();
37       FunctionsToDelete.push_back(N);
38       ++NumFunctions;
39     }
40   }
41
42   // Nothing to do if no unreachable functions have been found...
43   if (FunctionsToDelete.empty()) return false;
44
45   // Unreachables functions have been found and should have no references to
46   // them, delete them now.
47   //
48   for (std::vector<CallGraphNode*>::iterator I = FunctionsToDelete.begin(),
49          E = FunctionsToDelete.end(); I != E; ++I)
50     delete CallGraph.removeFunctionFromModule(*I);
51
52   return true;
53 }
54
55 static bool RemoveUnreachableGlobalVariables(Module &M) {
56   bool Changed = false;
57   // Eliminate all global variables that are unused, and that are internal, or
58   // do not have an initializer.
59   //
60   for (Module::giterator I = M.gbegin(); I != M.gend(); )
61     if (!I->use_empty() || (I->hasExternalLinkage() && I->hasInitializer()))
62       ++I;                     // Cannot eliminate global variable
63     else {
64       I = M.getGlobalList().erase(I);
65       ++NumVariables;
66       Changed = true;
67     }
68   return Changed;
69 }
70
71 namespace {
72   struct GlobalDCE : public Pass {
73     // run - Do the GlobalDCE pass on the specified module, optionally updating
74     // the specified callgraph to reflect the changes.
75     //
76     bool run(Module &M) {
77       return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>()) |
78              RemoveUnreachableGlobalVariables(M);
79     }
80
81     // getAnalysisUsage - This function works on the call graph of a module.
82     // It is capable of updating the call graph to reflect the new state of the
83     // module.
84     //
85     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86       AU.addRequired<CallGraph>();
87     }
88   };
89   RegisterOpt<GlobalDCE> X("globaldce", "Dead Global Elimination");
90 }
91
92 Pass *createGlobalDCEPass() { return new GlobalDCE(); }