From de3fa35cdb6f5136b34716396763b15ed01d186b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Bolvansk=C3=BD?= Date: Sat, 24 Apr 2021 18:24:54 +0200 Subject: [PATCH] [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 --- .../InstCombine/InstCombineCalls.cpp | 5 ++++ llvm/test/Transforms/InstCombine/ctpop.ll | 27 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 954c853ab7f0..378682b7b9cd 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -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)))) 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) if (Op0->hasOneUse() && match(Op0, m_c_Or(m_Value(X), m_Neg(m_Deferred(X))))) { diff --git a/llvm/test/Transforms/InstCombine/ctpop.ll b/llvm/test/Transforms/InstCombine/ctpop.ll index 9a238d27fd31..b389b1f6617a 100644 --- a/llvm/test/Transforms/InstCombine/ctpop.ll +++ b/llvm/test/Transforms/InstCombine/ctpop.ll @@ -203,11 +203,11 @@ define i32 @ctpop_add(i32 %a, i32 %b) { define i32 @ctpop_add_no_common_bits(i32 %a, i32 %b) { ; 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: ret i32 [[TMP2]] ; - %shl16 = shl i32 %b, 16 + %shl16 = shl i32 %a, 16 %ctpop1 = tail call i32 @llvm.ctpop.i32(i32 %shl16) %lshl16 = lshr i32 %b, 16 %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 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)