[llvm] [CodeGenPrepare] Unfold slow ctpop when used in power-of-two test (PR #102731)
    Matt Arsenault via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Mon Apr 21 04:27:29 PDT 2025
    
    
  
================
@@ -1762,6 +1763,61 @@ bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp,
   return true;
 }
 
+// Decanonicalizes icmp+ctpop power-of-two test if ctpop is slow.
+bool CodeGenPrepare::unfoldPow2Test(CmpInst *Cmp) {
+  CmpPredicate Pred;
+  Value *X;
+  const APInt *C;
+
+  // (icmp (ctpop x), c)
+  if (!match(Cmp, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Value(X)),
+                         m_APIntAllowPoison(C))))
+    return false;
+
+  // This transformation increases the number of instructions, don't do it if
+  // ctpop is fast.
+  Type *OpTy = X->getType();
+  if (TLI->isCtpopFast(TLI->getValueType(*DL, OpTy)))
+    return false;
----------------
arsenm wrote:
Could sink this after checking the IR is the right shape 
https://github.com/llvm/llvm-project/pull/102731
    
    
More information about the llvm-commits
mailing list