diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -504,6 +504,11 @@ 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 --- a/llvm/test/Transforms/InstCombine/ctpop.ll +++ b/llvm/test/Transforms/InstCombine/ctpop.ll @@ -203,11 +203,11 @@ 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 @@ %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)