From 8be8012ba493f1b17967c4ad7863c5709bf34a75 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 10 Oct 2004 17:07:12 +0000 Subject: [PATCH] Fix 2004-10-10-CastStoreOnce.llx, by adjusting types back if we strip off a cast git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16878 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/GlobalOpt.cpp | 39 +++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 3a58afdc260..17328956d92 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -447,27 +447,30 @@ static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal) { // initializer. Instead, replace all of the loads with the stored value. if (isa(GV->getInitializer()->getType()) && GV->getInitializer()->isNullValue()) { - if (isa(StoredOnceVal) && - AllUsesOfLoadedValueWillTrapIfNull(GV)) { - DEBUG(std::cerr << "REPLACING STORED GLOBAL POINTER: " << *GV); + if (Constant *SOVC = dyn_cast(StoredOnceVal)) + if (AllUsesOfLoadedValueWillTrapIfNull(GV)) { + DEBUG(std::cerr << "REPLACING STORED GLOBAL POINTER: " << *GV); - //std::cerr << " Stored Value: " << *StoredOnceVal << "\n"; + if (GV->getInitializer()->getType() != SOVC->getType()) + SOVC = ConstantExpr::getCast(SOVC, GV->getInitializer()->getType()); - // Replace all uses of loads with uses of uses of the stored value. - while (!GV->use_empty()) - if (LoadInst *LI = dyn_cast(GV->use_back())) { - LI->replaceAllUsesWith(StoredOnceVal); - LI->getParent()->getInstList().erase(LI); // Nuke the load. - } else if (StoreInst *SI = dyn_cast(GV->use_back())) { - SI->getParent()->getInstList().erase(SI); // Nuke the store - } else { - assert(0 && "Unknown user of stored once global!"); - } + //std::cerr << " Stored Value: " << *SOVC << "\n"; - // Nuke the now-dead global. - GV->getParent()->getGlobalList().erase(GV); - return true; - } + // Replace all uses of loads with uses of uses of the stored value. + while (!GV->use_empty()) + if (LoadInst *LI = dyn_cast(GV->use_back())) { + LI->replaceAllUsesWith(SOVC); + LI->getParent()->getInstList().erase(LI); // Nuke the load. + } else if (StoreInst *SI = dyn_cast(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; + } //if (isa(StoredOnceValue)) } return false;