Skip to content

[InstCombine] Infer exact for lshr by cttz #136696

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 13 commits into from
Apr 29, 2025
6 changes: 6 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,12 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
I.setIsExact();
return true;
}
// Infer 'exact' flag if shift amount is cttz(x) on the same operand.
if (match(I.getOperand(1), m_Intrinsic<Intrinsic::cttz>(
m_Specific(I.getOperand(0)), m_Value()))) {
I.setIsExact();
return true;
}
}

// Compute what we know about shift count.
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/Transforms/InstCombine/cttz-shift-exact.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes=instcombine -S < %s | FileCheck %s

declare i32 @llvm.cttz.i32(i32, i1)

define i32 @test_cttz_lshr(i32 %x) {
; CHECK-LABEL: define i32 @test_cttz_lshr(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X]], i1 true)
; CHECK-NEXT: [[SH:%.*]] = lshr exact i32 [[X]], [[CTTZ]]
; CHECK-NEXT: ret i32 [[SH]]
;
%cttz = call i32 @llvm.cttz.i32(i32 %x, i1 false)
%sh = lshr i32 %x, %cttz
ret i32 %sh
}

define i32 @test_cttz_ashr(i32 %x) {
; CHECK-LABEL: define i32 @test_cttz_ashr(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X]], i1 true)
; CHECK-NEXT: [[SH:%.*]] = ashr exact i32 [[X]], [[CTTZ]]
; CHECK-NEXT: ret i32 [[SH]]
;
%cttz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%sh = ashr i32 %x, %cttz
ret i32 %sh
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add one negative test where the cttz operand is an unrelated value.

define i32 @test_cttz_diff_operand(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @test_cttz_diff_operand(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Y]], i1 true)
; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X]], [[CTTZ]]
; CHECK-NEXT: ret i32 [[SH]]
;
%cttz = call i32 @llvm.cttz.i32(i32 %y, i1 true)
%sh = lshr i32 %x, %cttz
ret i32 %sh
}

2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,7 @@ define i32 @pr47322_more_poisonous_replacement(i32 %arg) {
; CHECK-LABEL: @pr47322_more_poisonous_replacement(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[ARG:%.*]], 0
; CHECK-NEXT: [[TRAILING:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[SHIFTED:%.*]] = lshr i32 [[ARG]], [[TRAILING]]
; CHECK-NEXT: [[SHIFTED:%.*]] = lshr exact i32 [[ARG]], [[TRAILING]]
; CHECK-NEXT: [[R1_SROA_0_1:%.*]] = select i1 [[CMP]], i32 0, i32 [[SHIFTED]]
; CHECK-NEXT: ret i32 [[R1_SROA_0_1]]
;
Expand Down