[PATCH] D16321: 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:22:40 PST 2016


mjacob created this revision.
mjacob added a reviewer: eddyb.
mjacob added a subscriber: llvm-commits.

http://reviews.llvm.org/D16321

Files:
  lib/IR/ConstantFold.cpp
  test/Assembler/ConstantExprFold.ll

Index: test/Assembler/ConstantExprFold.ll
===================================================================
--- test/Assembler/ConstantExprFold.ll
+++ test/Assembler/ConstantExprFold.ll
@@ -30,3 +30,7 @@
 @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>)
Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -2040,11 +2040,13 @@
     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 @@
         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);
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16321.45266.patch
Type: text/x-patch
Size: 2574 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160119/8f3d9a95/attachment.bin>


More information about the llvm-commits mailing list