[llvm-commits] CVS: llvm/lib/Transforms/IPO/GlobalOpt.cpp

Chris Lattner lattner at cs.uiuc.edu
Sun Oct 10 10:07:23 PDT 2004



Changes in directory llvm/lib/Transforms/IPO:

GlobalOpt.cpp updated: 1.20 -> 1.21
---
Log message:

Fix 2004-10-10-CastStoreOnce.llx, by adjusting types back if we strip off a cast


---
Diffs of the changes:  (+23 -20)

Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
diff -u llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.20 llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.21
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.20	Sun Oct 10 11:47:33 2004
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp	Sun Oct 10 12:07:12 2004
@@ -447,27 +447,30 @@
   // initializer.  Instead, replace all of the loads with the stored value.
   if (isa<PointerType>(GV->getInitializer()->getType()) &&
       GV->getInitializer()->isNullValue()) {
-    if (isa<Constant>(StoredOnceVal) &&
-        AllUsesOfLoadedValueWillTrapIfNull(GV)) {
-      DEBUG(std::cerr << "REPLACING STORED GLOBAL POINTER: " << *GV);
-
-      //std::cerr << " Stored Value: " << *StoredOnceVal << "\n";
-
-      // Replace all uses of loads with uses of uses of the stored value.
-      while (!GV->use_empty())
-        if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) {
-          LI->replaceAllUsesWith(StoredOnceVal);
-          LI->getParent()->getInstList().erase(LI); // Nuke the load.
-        } else if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) {
-          SI->getParent()->getInstList().erase(SI); // Nuke the store
-        } else {
-          assert(0 && "Unknown user of stored once global!");
-        }
+    if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal))
+      if (AllUsesOfLoadedValueWillTrapIfNull(GV)) {
+        DEBUG(std::cerr << "REPLACING STORED GLOBAL POINTER: " << *GV);
+
+        if (GV->getInitializer()->getType() != SOVC->getType())
+          SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType());
+
+        //std::cerr << " Stored Value: " << *SOVC << "\n";
+
+        // Replace all uses of loads with uses of uses of the stored value.
+        while (!GV->use_empty())
+          if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) {
+            LI->replaceAllUsesWith(SOVC);
+            LI->getParent()->getInstList().erase(LI); // Nuke the load.
+          } else if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) {
+            SI->getParent()->getInstList().erase(SI); // Nuke the store
+          } else {
+            assert(0 && "Unknown user of stored once global!");
+          }
 
-      // Nuke the now-dead global.
-      GV->getParent()->getGlobalList().erase(GV);
-      return true;
-    }
+        // Nuke the now-dead global.
+        GV->getParent()->getGlobalList().erase(GV);
+        return true;
+      }
     //if (isa<MallocInst>(StoredOnceValue))
   }
   return false;






More information about the llvm-commits mailing list