blob: 1b64a0b369bf8b8bc649816cd924c3ddb0cab3a8 [file] [log] [blame]
Chris Lattnerbd422e62001-11-26 18:42:17 +00001//===-- GlobalDCE.cpp - DCE unreachable internal methods ---------*- C++ -*--=//
2//
3// This transform is designed to eliminate unreachable internal globals
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Transforms/IPO/GlobalDCE.h"
8#include "llvm/Analysis/CallGraph.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +00009#include "llvm/Module.h"
10#include "llvm/Method.h"
Chris Lattner5de22042001-11-27 00:03:19 +000011#include "Support/DepthFirstIterator.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +000012#include <set>
13
Chris Lattner0686e432002-01-21 07:31:50 +000014static bool RemoveUnreachableMethods(Module *M, cfg::CallGraph &CallGraph) {
Chris Lattnerbd422e62001-11-26 18:42:17 +000015 // Calculate which methods are reachable from the external methods in the call
16 // graph.
17 //
Chris Lattner7f74a562002-01-20 22:54:45 +000018 std::set<cfg::CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
19 df_end(&CallGraph));
Chris Lattnerbd422e62001-11-26 18:42:17 +000020
21 // Loop over the methods in the module twice. The first time is used to drop
22 // references that methods have to each other before they are deleted. The
23 // second pass removes the methods that need to be removed.
24 //
Chris Lattner7f74a562002-01-20 22:54:45 +000025 std::vector<cfg::CallGraphNode*> MethodsToDelete; // Track unused methods
Chris Lattnerbd422e62001-11-26 18:42:17 +000026 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
27 cfg::CallGraphNode *N = CallGraph[*I];
28 if (!ReachableNodes.count(N)) { // Not reachable??
29 (*I)->dropAllReferences();
30 N->removeAllCalledMethods();
31 MethodsToDelete.push_back(N);
32 }
33 }
34
35 // Nothing to do if no unreachable methods have been found...
Chris Lattner0686e432002-01-21 07:31:50 +000036 if (MethodsToDelete.empty()) return false;
Chris Lattnerbd422e62001-11-26 18:42:17 +000037
38 // Unreachables methods have been found and should have no references to them,
39 // delete them now.
40 //
Chris Lattner7f74a562002-01-20 22:54:45 +000041 for (std::vector<cfg::CallGraphNode*>::iterator I = MethodsToDelete.begin(),
Chris Lattnerbd422e62001-11-26 18:42:17 +000042 E = MethodsToDelete.end(); I != E; ++I)
43 delete CallGraph.removeMethodFromModule(*I);
44
Chris Lattnerbd422e62001-11-26 18:42:17 +000045 return true;
46}
47
Chris Lattner0686e432002-01-21 07:31:50 +000048bool GlobalDCE::run(Module *M) {
Chris Lattnerd5d56782002-01-31 00:45:11 +000049 return RemoveUnreachableMethods(M, getAnalysis<cfg::CallGraph>());
50}
51
52// getAnalysisUsageInfo - This function works on the call graph of a module.
53// It is capable of updating the call graph to reflect the new state of the
54// module.
55//
56void GlobalDCE::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
57 Pass::AnalysisSet &Destroyed,
58 Pass::AnalysisSet &Provided) {
59 Required.push_back(cfg::CallGraph::ID);
60 // FIXME: This should update the callgraph, not destroy it!
61 Destroyed.push_back(cfg::CallGraph::ID);
Chris Lattnerbd422e62001-11-26 18:42:17 +000062}