[llvm] [CodeGenPrepare] Unfold slow ctpop when used in power-of-two test (PR #102731)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 19 22:22:38 PDT 2025


================
@@ -1757,6 +1758,40 @@ bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp,
   return true;
 }
 
+// Decanonicalizes icmp+ctpop power-of-two test if ctpop is slow.
+bool CodeGenPrepare::unfoldPow2Test(CmpInst *Cmp) {
+  ICmpInst::Predicate Pred;
+  Value *X;
+  uint64_t C;
+
+  if (!match(Cmp, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
+                         m_ConstantInt(C))))
+    return false;
+
+  Type *Ty = X->getType();
+  if (Ty->isVectorTy() || TTI->getPopcntSupport(Ty->getIntegerBitWidth()) ==
+                              TargetTransformInfo::PSK_FastHardware)
----------------
s-barannikov wrote:

This now handles vectors as well.

> Comparing this with simplifySetCCWithCTPOP, it seems to always expand the scalar case

If I do this here for the "fast" case, it only results in more instructions.


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


More information about the llvm-commits mailing list