diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h --- a/clang/lib/CodeGen/CGBuilder.h +++ b/clang/lib/CodeGen/CGBuilder.h @@ -309,7 +309,7 @@ const llvm::StructLayout *Layout = DL.getStructLayout(ElTy); auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index)); - return Address(CreatePreserveStructAccessIndex(Addr.getPointer(), + return Address(CreatePreserveStructAccessIndex(ElTy, Addr.getPointer(), Index, FieldIndex, DbgInfo), Addr.getAlignment().alignmentAtOffset(Offset)); } diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -3438,7 +3438,8 @@ llvm::DIType *DbgInfo = nullptr; if (arrayType) DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType(*arrayType, loc); - eltPtr = CGF.Builder.CreatePreserveArrayAccessIndex(addr.getPointer(), + eltPtr = CGF.Builder.CreatePreserveArrayAccessIndex(addr.getElementType(), + addr.getPointer(), indices.size() - 1, idx, DbgInfo); } diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -2521,8 +2521,9 @@ return V; } - Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension, - unsigned LastIndex, MDNode *DbgInfo) { + Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base, + unsigned Dimension, unsigned LastIndex, + MDNode *DbgInfo) { assert(isa(Base->getType()) && "Invalid Base ptr type for preserve.array.access.index."); auto *BaseType = Base->getType(); @@ -2535,7 +2536,7 @@ IdxList.push_back(LastIndexV); Type *ResultType = - GetElementPtrInst::getGEPReturnType(Base, IdxList); + GetElementPtrInst::getGEPReturnType(ElTy, Base, IdxList); Module *M = BB->getParent()->getParent(); Function *FnPreserveArrayAccessIndex = Intrinsic::getDeclaration( @@ -2569,8 +2570,9 @@ return Fn; } - Value *CreatePreserveStructAccessIndex(Value *Base, unsigned Index, - unsigned FieldIndex, MDNode *DbgInfo) { + Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base, + unsigned Index, unsigned FieldIndex, + MDNode *DbgInfo) { assert(isa(Base->getType()) && "Invalid Base ptr type for preserve.struct.access.index."); auto *BaseType = Base->getType(); @@ -2578,7 +2580,7 @@ Value *GEPIndex = getInt32(Index); Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); Type *ResultType = - GetElementPtrInst::getGEPReturnType(Base, {Zero, GEPIndex}); + GetElementPtrInst::getGEPReturnType(ElTy, Base, {Zero, GEPIndex}); Module *M = BB->getParent()->getParent(); Function *FnPreserveStructAccessIndex = Intrinsic::getDeclaration( diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -1039,11 +1039,6 @@ /// Returns the pointer type returned by the GEP /// instruction, which may be a vector of pointers. - static Type *getGEPReturnType(Value *Ptr, ArrayRef IdxList) { - return getGEPReturnType( - cast(Ptr->getType()->getScalarType())->getElementType(), - Ptr, IdxList); - } static Type *getGEPReturnType(Type *ElTy, Value *Ptr, ArrayRef IdxList) { Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),