summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2002-04-29 18:13:11 +0000
committerChris Lattner <sabre@nondot.org>2002-04-29 18:13:11 +0000
commitb4de02df67d4a9295dee98a775b40e7df9199d5a (patch)
tree53ad2035c1f2e5202073588396d4d4f0012efdfb /llvm/lib/Transforms
parent9550e211526c3dbaf3de1c82faa9b0ac662e8001 (diff)
downloadbcm5719-llvm-b4de02df67d4a9295dee98a775b40e7df9199d5a.tar.gz
bcm5719-llvm-b4de02df67d4a9295dee98a775b40e7df9199d5a.zip
Eliminate dead global variables
llvm-svn: 2400
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/IPO/GlobalDCE.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/IPO/GlobalDCE.cpp b/llvm/lib/Transforms/IPO/GlobalDCE.cpp
index 2f938079a84..a0614e2bf31 100644
--- a/llvm/lib/Transforms/IPO/GlobalDCE.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalDCE.cpp
@@ -6,9 +6,10 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/IPO/GlobalDCE.h"
-#include "llvm/Analysis/CallGraph.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
+#include "llvm/GlobalVariable.h"
+#include "llvm/Analysis/CallGraph.h"
#include "Support/DepthFirstIterator.h"
static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
@@ -45,6 +46,22 @@ static bool RemoveUnreachableFunctions(Module *M, CallGraph &CallGraph) {
return true;
}
+static bool RemoveUnreachableGlobalVariables(Module *M) {
+ bool Changed = false;
+ // Eliminate all global variables that are unused, and that are internal, or
+ // do not have an initializer.
+ //
+ for (Module::giterator I = M->gbegin(); I != M->gend(); )
+ if (!(*I)->use_empty() ||
+ ((*I)->hasExternalLinkage() && (*I)->hasInitializer()))
+ ++I; // Cannot eliminate global variable
+ else {
+ delete M->getGlobalList().remove(I);
+ Changed = true;
+ }
+ return Changed;
+}
+
namespace {
struct GlobalDCE : public Pass {
const char *getPassName() const { return "Dead Global Elimination"; }
@@ -53,7 +70,8 @@ namespace {
// the specified callgraph to reflect the changes.
//
bool run(Module *M) {
- return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>());
+ return RemoveUnreachableFunctions(M, getAnalysis<CallGraph>()) |
+ RemoveUnreachableGlobalVariables(M);
}
// getAnalysisUsage - This function works on the call graph of a module.
OpenPOWER on IntegriCloud