Skip to content

Commit 27bf45a

Browse files
authored
[InstCombine] Fix poison safety of folding shufflevector into select (#115483)
We are allowed to fold shufflevector into select iff the condition is guaranteed not to be poison or the RHS is a poison. Alive2: https://alive2.llvm.org/ce/z/28zEWR Closes #115465.
1 parent d822c09 commit 27bf45a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2904,7 +2904,9 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
29042904
if (auto *SI = dyn_cast<SelectInst>(LHS)) {
29052905
// We cannot do this fold for elementwise select since ShuffleVector is
29062906
// not elementwise.
2907-
if (SI->getCondition()->getType()->isIntegerTy()) {
2907+
if (SI->getCondition()->getType()->isIntegerTy() &&
2908+
(isa<PoisonValue>(RHS) ||
2909+
isGuaranteedNotToBePoison(SI->getCondition()))) {
29082910
if (Instruction *I = FoldOpIntoSelect(SVI, SI))
29092911
return I;
29102912
}

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,18 @@ define <4 x i32> @shuf_same_length_vec_select(<4 x i1> %cond) {
24112411
ret <4 x i32> %shuf
24122412
}
24132413

2414+
; Make sure we do not fold in this case.
2415+
define <4 x i8> @shuf_cmp_may_be_poison(<4 x i8> %x, <4 x i8> %y, i1 %cmp) {
2416+
; CHECK-LABEL: @shuf_cmp_may_be_poison(
2417+
; CHECK-NEXT: [[Y:%.*]] = select i1 [[CMP:%.*]], <4 x i8> [[Y1:%.*]], <4 x i8> <i8 0, i8 poison, i8 0, i8 poison>
2418+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <4 x i8> [[Y]], <4 x i8> <i8 poison, i8 1, i8 poison, i8 3>, <4 x i32> <i32 0, i32 5, i32 2, i32 7>
2419+
; CHECK-NEXT: ret <4 x i8> [[TMP1]]
2420+
;
2421+
%sel = select i1 %cmp, <4 x i8> %y, <4 x i8> <i8 0, i8 poison, i8 0, i8 poison>
2422+
%shuf = shufflevector <4 x i8> %sel, <4 x i8> <i8 poison, i8 1, i8 poison, i8 3>, <4 x i32> <i32 0, i32 5, i32 2, i32 7>
2423+
ret <4 x i8> %shuf
2424+
}
2425+
24142426
declare i1 @cond()
24152427
declare <4 x i32> @value()
24162428

0 commit comments

Comments
 (0)