Skip to content

[InstCombine] Fold X!=Y ? ctz(X^Y, true) : BW -> ctz(X^Y, false) #128483

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

Merged
merged 3 commits into from
Feb 24, 2025
Merged
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
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,10 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,

// (X == 0) ? BitWidth : ctz(X)
// (X == -1) ? BitWidth : ctz(~X)
// (X == Y) ? BitWidth : ctz(X ^ Y)
if ((X != CmpLHS || !match(CmpRHS, m_Zero())) &&
(!match(X, m_Not(m_Specific(CmpLHS))) || !match(CmpRHS, m_AllOnes())))
(!match(X, m_Not(m_Specific(CmpLHS))) || !match(CmpRHS, m_AllOnes())) &&
!match(X, m_c_Xor(m_Specific(CmpLHS), m_Specific(CmpRHS))))
return nullptr;

IntrinsicInst *II = cast<IntrinsicInst>(Count);
Expand Down
61 changes: 61 additions & 0 deletions llvm/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,67 @@ define i16 @test_multiuse_trunc_undef(i64 %x, ptr %p) {
ret i16 %cond
}

define i64 @test_pr128441(i64 %x, i64 %y) {
; CHECK-LABEL: @test_pr128441(
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
; CHECK-NEXT: ret i64 [[SEL]]
;
%iszero = icmp ne i64 %x, %y
%xor = xor i64 %x, %y
%cttz = call i64 @llvm.cttz.i64(i64 %xor, i1 true)
%sel = select i1 %iszero, i64 %cttz, i64 64
ret i64 %sel
}

define i64 @test_pr128441_commuted1(i64 %x, i64 %y) {
; CHECK-LABEL: @test_pr128441_commuted1(
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
; CHECK-NEXT: ret i64 [[SEL]]
;
%iszero = icmp ne i64 %x, %y
%xor = xor i64 %y, %x
%cttz = call i64 @llvm.cttz.i64(i64 %xor, i1 true)
%sel = select i1 %iszero, i64 %cttz, i64 64
ret i64 %sel
}

define i64 @test_pr128441_commuted2(i64 %x, i64 %y) {
; CHECK-LABEL: @test_pr128441_commuted2(
; CHECK-NEXT: [[XOR:%.*]] = xor i64 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = call range(i64 0, 65) i64 @llvm.cttz.i64(i64 [[XOR]], i1 false)
; CHECK-NEXT: ret i64 [[SEL]]
;
%iszero = icmp eq i64 %x, %y
%xor = xor i64 %x, %y
%cttz = call i64 @llvm.cttz.i64(i64 %xor, i1 true)
%sel = select i1 %iszero, i64 64, i64 %cttz
ret i64 %sel
}

define i64 @test_pr128441_commuted3_negative(i64 %x, i64 %y) {
; CHECK-LABEL: @test_pr128441_commuted3_negative(
; CHECK-NEXT: ret i64 64
;
%iszero = icmp eq i64 %x, %y
%xor = xor i64 %y, %x
%cttz = call i64 @llvm.cttz.i64(i64 %xor, i1 true)
%sel = select i1 %iszero, i64 %cttz, i64 64
ret i64 %sel
}

define i64 @test_pr128441_commuted4_negative(i64 %x, i64 %y) {
; CHECK-LABEL: @test_pr128441_commuted4_negative(
; CHECK-NEXT: ret i64 64
;
%iszero = icmp ne i64 %x, %y
%xor = xor i64 %x, %y
%cttz = call i64 @llvm.cttz.i64(i64 %xor, i1 true)
%sel = select i1 %iszero, i64 64, i64 %cttz
ret i64 %sel
}

declare i16 @llvm.ctlz.i16(i16, i1)
declare i32 @llvm.ctlz.i32(i32, i1)
declare i64 @llvm.ctlz.i64(i64, i1)
Expand Down
Loading