Changes For Bug 352
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureOpt.cpp
index ad5b8947edc788d57b98567fb186ea612ec3877a..f7a1ed9af3f8c660e37902c15db464d9a962f611 100644 (file)
@@ -1,19 +1,29 @@
 //===- DataStructureOpt.cpp - Data Structure Analysis Based Optimizations -===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This pass uses DSA to a series of simple optimizations, like marking
 // unwritten global variables 'constant'.
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/DataStructure.h"
-#include "llvm/Analysis/DSGraph.h"
+#include "llvm/Analysis/DataStructure/DataStructure.h"
+#include "llvm/Analysis/DataStructure/DSGraph.h"
 #include "llvm/Module.h"
 #include "llvm/Constant.h"
-#include "Support/Statistic.h"
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
 
 namespace {
   Statistic<>
   NumGlobalsConstanted("ds-opt", "Number of globals marked constant");
+  Statistic<>
+  NumGlobalsIsolated("ds-opt", "Number of globals with references dropped");
 
   class DSOpt : public Pass {
     TDDataStructures *TD;
@@ -38,7 +48,6 @@ namespace {
   RegisterOpt<DSOpt> X("ds-opt", "DSA-based simple optimizations");
 }
 
-
 /// OptimizeGlobals - This method uses information taken from DSA to optimize
 /// global variables.
 ///
@@ -60,8 +69,21 @@ bool DSOpt::OptimizeGlobals(Module &M) {
         // can delete it.  We don't ACTUALLY want to delete the global, just
         // remove anything that references the global: later passes will take
         // care of nuking it.
-        I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
+        if (!I->use_empty()) {
+          I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
+          ++NumGlobalsIsolated;
+        }
       } else if (GNode && GNode->isComplete()) {
+
+        // If the node has not been read or written, and it is not externally
+        // visible, kill any references to it so it can be DCE'd.
+        if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){
+          if (!I->use_empty()) {
+            I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
+            ++NumGlobalsIsolated;
+          }
+        }
+
         // We expect that there will almost always be a node for this global.
         // If there is, and the node doesn't have the M bit set, we can set the
         // 'constant' bit on the global.