blob: e852a6a29fcf67ea80b3139e37b28476cd36f6ac [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
Chris Lattnerbd422e62001-11-26 18:42:17 +00002//
3// This transform is designed to eliminate unreachable internal globals
Chris Lattnerc8e66542002-04-27 06:56:12 +00004// FIXME: GlobalDCE should update the callgraph, not destroy it!
Chris Lattnerbd422e62001-11-26 18:42:17 +00005//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Transforms/IPO/GlobalDCE.h"
9#include "llvm/Analysis/CallGraph.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +000010#include "llvm/Module.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000011#include "llvm/Function.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000012#include "llvm/Pass.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/DepthFirstIterator.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +000014#include <set>
15
Chris Lattner62b7fd12002-04-07 20:49:59 +000016static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
17 // Calculate which functions are reachable from the external functions in the
18 // call graph.
Chris Lattnerbd422e62001-11-26 18:42:17 +000019 //
Chris Lattner80327322002-03-06 17:16:43 +000020 std::set<CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
21 df_end(&CallGraph));
Chris Lattnerbd422e62001-11-26 18:42:17 +000022
Chris Lattner62b7fd12002-04-07 20:49:59 +000023 // Loop over the functions in the module twice. The first time is used to
24 // drop references that functions have to each other before they are deleted.
25 // The second pass removes the functions that need to be removed.
Chris Lattnerbd422e62001-11-26 18:42:17 +000026 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000027 std::vector<CallGraphNode*> FunctionsToDelete; // Track unused functions
Chris Lattnerbd422e62001-11-26 18:42:17 +000028 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattner80327322002-03-06 17:16:43 +000029 CallGraphNode *N = CallGraph[*I];
Chris Lattnerbd422e62001-11-26 18:42:17 +000030 if (!ReachableNodes.count(N)) { // Not reachable??
31 (*I)->dropAllReferences();
32 N->removeAllCalledMethods();
Chris Lattner62b7fd12002-04-07 20:49:59 +000033 FunctionsToDelete.push_back(N);
Chris Lattnerbd422e62001-11-26 18:42:17 +000034 }
35 }
36
Chris Lattner62b7fd12002-04-07 20:49:59 +000037 // Nothing to do if no unreachable functions have been found...
38 if (FunctionsToDelete.empty()) return false;
Chris Lattnerbd422e62001-11-26 18:42:17 +000039
Chris Lattner62b7fd12002-04-07 20:49:59 +000040 // Unreachables functions have been found and should have no references to
41 // them, delete them now.
Chris Lattnerbd422e62001-11-26 18:42:17 +000042 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000043 for (std::vector<CallGraphNode*>::iterator I = FunctionsToDelete.begin(),
44 E = FunctionsToDelete.end(); I != E; ++I)
Chris Lattnerbd422e62001-11-26 18:42:17 +000045 delete CallGraph.removeMethodFromModule(*I);
46
Chris Lattnerbd422e62001-11-26 18:42:17 +000047 return true;
48}
49
Chris Lattner04805fa2002-02-26 21:46:54 +000050namespace {
51 struct GlobalDCE : public Pass {
Chris Lattner37104aa2002-04-29 14:57:45 +000052 const char *getPassName() const { return "Dead Global Elimination"; }
53
Chris Lattner04805fa2002-02-26 21:46:54 +000054 // run - Do the GlobalDCE pass on the specified module, optionally updating
55 // the specified callgraph to reflect the changes.
56 //
57 bool run(Module *M) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000058 return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>());
Chris Lattner04805fa2002-02-26 21:46:54 +000059 }
60
Chris Lattnerc8e66542002-04-27 06:56:12 +000061 // getAnalysisUsage - This function works on the call graph of a module.
Chris Lattner04805fa2002-02-26 21:46:54 +000062 // It is capable of updating the call graph to reflect the new state of the
63 // module.
64 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000065 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.addRequired(CallGraph::ID);
Chris Lattner04805fa2002-02-26 21:46:54 +000067 }
68 };
Chris Lattnerd5d56782002-01-31 00:45:11 +000069}
70
Chris Lattner04805fa2002-02-26 21:46:54 +000071Pass *createGlobalDCEPass() { return new GlobalDCE(); }