Skip to content

Commit 6f68010

Browse files
authored
[InstCombine] Drop samesign flags in foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed (#120373)
Counterexamples: https://alive2.llvm.org/ce/z/6Ks8Qz Closes #120361.
1 parent c6967ef commit 6f68010

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,20 @@ static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
455455
// RHS. For example,
456456
// (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
457457
// (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
458-
if (IsSuperSetOrEqual(BCst, DCst))
458+
if (IsSuperSetOrEqual(BCst, DCst)) {
459+
// We can't guarantee that samesign hold after this fold.
460+
RHS->setSameSign(false);
459461
return RHS;
462+
}
460463
// Otherwise, B is a subset of D. If B and E have a common bit set,
461464
// ie. (B & E) != 0, then LHS is subsumed by RHS. For example.
462465
// (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
463466
assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code");
464-
if ((*BCst & ECst) != 0)
467+
if ((*BCst & ECst) != 0) {
468+
// We can't guarantee that samesign hold after this fold.
469+
RHS->setSameSign(false);
465470
return RHS;
471+
}
466472
// Otherwise, LHS and RHS contradict and the whole expression becomes false
467473
// (or true if negated.) For example,
468474
// (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false.

llvm/test/Transforms/InstCombine/icmp-logical.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,3 +1900,28 @@ define i1 @masked_icmps_bmask_notmixed_not_subset_notoptimized(i32 %A) {
19001900
%res = and i1 %tst1, %tst2
19011901
ret i1 %res
19021902
}
1903+
1904+
define i1 @pr120361(i8 %x, i8 %y) {
1905+
; CHECK-LABEL: @pr120361(
1906+
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X:%.*]], -1
1907+
; CHECK-NEXT: ret i1 [[CMP1]]
1908+
;
1909+
%cmp1 = icmp samesign eq i8 %x, -1
1910+
%cmp2 = icmp ne i8 %x, 0
1911+
%result = select i1 %cmp2, i1 %cmp1, i1 false
1912+
ret i1 %result
1913+
}
1914+
1915+
define i1 @pr120361_v2(i32 %x) {
1916+
; CHECK-LABEL: @pr120361_v2(
1917+
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[X:%.*]], -113
1918+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND2]], 15
1919+
; CHECK-NEXT: ret i1 [[CMP2]]
1920+
;
1921+
%and1 = and i32 %x, 15
1922+
%cmp1 = icmp ne i32 %and1, 0
1923+
%and2 = and i32 %x, -113
1924+
%cmp2 = icmp samesign eq i32 %and2, 15
1925+
%and = select i1 %cmp1, i1 %cmp2, i1 false
1926+
ret i1 %and
1927+
}

0 commit comments

Comments
 (0)