Replace TraverseGEPInitializer with ConstantFoldLoadThroughGEPConstantExpr
authorChris Lattner <sabre@nondot.org>
Mon, 26 Sep 2005 05:34:07 +0000 (05:34 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 26 Sep 2005 05:34:07 +0000 (05:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23447 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/IPO/GlobalOpt.cpp

index 09e4928a1f94a185f1eda9e01ee849a39622ec3a..4e081dd20571ec3c9b9ba6148a26a1b7016e44df 100644 (file)
@@ -297,21 +297,6 @@ static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) {
   return 0;
 }
 
-static Constant *TraverseGEPInitializer(User *GEP, Constant *Init) {
-  if (Init == 0) return 0;
-  if (GEP->getNumOperands() == 1 ||
-      !isa<Constant>(GEP->getOperand(1)) ||
-      !cast<Constant>(GEP->getOperand(1))->isNullValue())
-    return 0;
-
-  for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) {
-    ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
-    if (!Idx) return 0;
-    Init = getAggregateConstantElement(Init, Idx);
-    if (Init == 0) return 0;
-  }
-  return Init;
-}
 
 /// CleanupConstantGlobalUsers - We just marked GV constant.  Loop over all
 /// users of the global, cleaning up the obvious ones.  This is largely just a
@@ -335,7 +320,7 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) {
       Changed = true;
     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
       if (CE->getOpcode() == Instruction::GetElementPtr) {
-        Constant *SubInit = TraverseGEPInitializer(CE, Init);
+        Constant *SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
         Changed |= CleanupConstantGlobalUsers(CE, SubInit);
       } else if (CE->getOpcode() == Instruction::Cast &&
                  isa<PointerType>(CE->getType())) {
@@ -348,7 +333,10 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) {
         Changed = true;
       }
     } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
-      Constant *SubInit = TraverseGEPInitializer(GEP, Init);
+      Constant *SubInit = 0;
+      ConstantExpr *CE = dyn_cast<ConstantExpr>(ConstantFoldInstruction(GEP));
+      if (CE && CE->getOpcode() == Instruction::GetElementPtr)
+        SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
       Changed |= CleanupConstantGlobalUsers(GEP, SubInit);
 
       if (GEP->use_empty()) {