[clang] [llvm] Match bitsin(typeof(x)) - popcnt(x) to s_bcnt0_i32 on AMDGPU (PR #164847)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 24 00:54:17 PDT 2025


================
@@ -1977,6 +1989,37 @@ Value *AMDGPUCodeGenPrepareImpl::applyFractPat(IRBuilder<> &Builder,
   return insertValues(Builder, FractArg->getType(), ResultVals);
 }
 
+bool AMDGPUCodeGenPrepareImpl::visitCtpop(IntrinsicInst &I) {
+  uint32_t BitWidth, DestinationWidth, IntrinsicWidth;
+  if (!I.hasOneUse() || !I.getType()->isIntegerTy() ||
+      !ST.hasBCNT(BitWidth = I.getType()->getIntegerBitWidth()))
----------------
jmmartinez wrote:

Please do not try to put everything into a one-liner.

There is no need to assign variables at the same time you're trying to pass an argument to the function. 
It makes the code shorter, but harder to read.

Doing one single thing at a time makes everything much more readable.


```suggestion
  uint32_t DestinationWidth, IntrinsicWidth;
  
  if(!I.hasOneUse())
    return false;
    
  IntegerType *IType = dyn_cast<IntegerType>(I.getType());
  if(!IType)
    return false;
    
  uint32_t BitWidth = IType->getIntegerBitWidth();
  if (!ST.hasBCNT(BitWidth))
    return false;
```

In the if below, I was really not expecting the assignments on each side of the comparison either:
```cpp
  if ((DestinationWidth = MustBeSub->getType()->getIntegerBitWidth()) !=
      (IntrinsicWidth = TransformedIns->getType()->getIntegerBitWidth()))
```


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


More information about the llvm-commits mailing list