[llvm] r258134 - Fix constant folding of constant vector GEPs with undef or null as pointer argument.

Manuel Jacob via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 19 08:34:32 PST 2016


Author: mjacob
Date: Tue Jan 19 10:34:31 2016
New Revision: 258134

URL: http://llvm.org/viewvc/llvm-project?rev=258134&view=rev
Log:
Fix constant folding of constant vector GEPs with undef or null as pointer argument.

Reviewers: eddyb

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D16321

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Assembler/ConstantExprFold.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=258134&r1=258133&r2=258134&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Tue Jan 19 10:34:31 2016
@@ -2040,11 +2040,13 @@ static Constant *ConstantFoldGetElementP
     return C;
 
   if (isa<UndefValue>(C)) {
-    PointerType *PtrTy = cast<PointerType>(C->getType());
-    Type *Ty = GetElementPtrInst::getIndexedType(
-        cast<PointerType>(PtrTy->getScalarType())->getElementType(), Idxs);
+    PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType());
+    Type *Ty = GetElementPtrInst::getIndexedType(PtrTy->getElementType(), Idxs);
     assert(Ty && "Invalid indices for GEP!");
-    return UndefValue::get(PointerType::get(Ty, PtrTy->getAddressSpace()));
+    Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
+    if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
+      GEPTy = VectorType::get(GEPTy, VT->getNumElements());
+    return UndefValue::get(GEPTy);
   }
 
   if (C->isNullValue()) {
@@ -2055,12 +2057,14 @@ static Constant *ConstantFoldGetElementP
         break;
       }
     if (isNull) {
-      PointerType *PtrTy = cast<PointerType>(C->getType());
-      Type *Ty = GetElementPtrInst::getIndexedType(
-          cast<PointerType>(PtrTy->getScalarType())->getElementType(), Idxs);
+      PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType());
+      Type *Ty =
+          GetElementPtrInst::getIndexedType(PtrTy->getElementType(), Idxs);
       assert(Ty && "Invalid indices for GEP!");
-      return ConstantPointerNull::get(PointerType::get(Ty,
-                                                       PtrTy->getAddressSpace()));
+      Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
+      if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
+        GEPTy = VectorType::get(GEPTy, VT->getNumElements());
+      return Constant::getNullValue(GEPTy);
     }
   }
 

Modified: llvm/trunk/test/Assembler/ConstantExprFold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Assembler/ConstantExprFold.ll?rev=258134&r1=258133&r2=258134&view=diff
==============================================================================
--- llvm/trunk/test/Assembler/ConstantExprFold.ll (original)
+++ llvm/trunk/test/Assembler/ConstantExprFold.ll Tue Jan 19 10:34:31 2016
@@ -30,3 +30,7 @@ global i1 icmp slt (i32* getelementptr (
 @cons = weak global i32 0, align 8              ; <i32*> [#uses=1]
 global i64 and (i64 ptrtoint (i32* @cons to i64), i64 7)
 
+global <2 x i8*> getelementptr(i8, <2 x i8*> undef, <2 x i64> <i64 1, i64 1>)
+global <2 x i8*> getelementptr({ i8 }, <2 x { i8 }*> undef, <2 x i64> <i64 1, i64 1>, <2 x i32> <i32 0, i32 0>)
+global <2 x i8*> getelementptr(i8, <2 x i8*> zeroinitializer, <2 x i64> <i64 0, i64 0>)
+global <2 x i8*> getelementptr({ i8 }, <2 x { i8 }*> zeroinitializer, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>)




More information about the llvm-commits mailing list