blob: a0614e2bf3130ebcff840498eb3b7b14fe03c4b1 [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"
Chris Lattnerbd422e62001-11-26 18:42:17 +00009#include "llvm/Module.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000010#include "llvm/Function.h"
Chris Lattnerb4de02d2002-04-29 18:13:11 +000011#include "llvm/GlobalVariable.h"
12#include "llvm/Analysis/CallGraph.h"
Chris Lattner5de22042001-11-27 00:03:19 +000013#include "Support/DepthFirstIterator.h"
Chris Lattnerbd422e62001-11-26 18:42:17 +000014
Chris Lattner62b7fd12002-04-07 20:49:59 +000015static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
16 // Calculate which functions are reachable from the external functions in the
17 // call graph.
Chris Lattnerbd422e62001-11-26 18:42:17 +000018 //
Chris Lattner80327322002-03-06 17:16:43 +000019 std::set<CallGraphNode*> ReachableNodes(df_begin(&CallGraph),
20 df_end(&CallGraph));
Chris Lattnerbd422e62001-11-26 18:42:17 +000021
Chris Lattner62b7fd12002-04-07 20:49:59 +000022 // Loop over the functions in the module twice. The first time is used to
23 // drop references that functions have to each other before they are deleted.
24 // The second pass removes the functions that need to be removed.
Chris Lattnerbd422e62001-11-26 18:42:17 +000025 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000026 std::vector<CallGraphNode*> FunctionsToDelete; // Track unused functions
Chris Lattnerbd422e62001-11-26 18:42:17 +000027 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattner80327322002-03-06 17:16:43 +000028 CallGraphNode *N = CallGraph[*I];
Chris Lattnerbd422e62001-11-26 18:42:17 +000029 if (!ReachableNodes.count(N)) { // Not reachable??
30 (*I)->dropAllReferences();
31 N->removeAllCalledMethods();
Chris Lattner62b7fd12002-04-07 20:49:59 +000032 FunctionsToDelete.push_back(N);
Chris Lattnerbd422e62001-11-26 18:42:17 +000033 }
34 }
35
Chris Lattner62b7fd12002-04-07 20:49:59 +000036 // Nothing to do if no unreachable functions have been found...
37 if (FunctionsToDelete.empty()) return false;
Chris Lattnerbd422e62001-11-26 18:42:17 +000038
Chris Lattner62b7fd12002-04-07 20:49:59 +000039 // Unreachables functions have been found and should have no references to
40 // them, delete them now.
Chris Lattnerbd422e62001-11-26 18:42:17 +000041 //
Chris Lattner62b7fd12002-04-07 20:49:59 +000042 for (std::vector<CallGraphNode*>::iterator I = FunctionsToDelete.begin(),
43 E = FunctionsToDelete.end(); I != E; ++I)
Chris Lattnerbd422e62001-11-26 18:42:17 +000044 delete CallGraph.removeMethodFromModule(*I);
45
Chris Lattnerbd422e62001-11-26 18:42:17 +000046 return true;
47}
48
Chris Lattnerb4de02d2002-04-29 18:13:11 +000049static bool RemoveUnreachableGlobalVariables(Module *M) {
50 bool Changed = false;
51 // Eliminate all global variables that are unused, and that are internal, or
52 // do not have an initializer.
53 //
54 for (Module::giterator I = M->gbegin(); I != M->gend(); )
55 if (!(*I)->use_empty() ||
56 ((*I)->hasExternalLinkage() && (*I)->hasInitializer()))
57 ++I; // Cannot eliminate global variable
58 else {
59 delete M->getGlobalList().remove(I);
60 Changed = true;
61 }
62 return Changed;
63}
64
Chris Lattner04805fa2002-02-26 21:46:54 +000065namespace {
66 struct GlobalDCE : public Pass {
Chris Lattner37104aa2002-04-29 14:57:45 +000067 const char *getPassName() const { return "Dead Global Elimination"; }
68
Chris Lattner04805fa2002-02-26 21:46:54 +000069 // run - Do the GlobalDCE pass on the specified module, optionally updating
70 // the specified callgraph to reflect the changes.
71 //
72 bool run(Module *M) {
Chris Lattnerb4de02d2002-04-29 18:13:11 +000073 return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>()) |
74 RemoveUnreachableGlobalVariables(M);
Chris Lattner04805fa2002-02-26 21:46:54 +000075 }
76
Chris Lattnerc8e66542002-04-27 06:56:12 +000077 // getAnalysisUsage - This function works on the call graph of a module.
Chris Lattner04805fa2002-02-26 21:46:54 +000078 // It is capable of updating the call graph to reflect the new state of the
79 // module.
80 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000081 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.addRequired(CallGraph::ID);
Chris Lattner04805fa2002-02-26 21:46:54 +000083 }
84 };
Chris Lattnerd5d56782002-01-31 00:45:11 +000085}
86
Chris Lattner04805fa2002-02-26 21:46:54 +000087Pass *createGlobalDCEPass() { return new GlobalDCE(); }