2002-04-07 22:49:59 +02:00
|
|
|
//===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===//
|
2001-11-26 19:42:17 +01:00
|
|
|
//
|
|
|
|
// This transform is designed to eliminate unreachable internal globals
|
2002-04-27 08:56:12 +02:00
|
|
|
// FIXME: GlobalDCE should update the callgraph, not destroy it!
|
2001-11-26 19:42:17 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO/GlobalDCE.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2002-02-26 22:46:54 +01:00
|
|
|
#include "llvm/Pass.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/DepthFirstIterator.h"
|
2001-11-26 19:42:17 +01:00
|
|
|
#include <set>
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
|
|
|
|
// Calculate which functions are reachable from the external functions in the
|
|
|
|
// call graph.
|
2001-11-26 19:42:17 +01:00
|
|
|
//
|
2002-03-06 18:16:43 +01:00
|
|
|
std::set<CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
|
|
|
|
df_end(&CallGraph));
|
2001-11-26 19:42:17 +01:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// Loop over the functions in the module twice. The first time is used to
|
|
|
|
// drop references that functions have to each other before they are deleted.
|
|
|
|
// The second pass removes the functions that need to be removed.
|
2001-11-26 19:42:17 +01:00
|
|
|
//
|
2002-04-07 22:49:59 +02:00
|
|
|
std::vector<CallGraphNode*> FunctionsToDelete; // Track unused functions
|
2001-11-26 19:42:17 +01:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
|
2002-03-06 18:16:43 +01:00
|
|
|
CallGraphNode *N = CallGraph[*I];
|
2001-11-26 19:42:17 +01:00
|
|
|
if (!ReachableNodes.count(N)) { // Not reachable??
|
|
|
|
(*I)->dropAllReferences();
|
|
|
|
N->removeAllCalledMethods();
|
2002-04-07 22:49:59 +02:00
|
|
|
FunctionsToDelete.push_back(N);
|
2001-11-26 19:42:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// Nothing to do if no unreachable functions have been found...
|
|
|
|
if (FunctionsToDelete.empty()) return false;
|
2001-11-26 19:42:17 +01:00
|
|
|
|
2002-04-07 22:49:59 +02:00
|
|
|
// Unreachables functions have been found and should have no references to
|
|
|
|
// them, delete them now.
|
2001-11-26 19:42:17 +01:00
|
|
|
//
|
2002-04-07 22:49:59 +02:00
|
|
|
for (std::vector<CallGraphNode*>::iterator I = FunctionsToDelete.begin(),
|
|
|
|
E = FunctionsToDelete.end(); I != E; ++I)
|
2001-11-26 19:42:17 +01:00
|
|
|
delete CallGraph.removeMethodFromModule(*I);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-02-26 22:46:54 +01:00
|
|
|
namespace {
|
|
|
|
struct GlobalDCE : public Pass {
|
|
|
|
// run - Do the GlobalDCE pass on the specified module, optionally updating
|
|
|
|
// the specified callgraph to reflect the changes.
|
|
|
|
//
|
|
|
|
bool run(Module *M) {
|
2002-04-07 22:49:59 +02:00
|
|
|
return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>());
|
2002-02-26 22:46:54 +01:00
|
|
|
}
|
2002-01-31 01:45:11 +01:00
|
|
|
|
2002-04-27 08:56:12 +02:00
|
|
|
// getAnalysisUsage - This function works on the call graph of a module.
|
2002-02-26 22:46:54 +01:00
|
|
|
// It is capable of updating the call graph to reflect the new state of the
|
|
|
|
// module.
|
|
|
|
//
|
2002-04-27 08:56:12 +02:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired(CallGraph::ID);
|
2002-02-26 22:46:54 +01:00
|
|
|
}
|
|
|
|
};
|
2001-11-26 19:42:17 +01:00
|
|
|
}
|
2002-02-26 22:46:54 +01:00
|
|
|
|
|
|
|
Pass *createGlobalDCEPass() { return new GlobalDCE(); }
|