Skip to content

[InstCombine] Drop samesign flags in foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed #120373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,20 @@ static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
// RHS. For example,
// (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
// (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
if (IsSuperSetOrEqual(BCst, DCst))
if (IsSuperSetOrEqual(BCst, DCst)) {
// We can't guarantee that samesign hold after this fold.
RHS->setSameSign(false);
return RHS;
}
// Otherwise, B is a subset of D. If B and E have a common bit set,
// ie. (B & E) != 0, then LHS is subsumed by RHS. For example.
// (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code");
if ((*BCst & ECst) != 0)
if ((*BCst & ECst) != 0) {
// We can't guarantee that samesign hold after this fold.
RHS->setSameSign(false);
return RHS;
}
// Otherwise, LHS and RHS contradict and the whole expression becomes false
// (or true if negated.) For example,
// (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false.
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-logical.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1900,3 +1900,28 @@ define i1 @masked_icmps_bmask_notmixed_not_subset_notoptimized(i32 %A) {
%res = and i1 %tst1, %tst2
ret i1 %res
}

define i1 @pr120361(i8 %x, i8 %y) {
; CHECK-LABEL: @pr120361(
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[X:%.*]], -1
; CHECK-NEXT: ret i1 [[CMP1]]
;
%cmp1 = icmp samesign eq i8 %x, -1
%cmp2 = icmp ne i8 %x, 0
%result = select i1 %cmp2, i1 %cmp1, i1 false
ret i1 %result
}

define i1 @pr120361_v2(i32 %x) {
; CHECK-LABEL: @pr120361_v2(
; CHECK-NEXT: [[AND2:%.*]] = and i32 [[X:%.*]], -113
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[AND2]], 15
; CHECK-NEXT: ret i1 [[CMP2]]
;
%and1 = and i32 %x, 15
%cmp1 = icmp ne i32 %and1, 0
%and2 = and i32 %x, -113
%cmp2 = icmp samesign eq i32 %and2, 15
%and = select i1 %cmp1, i1 %cmp2, i1 false
ret i1 %and
}
Loading