diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index 454fe5a91d375..ede89b099e8de 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -2904,7 +2904,9 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) { if (auto *SI = dyn_cast(LHS)) { // We cannot do this fold for elementwise select since ShuffleVector is // not elementwise. - if (SI->getCondition()->getType()->isIntegerTy()) { + if (SI->getCondition()->getType()->isIntegerTy() && + (isa(RHS) || + isGuaranteedNotToBePoison(SI->getCondition()))) { if (Instruction *I = FoldOpIntoSelect(SVI, SI)) return I; } diff --git a/llvm/test/Transforms/InstCombine/vec_shuffle.ll b/llvm/test/Transforms/InstCombine/vec_shuffle.ll index 163d9c9557b23..9fb68b5399c84 100644 --- a/llvm/test/Transforms/InstCombine/vec_shuffle.ll +++ b/llvm/test/Transforms/InstCombine/vec_shuffle.ll @@ -2411,6 +2411,18 @@ define <4 x i32> @shuf_same_length_vec_select(<4 x i1> %cond) { ret <4 x i32> %shuf } +; Make sure we do not fold in this case. +define <4 x i8> @shuf_cmp_may_be_poison(<4 x i8> %x, <4 x i8> %y, i1 %cmp) { +; CHECK-LABEL: @shuf_cmp_may_be_poison( +; CHECK-NEXT: [[Y:%.*]] = select i1 [[CMP:%.*]], <4 x i8> [[Y1:%.*]], <4 x i8> +; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i8> [[Y]], <4 x i8> , <4 x i32> +; CHECK-NEXT: ret <4 x i8> [[TMP1]] +; + %sel = select i1 %cmp, <4 x i8> %y, <4 x i8> + %shuf = shufflevector <4 x i8> %sel, <4 x i8> , <4 x i32> + ret <4 x i8> %shuf +} + declare i1 @cond() declare <4 x i32> @value()