Skip to content

Commit e577f14

Browse files
authored
[InstCombine] Use m_NotForbidPoison when folding (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y) (#114345)
Alive2: https://alive2.llvm.org/ce/z/mTGCo- We cannot reuse `~X` if `m_AllOnes` matches a vector constant with some poison elts. An alternative solution is to create a new not instead of reusing `~X`. But it doesn't worth the effort because we need to add a one-use check. Fixes #113869.
1 parent c752efb commit e577f14

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
10561056
// Strictness of the comparison is irrelevant.
10571057
X = Cmp0;
10581058
Y = Cmp1;
1059-
if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) {
1059+
if (match(FVal, m_c_Add(m_NotForbidPoison(m_Specific(X)), m_Specific(Y)))) {
10601060
// (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
10611061
// (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
10621062
BinaryOperator *BO = cast<BinaryOperator>(FVal);

llvm/test/Transforms/InstCombine/saturating-add-sub.ll

+15
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,21 @@ define <2 x i32> @uadd_sat_not_ugt_commute_add(<2 x i32> %x, <2 x i32> %yp) {
18901890
ret <2 x i32> %r
18911891
}
18921892

1893+
define <2 x i32> @uadd_sat_not_ugt_commute_add_partial_poison(<2 x i32> %x, <2 x i32> %yp) {
1894+
; CHECK-LABEL: @uadd_sat_not_ugt_commute_add_partial_poison(
1895+
; CHECK-NEXT: [[NOTX:%.*]] = xor <2 x i32> [[X:%.*]], <i32 -1, i32 poison>
1896+
; CHECK-NEXT: [[A:%.*]] = add nuw <2 x i32> [[YP:%.*]], [[NOTX]]
1897+
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[YP]], [[X]]
1898+
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> <i32 -1, i32 -1>, <2 x i32> [[A]]
1899+
; CHECK-NEXT: ret <2 x i32> [[R]]
1900+
;
1901+
%notx = xor <2 x i32> %x, <i32 -1, i32 poison>
1902+
%a = add nuw <2 x i32> %yp, %notx
1903+
%c = icmp ugt <2 x i32> %yp, %x
1904+
%r = select <2 x i1> %c, <2 x i32> <i32 -1, i32 -1>, <2 x i32> %a
1905+
ret <2 x i32> %r
1906+
}
1907+
18931908
define i32 @uadd_sat_not_commute_select(i32 %x, i32 %y) {
18941909
; CHECK-LABEL: @uadd_sat_not_commute_select(
18951910
; CHECK-NEXT: [[NOTX:%.*]] = xor i32 [[X:%.*]], -1

0 commit comments

Comments
 (0)