[llvm] [AggressiveInstCombine] Improve popcount matching if the input has known zero bits (PR #142501)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 3 04:57:25 PDT 2025
================
@@ -191,3 +191,51 @@ define <4 x i32> @popcount32vec(<4 x i32> %0) {
%13 = lshr <4 x i32> %12, <i32 24, i32 24, i32 24, i32 24>
ret <4 x i32> %13
}
+
+define signext i32 @popcount64_zext(i32 %x) {
+; CHECK-LABEL: @popcount64_zext(
+; CHECK-NEXT: [[ZEXT:%.*]] = zext i32 [[X:%.*]] to i64
+; CHECK-NEXT: [[TMP12:%.*]] = call i64 @llvm.ctpop.i64(i64 [[ZEXT]])
+; CHECK-NEXT: [[TMP13:%.*]] = trunc nuw nsw i64 [[TMP12]] to i32
+; CHECK-NEXT: ret i32 [[TMP13]]
+;
+ %zext = zext i32 %x to i64
+ %1 = lshr i64 %zext, 1
+ %2 = and i64 %1, 1431655765
+ %3 = sub nsw i64 %zext, %2
+ %4 = and i64 %3, 3689348814741910323
+ %5 = lshr i64 %3, 2
+ %6 = and i64 %5, 3689348814741910323
+ %7 = add nuw nsw i64 %6, %4
+ %8 = lshr i64 %7, 4
+ %9 = add nuw nsw i64 %8, %7
+ %10 = and i64 %9, 1085102592571150095
+ %11 = mul i64 %10, 72340172838076673
+ %12 = lshr i64 %11, 56
+ %13 = trunc nuw nsw i64 %12 to i32
+ ret i32 %13
+}
+
+define signext i32 @popcount64_mask(i64 %x) {
----------------
dtcxzyw wrote:
```suggestion
define i32 @popcount64_mask(i64 %x) {
```
https://github.com/llvm/llvm-project/pull/142501
More information about the llvm-commits
mailing list