2001-11-27 02:42:17 +08:00
|
|
|
//===-- GlobalDCE.cpp - DCE unreachable internal methods ---------*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This transform is designed to eliminate unreachable internal globals
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/DepthFirstIterator.h"
|
2001-11-27 02:42:17 +08:00
|
|
|
#include <set>
|
|
|
|
|
2002-01-21 15:31:50 +08:00
|
|
|
static bool RemoveUnreachableMethods(Module *M, cfg::CallGraph &CallGraph) {
|
2001-11-27 02:42:17 +08:00
|
|
|
// Calculate which methods are reachable from the external methods in the call
|
|
|
|
// graph.
|
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
std::set<cfg::CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
|
|
|
|
df_end(&CallGraph));
|
2001-11-27 02:42:17 +08:00
|
|
|
|
|
|
|
// Loop over the methods in the module twice. The first time is used to drop
|
|
|
|
// references that methods have to each other before they are deleted. The
|
|
|
|
// second pass removes the methods that need to be removed.
|
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
std::vector<cfg::CallGraphNode*> MethodsToDelete; // Track unused methods
|
2001-11-27 02:42:17 +08:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
|
|
|
cfg::CallGraphNode *N = CallGraph[*I];
|
|
|
|
if (!ReachableNodes.count(N)) { // Not reachable??
|
|
|
|
(*I)->dropAllReferences();
|
|
|
|
N->removeAllCalledMethods();
|
|
|
|
MethodsToDelete.push_back(N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if no unreachable methods have been found...
|
2002-01-21 15:31:50 +08:00
|
|
|
if (MethodsToDelete.empty()) return false;
|
2001-11-27 02:42:17 +08:00
|
|
|
|
|
|
|
// Unreachables methods have been found and should have no references to them,
|
|
|
|
// delete them now.
|
|
|
|
//
|
2002-01-21 06:54:45 +08:00
|
|
|
for (std::vector<cfg::CallGraphNode*>::iterator I = MethodsToDelete.begin(),
|
2001-11-27 02:42:17 +08:00
|
|
|
E = MethodsToDelete.end(); I != E; ++I)
|
|
|
|
delete CallGraph.removeMethodFromModule(*I);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-01-21 15:31:50 +08:00
|
|
|
bool GlobalDCE::run(Module *M) {
|
2002-01-31 08:45:11 +08:00
|
|
|
return RemoveUnreachableMethods(M, getAnalysis<cfg::CallGraph>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAnalysisUsageInfo - This function works on the call graph of a module.
|
|
|
|
// It is capable of updating the call graph to reflect the new state of the
|
|
|
|
// module.
|
|
|
|
//
|
|
|
|
void GlobalDCE::getAnalysisUsageInfo(Pass::AnalysisSet &Required,
|
|
|
|
Pass::AnalysisSet &Destroyed,
|
|
|
|
Pass::AnalysisSet &Provided) {
|
|
|
|
Required.push_back(cfg::CallGraph::ID);
|
|
|
|
// FIXME: This should update the callgraph, not destroy it!
|
|
|
|
Destroyed.push_back(cfg::CallGraph::ID);
|
2001-11-27 02:42:17 +08:00
|
|
|
}
|