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
This commit is contained in:
Manuel Jacob 2016-01-19 16:34:31 +00:00
parent e04dd2525c
commit c784e6acd9
2 changed files with 17 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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>)