Skip to content

Commit 16dad11

Browse files
authored
[ValueTracking] Have sub and xor in KnownNonZero take the same exact path (#146975)
If x - y == 0, then x ^ y == 0. Therefore, we can do the exact same checks. https://alive2.llvm.org/ce/z/MtBRoj
1 parent c3c3919 commit 16dad11

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,14 +3036,12 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
30363036
return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
30373037
break;
30383038

3039+
// Iff x - y != 0, then x ^ y != 0
3040+
// Therefore we can do the same exact checks
3041+
case Instruction::Xor:
30393042
case Instruction::Sub:
30403043
return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
30413044
I->getOperand(1), Depth);
3042-
case Instruction::Xor:
3043-
// (X ^ (X != 0)) is non zero
3044-
if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3045-
return true;
3046-
break;
30473045
case Instruction::Or:
30483046
// (X | (X != 0)) is non zero
30493047
if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))

llvm/test/Transforms/InstCombine/ctpop-cttz.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,24 @@ define <2 x i32> @ctpop3v_poison(<2 x i32> %0) {
127127
%5 = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %4)
128128
ret <2 x i32> %5
129129
}
130+
131+
define i32 @ctpop_xor(i32 %x, i32 %y) {
132+
; CHECK-LABEL: @ctpop_xor(
133+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
134+
; CHECK-NEXT: br i1 [[CMP_NOT]], label [[FALSE:%.*]], label [[TRUE:%.*]]
135+
; CHECK: true:
136+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[X]], [[Y]]
137+
; CHECK-NEXT: [[RET:%.*]] = call range(i32 1, 33) i32 @llvm.ctpop.i32(i32 [[XOR]])
138+
; CHECK-NEXT: ret i32 [[RET]]
139+
; CHECK: false:
140+
; CHECK-NEXT: ret i32 0
141+
;
142+
%cmp = icmp ne i32 %x, %y
143+
br i1 %cmp, label %true, label %false
144+
true:
145+
%xor = xor i32 %x, %y
146+
%ret = call i32 @llvm.ctpop.i32(i32 %xor)
147+
ret i32 %ret
148+
false:
149+
ret i32 0
150+
}

0 commit comments

Comments
 (0)