Skip to content

Commit 9304af3

Browse files
authored
[InstCombine] Fixing wrong select folding in vectors with undef elements (#102244)
This PR fixes #98435. `SimplifyDemandedVectorElts` mishandles the undef by assuming that !isNullValue() means the condition is true. By preventing any value that we're not certain equals 1 or 0, it avoids having to make any particular choice by not demanding bits from a particular branch with potentially picking a wrong value. Proof: https://alive2.llvm.org/ce/z/r8CmEu
1 parent 0e12453 commit 9304af3

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,17 +1735,12 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
17351735
APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
17361736
if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
17371737
for (unsigned i = 0; i < VWidth; i++) {
1738-
// isNullValue() always returns false when called on a ConstantExpr.
1739-
// Skip constant expressions to avoid propagating incorrect information.
17401738
Constant *CElt = CV->getAggregateElement(i);
1741-
if (isa<ConstantExpr>(CElt))
1742-
continue;
1743-
// TODO: If a select condition element is undef, we can demand from
1744-
// either side. If one side is known undef, choosing that side would
1745-
// propagate undef.
1739+
1740+
// isNullValue() always returns false when called on a ConstantExpr.
17461741
if (CElt->isNullValue())
17471742
DemandedLHS.clearBit(i);
1748-
else
1743+
else if (CElt->isOneValue())
17491744
DemandedRHS.clearBit(i);
17501745
}
17511746
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=instcombine < %s 2>&1 | FileCheck %s
3+
4+
define <2 x i1> @pr98435(<2 x i1> %val) {
5+
; CHECK-LABEL: define <2 x i1> @pr98435(
6+
; CHECK-SAME: <2 x i1> [[VAL:%.*]]) {
7+
; CHECK-NEXT: [[VAL1:%.*]] = select <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 true, i1 true>, <2 x i1> [[VAL]]
8+
; CHECK-NEXT: ret <2 x i1> [[VAL1]]
9+
;
10+
%val1 = select <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 true, i1 true>, <2 x i1> %val
11+
ret <2 x i1> %val1
12+
}

0 commit comments

Comments
 (0)