[llvm] [AArch64][CostModel] Fix stale costs for "ctpop" Intrinsic (PR #191769)

via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 13 00:57:49 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Yashwant Singh (yashssh)

<details>
<summary>Changes</summary>

https://reviews.llvm.org/D103952 Introduced codegen based costs
for ctpop intrinsic variants. However, with recent compiler the
codegen is more optimised but the costs have remained the same.

Old codegen: https://godbolt.org/z/YGeEGbqMY
New codegen: https://godbolt.org/z/jr3GjT9EK

---
Full diff: https://github.com/llvm/llvm-project/pull/191769.diff


2 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp (+9-4) 
- (modified) llvm/test/Analysis/CostModel/AArch64/ctpop.ll (+2-2) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index 734339e5c7a05..9fb9758c96197 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -811,15 +811,20 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
         {ISD::CTPOP, MVT::v2i32, 3},
         {ISD::CTPOP, MVT::v4i16, 2},
         {ISD::CTPOP, MVT::v8i8,  1},
-        {ISD::CTPOP, MVT::i32,   5},
+        {ISD::CTPOP, MVT::i32,   4},
     };
+    EVT VT = TLI->getValueType(DL, RetTy, true);
+    // i8 ctpop can avoid the uaddlv after cnt since only one byte is active,
+    // making it cheaper than the legalized i32 cost.
+    if (VT == MVT::i8)
+      return 4;
+
     auto LT = getTypeLegalizationCost(RetTy);
     MVT MTy = LT.second;
     if (const auto *Entry = CostTableLookup(CtpopCostTbl, ISD::CTPOP, MTy)) {
-      // Extra cost of +1 when illegal vector types are legalized by promoting
+      // Extra cost of +1 when illegal vector/scalar types are legalized by promoting
       // the integer type.
-      int ExtraCost = MTy.isVector() && MTy.getScalarSizeInBits() !=
-                                            RetTy->getScalarSizeInBits()
+      int ExtraCost = MTy.getScalarSizeInBits() != RetTy->getScalarSizeInBits()
                           ? 1
                           : 0;
       return LT.first * Entry->Cost + ExtraCost;
diff --git a/llvm/test/Analysis/CostModel/AArch64/ctpop.ll b/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
index 013432991f5ae..1a48d9d37d03e 100644
--- a/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
+++ b/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
@@ -16,7 +16,7 @@ define i64 @test_ctpop_i64(i64 %a) {
 
 define i32 @test_ctpop_i32(i32 %a) {
 ; CHECK-LABEL: 'test_ctpop_i32'
-; CHECK-NEXT:  Cost Model: Found costs of 5 for: %ctpop = call i32 @llvm.ctpop.i32(i32 %a)
+; CHECK-NEXT:  Cost Model: Found costs of 4 for: %ctpop = call i32 @llvm.ctpop.i32(i32 %a)
 ; CHECK-NEXT:  Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret i32 %ctpop
 ;
   %ctpop = call i32 @llvm.ctpop.i32(i32 %a)
@@ -34,7 +34,7 @@ define i16 @test_ctpop_i16(i16 %a) {
 
 define i8 @test_ctpop_i8(i8 %a) {
 ; CHECK-LABEL: 'test_ctpop_i8'
-; CHECK-NEXT:  Cost Model: Found costs of 5 for: %ctpop = call i8 @llvm.ctpop.i8(i8 %a)
+; CHECK-NEXT:  Cost Model: Found costs of 4 for: %ctpop = call i8 @llvm.ctpop.i8(i8 %a)
 ; CHECK-NEXT:  Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret i8 %ctpop
 ;
   %ctpop = call i8 @llvm.ctpop.i8(i8 %a)

``````````

</details>


https://github.com/llvm/llvm-project/pull/191769


More information about the llvm-commits mailing list