diff options
| author | Manuel Jacob <me@manueljacob.de> | 2016-01-19 16:34:31 +0000 |
|---|---|---|
| committer | Manuel Jacob <me@manueljacob.de> | 2016-01-19 16:34:31 +0000 |
| commit | c784e6acd91e62eac7bea374ff5eec62a32a511f (patch) | |
| tree | befe0befcf90caed5cd7c276ed309ad3be4bccc8 | |
| parent | e04dd2525c43330e6b7348cb65d4d033d3eaf6db (diff) | |
| download | bcm5719-llvm-c784e6acd91e62eac7bea374ff5eec62a32a511f.tar.gz bcm5719-llvm-c784e6acd91e62eac7bea374ff5eec62a32a511f.zip | |
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
llvm-svn: 258134
| -rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 22 | ||||
| -rw-r--r-- | llvm/test/Assembler/ConstantExprFold.ll | 4 |
2 files changed, 17 insertions, 9 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 35855860129..7e73be124c3 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -2040,11 +2040,13 @@ static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C, 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 *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C, 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); } } diff --git a/llvm/test/Assembler/ConstantExprFold.ll b/llvm/test/Assembler/ConstantExprFold.ll index 3314f8c1af8..94058230519 100644 --- a/llvm/test/Assembler/ConstantExprFold.ll +++ b/llvm/test/Assembler/ConstantExprFold.ll @@ -30,3 +30,7 @@ global i1 icmp slt (i32* getelementptr (%Ty, %Ty* @B, i64 0, i32 0), @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>) |

