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 @@ -4396,10 +4396,10 @@ if (Q.isUndefValue(Idx)) return PoisonValue::get(Vec->getType()); - // If the scalar is undef, and there is no risk of propagating poison from the - // vector value, simplify to the vector value. - if (Q.isUndefValue(Val) && - isGuaranteedNotToBeUndefOrPoison(Vec)) + // If the scalar is poison, or it is undef and there is no risk of + // propagating poison from the vector value, simplify to the vector value. + if (isa(Val) || + (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec))) return Vec; // If we are extracting a value from a vector, then inserting it into the same diff --git a/llvm/test/Transforms/InstSimplify/insertelement.ll b/llvm/test/Transforms/InstSimplify/insertelement.ll --- a/llvm/test/Transforms/InstSimplify/insertelement.ll +++ b/llvm/test/Transforms/InstSimplify/insertelement.ll @@ -52,8 +52,7 @@ define <4 x i32> @elem_poison(<4 x i32> %A) { ; CHECK-LABEL: @elem_poison( -; CHECK-NEXT: [[B:%.*]] = insertelement <4 x i32> [[A:%.*]], i32 poison, i32 1 -; CHECK-NEXT: ret <4 x i32> [[B]] +; CHECK-NEXT: ret <4 x i32> [[A:%.*]] ; %B = insertelement <4 x i32> %A, i32 poison, i32 1 ret <4 x i32> %B