Skip to content

[InstCombine] Detect (x ^ -x) as a ~Mask #84868

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

Closed
Closed
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
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4113,9 +4113,12 @@ static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
if (match(V, m_Not(m_Value(X))))
return isMaskOrZero(X, !Not, Q, Depth);

// (X ^ -X) is a ~Mask
if (Not)
return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
// (X ^ (X - 1)) is a Mask
return !Not &&
match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
else
return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
case Instruction::Select:
// c ? Mask0 : Mask1 is a Mask.
return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-and-lowbit-mask.ll
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ define i1 @src_is_notmask_shl(i8 %x_in, i8 %y, i1 %cond) {
ret i1 %r
}

define i1 @src_is_notmask_x_xor_neg_x(i8 %x_in, i8 %y, i1 %cond) {
; CHECK-LABEL: @src_is_notmask_x_xor_neg_x(
; CHECK-NEXT: [[X:%.*]] = xor i8 [[X_IN:%.*]], 123
; CHECK-NEXT: [[NEG_Y:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[NOTMASK0:%.*]] = xor i8 [[NEG_Y]], [[Y]]
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[COND:%.*]], i8 [[NOTMASK0]], i8 7
; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[X]], [[TMP3]]
; CHECK-NEXT: ret i1 [[R]]
;
%x = xor i8 %x_in, 123
%neg_y = sub i8 0, %y
%nmask0 = xor i8 %y, %neg_y
%notmask = select i1 %cond, i8 %nmask0, i8 -8
%and = and i8 %x, %notmask
%r = icmp eq i8 %and, 0
ret i1 %r
}

define i1 @src_is_notmask_shl_fail_multiuse_invert(i8 %x_in, i8 %y, i1 %cond) {
; CHECK-LABEL: @src_is_notmask_shl_fail_multiuse_invert(
; CHECK-NEXT: [[X:%.*]] = xor i8 [[X_IN:%.*]], 122
Expand Down