Skip to content

Commit

Permalink
[InstCombine] Detect (x ^ -x) as a ~Mask
Browse files Browse the repository at this point in the history
Proof: https://alive2.llvm.org/ce/z/TAFmPw

This is a lemma for clearing up some of the regressions that #84688
causes.

Closes #84868
  • Loading branch information
goldsteinn committed Mar 12, 2024
1 parent 377da51 commit 5ca325e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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
7 changes: 3 additions & 4 deletions llvm/test/Transforms/InstCombine/icmp-and-lowbit-mask.ll
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,10 @@ define i1 @src_is_notmask_shl(i8 %x_in, i8 %y, i1 %cond) {
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:%.*]] = sub i8 0, [[Y:%.*]]
; CHECK-NEXT: [[NEG_Y:%.*]] = add i8 [[Y:%.*]], -1
; CHECK-NEXT: [[NOTMASK0:%.*]] = xor i8 [[NEG_Y]], [[Y]]
; CHECK-NEXT: [[NOTMASK:%.*]] = select i1 [[COND:%.*]], i8 [[NOTMASK0]], i8 -8
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NOTMASK]]
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[AND]], 0
; 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
Expand Down

0 comments on commit 5ca325e

Please sign in to comment.