diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4457,7 +4457,8 @@ return PoisonValue::get(GEPTy); if (Q.isUndefValue(Ops[0])) - return UndefValue::get(GEPTy); + // If inbounds, we can choose an out-of-bounds pointer as a base pointer. + return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy); bool IsScalableVec = isa(SrcTy) || any_of(Ops, [](const Value *V) { diff --git a/llvm/test/Transforms/InstSimplify/gep.ll b/llvm/test/Transforms/InstSimplify/gep.ll --- a/llvm/test/Transforms/InstSimplify/gep.ll +++ b/llvm/test/Transforms/InstSimplify/gep.ll @@ -160,7 +160,7 @@ define i64* @undef_inbounds_var_idx(i64 %idx) { ; CHECK-LABEL: @undef_inbounds_var_idx( -; CHECK-NEXT: ret i64* undef +; CHECK-NEXT: ret i64* poison ; %el = getelementptr inbounds i64, i64* undef, i64 %idx ret i64* %el @@ -176,7 +176,7 @@ define <8 x i64*> @undef_vec1() { ; CHECK-LABEL: @undef_vec1( -; CHECK-NEXT: ret <8 x i64*> undef +; CHECK-NEXT: ret <8 x i64*> poison ; %el = getelementptr inbounds i64, i64* undef, <8 x i64> undef ret <8 x i64*> %el