fold gep undef, ... -> undef
authorChris Lattner <sabre@nondot.org>
Sun, 17 Oct 2004 21:54:55 +0000 (21:54 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 17 Oct 2004 21:54:55 +0000 (21:54 +0000)
This comes up many times in perlbmk and probably others.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17100 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantFold.cpp

index b56366ea6635e5148e315b54c722e645a1b8678d..d224afefc5c6bde78cf44e7dcceb8a7fea300aae 100644 (file)
@@ -1011,8 +1011,15 @@ Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
   if (IdxList.size() == 0 ||
       (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
     return const_cast<Constant*>(C);
-  Constant *Idx0 = cast<Constant>(IdxList[0]);
 
+  if (isa<UndefValue>(C)) {
+    const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
+                                                       true);
+    assert(Ty != 0 && "Invalid indices for GEP!");
+    return UndefValue::get(PointerType::get(Ty));
+  }
+
+  Constant *Idx0 = cast<Constant>(IdxList[0]);
   if (C->isNullValue()) {
     bool isNull = true;
     for (unsigned i = 0, e = IdxList.size(); i != e; ++i)