blob: 2f938079a8481fd7d90235edb1be43552704fb64 [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 Lattner5de22042001-11-27 00:03:19 +000012#include "Support/DepthFirstIterator.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +000013
Chris Lattner62b7fd12002-04-07 20:49:59 +000014static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
15 // Calculate which functions are reachable from the external functions in the
16 // call graph.
Chris Lattnerbd422e62001-11-26 18:42:17 +000017 //
Chris Lattner80327322002-03-06 17:16:43 +000018 std::set<CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
19 df_end(&CallGraph));
Chris Lattnerbd422e62001-11-26 18:42:17 +000020
Chris Lattner62b7fd12002-04-07 20:49:59 +000021 // Loop over the functions in the module twice. The first time is used to
22 // drop references that functions have to each other before they are deleted.
23 // The second pass removes the functions that need to be removed.
Chris Lattnerbd422e62001-11-26 18:42:17 +000024 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000025 std::vector<CallGraphNode*> FunctionsToDelete; // Track unused functions
Chris Lattnerbd422e62001-11-26 18:42:17 +000026 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattner80327322002-03-06 17:16:43 +000027 CallGraphNode *N = CallGraph[*I];
Chris Lattnerbd422e62001-11-26 18:42:17 +000028 if (!ReachableNodes.count(N)) { // Not reachable??
29 (*I)->dropAllReferences();
30 N->removeAllCalledMethods();
Chris Lattner62b7fd12002-04-07 20:49:59 +000031 FunctionsToDelete.push_back(N);
Chris Lattnerbd422e62001-11-26 18:42:17 +000032 }
33 }
34
Chris Lattner62b7fd12002-04-07 20:49:59 +000035 // Nothing to do if no unreachable functions have been found...
36 if (FunctionsToDelete.empty()) return false;
Chris Lattnerbd422e62001-11-26 18:42:17 +000037
Chris Lattner62b7fd12002-04-07 20:49:59 +000038 // Unreachables functions have been found and should have no references to
39 // them, delete them now.
Chris Lattnerbd422e62001-11-26 18:42:17 +000040 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000041 for (std::vector<CallGraphNode*>::iterator I = FunctionsToDelete.begin(),
42 E = FunctionsToDelete.end(); I != E; ++I)
Chris Lattnerbd422e62001-11-26 18:42:17 +000043 delete CallGraph.removeMethodFromModule(*I);
44
Chris Lattnerbd422e62001-11-26 18:42:17 +000045 return true;
46}
47
Chris Lattner04805fa2002-02-26 21:46:54 +000048namespace {
49 struct GlobalDCE : public Pass {
Chris Lattner37104aa2002-04-29 14:57:45 +000050 const char *getPassName() const { return "Dead Global Elimination"; }
51
Chris Lattner04805fa2002-02-26 21:46:54 +000052 // run - Do the GlobalDCE pass on the specified module, optionally updating
53 // the specified callgraph to reflect the changes.
54 //
55 bool run(Module *M) {
Chris Lattner62b7fd12002-04-07 20:49:59 +000056 return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>());
Chris Lattner04805fa2002-02-26 21:46:54 +000057 }
58
Chris Lattnerc8e66542002-04-27 06:56:12 +000059 // getAnalysisUsage - This function works on the call graph of a module.
Chris Lattner04805fa2002-02-26 21:46:54 +000060 // It is capable of updating the call graph to reflect the new state of the
61 // module.
62 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.addRequired(CallGraph::ID);
Chris Lattner04805fa2002-02-26 21:46:54 +000065 }
66 };
Chris Lattnerd5d56782002-01-31 00:45:11 +000067}
68
Chris Lattner04805fa2002-02-26 21:46:54 +000069Pass *createGlobalDCEPass() { return new GlobalDCE(); }