Skip to content

Commit 546ea60

Browse files
committed
[InstCombine] Fold X!=Y ? ctz(X^Y,true) : BW -> ctz(X^Y,false)
1 parent ec4fa84 commit 546ea60

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,10 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
12271227

12281228
// (X == 0) ? BitWidth : ctz(X)
12291229
// (X == -1) ? BitWidth : ctz(~X)
1230-
if ((X != CmpLHS || !match(CmpRHS, m_Zero())) &&
1231-
(!match(X, m_Not(m_Specific(CmpLHS))) || !match(CmpRHS, m_AllOnes())))
1230+
// (X == Y) ? BitWidth : ctz(X ^ Y)
1231+
if (!(X == CmpLHS && match(CmpRHS, m_Zero())) &&
1232+
!(match(X, m_Not(m_Specific(CmpLHS))) && match(CmpRHS, m_AllOnes())) &&
1233+
!match(X, m_c_Xor(m_Specific(CmpLHS), m_Specific(CmpRHS))))
12321234
return nullptr;
12331235

12341236
IntrinsicInst *II = cast<IntrinsicInst>(Count);

llvm/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,8 @@ define i16 @test_multiuse_trunc_undef(i64 %x, ptr %p) {
659659

660660
define i64 @test_pr128441(i64 %x, i64 %y) {
661661
; CHECK-LABEL: @test_pr128441(
662-
; CHECK-NEXT: [[ISZERO_NOT:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
663-
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X]], [[Y]]
664-
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 true)
665-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[ISZERO_NOT]], i64 64, i64 [[CTTZ]]
662+
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X:%.*]], [[Y:%.*]]
663+
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
666664
; CHECK-NEXT: ret i64 [[SEL]]
667665
;
668666
%iszero = icmp ne i64 %x, %y
@@ -674,10 +672,8 @@ define i64 @test_pr128441(i64 %x, i64 %y) {
674672

675673
define i64 @test_pr128441_commuted1(i64 %x, i64 %y) {
676674
; CHECK-LABEL: @test_pr128441_commuted1(
677-
; CHECK-NEXT: [[ISZERO_NOT:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
678-
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[Y]], [[X]]
679-
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 true)
680-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[ISZERO_NOT]], i64 64, i64 [[CTTZ]]
675+
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[Y:%.*]], [[X:%.*]]
676+
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
681677
; CHECK-NEXT: ret i64 [[SEL]]
682678
;
683679
%iszero = icmp ne i64 %x, %y
@@ -689,10 +685,8 @@ define i64 @test_pr128441_commuted1(i64 %x, i64 %y) {
689685

690686
define i64 @test_pr128441_commuted2(i64 %x, i64 %y) {
691687
; CHECK-LABEL: @test_pr128441_commuted2(
692-
; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
693-
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X]], [[Y]]
694-
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 true)
695-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[ISZERO]], i64 64, i64 [[CTTZ]]
688+
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X:%.*]], [[Y:%.*]]
689+
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
696690
; CHECK-NEXT: ret i64 [[SEL]]
697691
;
698692
%iszero = icmp eq i64 %x, %y

0 commit comments

Comments
 (0)