Skip to content

Commit 5ca325e

Browse files
committed
[InstCombine] Detect (x ^ -x) as a ~Mask
Proof: https://alive2.llvm.org/ce/z/TAFmPw This is a lemma for clearing up some of the regressions that llvm#84688 causes. Closes llvm#84868
1 parent 377da51 commit 5ca325e

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,9 +4113,12 @@ static bool isMaskOrZero(const Value *V, bool Not, const SimplifyQuery &Q,
41134113
if (match(V, m_Not(m_Value(X))))
41144114
return isMaskOrZero(X, !Not, Q, Depth);
41154115

4116+
// (X ^ -X) is a ~Mask
4117+
if (Not)
4118+
return match(V, m_c_Xor(m_Value(X), m_Neg(m_Deferred(X))));
41164119
// (X ^ (X - 1)) is a Mask
4117-
return !Not &&
4118-
match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
4120+
else
4121+
return match(V, m_c_Xor(m_Value(X), m_Add(m_Deferred(X), m_AllOnes())));
41194122
case Instruction::Select:
41204123
// c ? Mask0 : Mask1 is a Mask.
41214124
return isMaskOrZero(I->getOperand(1), Not, Q, Depth) &&

llvm/test/Transforms/InstCombine/icmp-and-lowbit-mask.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,10 @@ define i1 @src_is_notmask_shl(i8 %x_in, i8 %y, i1 %cond) {
456456
define i1 @src_is_notmask_x_xor_neg_x(i8 %x_in, i8 %y, i1 %cond) {
457457
; CHECK-LABEL: @src_is_notmask_x_xor_neg_x(
458458
; CHECK-NEXT: [[X:%.*]] = xor i8 [[X_IN:%.*]], 123
459-
; CHECK-NEXT: [[NEG_Y:%.*]] = sub i8 0, [[Y:%.*]]
459+
; CHECK-NEXT: [[NEG_Y:%.*]] = add i8 [[Y:%.*]], -1
460460
; CHECK-NEXT: [[NOTMASK0:%.*]] = xor i8 [[NEG_Y]], [[Y]]
461-
; CHECK-NEXT: [[NOTMASK:%.*]] = select i1 [[COND:%.*]], i8 [[NOTMASK0]], i8 -8
462-
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[NOTMASK]]
463-
; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[AND]], 0
461+
; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[COND:%.*]], i8 [[NOTMASK0]], i8 7
462+
; CHECK-NEXT: [[R:%.*]] = icmp ule i8 [[X]], [[TMP3]]
464463
; CHECK-NEXT: ret i1 [[R]]
465464
;
466465
%x = xor i8 %x_in, 123

0 commit comments

Comments
 (0)