[InstCombine] ctpop(rot(X)) -> ctpop(X)

Proof:
https://alive2.llvm.org/ce/z/ss2zyt - rotl
https://alive2.llvm.org/ce/z/ZM7Aue - rotr

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D101235
This commit is contained in:
Dávid Bolvanský 2021-04-24 18:24:54 +02:00
parent d4ec8ea19c
commit de3fa35cdb
2 changed files with 30 additions and 2 deletions

View File

@ -504,6 +504,11 @@ static Instruction *foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC) {
if (match(Op0, m_BitReverse(m_Value(X))) || match(Op0, m_BSwap(m_Value(X)))) if (match(Op0, m_BitReverse(m_Value(X))) || match(Op0, m_BSwap(m_Value(X))))
return IC.replaceOperand(II, 0, X); return IC.replaceOperand(II, 0, X);
// ctpop(rot(x)) -> ctpop(x)
if (match(Op0, m_FShl(m_Value(X), m_Specific(X), m_Value())) ||
match(Op0, m_FShr(m_Value(X), m_Specific(X), m_Value())))
return IC.replaceOperand(II, 0, X);
// ctpop(x | -x) -> bitwidth - cttz(x, false) // ctpop(x | -x) -> bitwidth - cttz(x, false)
if (Op0->hasOneUse() && if (Op0->hasOneUse() &&
match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) { match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) {

View File

@ -203,11 +203,11 @@ define i32 @ctpop_add(i32 %a, i32 %b) {
define i32 @ctpop_add_no_common_bits(i32 %a, i32 %b) { define i32 @ctpop_add_no_common_bits(i32 %a, i32 %b) {
; CHECK-LABEL: @ctpop_add_no_common_bits( ; CHECK-LABEL: @ctpop_add_no_common_bits(
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.fshl.i32(i32 [[B:%.*]], i32 [[B]], i32 16) ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.fshl.i32(i32 [[A:%.*]], i32 [[B:%.*]], i32 16)
; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP1]]), !range [[RNG1]] ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.ctpop.i32(i32 [[TMP1]]), !range [[RNG1]]
; CHECK-NEXT: ret i32 [[TMP2]] ; CHECK-NEXT: ret i32 [[TMP2]]
; ;
%shl16 = shl i32 %b, 16 %shl16 = shl i32 %a, 16
%ctpop1 = tail call i32 @llvm.ctpop.i32(i32 %shl16) %ctpop1 = tail call i32 @llvm.ctpop.i32(i32 %shl16)
%lshl16 = lshr i32 %b, 16 %lshl16 = lshr i32 %b, 16
%ctpop2 = tail call i32 @llvm.ctpop.i32(i32 %lshl16) %ctpop2 = tail call i32 @llvm.ctpop.i32(i32 %lshl16)
@ -266,3 +266,26 @@ define <2 x i32> @ctpop_add_no_common_bits_vec_use2(<2 x i32> %a, <2 x i32> %b,
%res = add <2 x i32> %ctpop1, %ctpop2 %res = add <2 x i32> %ctpop1, %ctpop2
ret <2 x i32> %res ret <2 x i32> %res
} }
define i8 @ctpop_rotate_left(i8 %a, i8 %amt) {
; CHECK-LABEL: @ctpop_rotate_left(
; CHECK-NEXT: [[CTPOP:%.*]] = tail call i8 @llvm.ctpop.i8(i8 [[A:%.*]]), !range [[RNG0]]
; CHECK-NEXT: ret i8 [[CTPOP]]
;
%rotl = tail call i8 @llvm.fshl.i8(i8 %a, i8 %a, i8 %amt)
%ctpop = tail call i8 @llvm.ctpop.i8(i8 %rotl)
ret i8 %ctpop
}
define i8 @ctpop_rotate_right(i8 %a, i8 %amt) {
; CHECK-LABEL: @ctpop_rotate_right(
; CHECK-NEXT: [[CTPOP:%.*]] = tail call i8 @llvm.ctpop.i8(i8 [[A:%.*]]), !range [[RNG0]]
; CHECK-NEXT: ret i8 [[CTPOP]]
;
%rotr = tail call i8 @llvm.fshr.i8(i8 %a, i8 %a, i8 %amt)
%ctpop = tail call i8 @llvm.ctpop.i8(i8 %rotr)
ret i8 %ctpop
}
declare i8 @llvm.fshl.i8(i8, i8, i8)
declare i8 @llvm.fshr.i8(i8, i8, i8)