[llvm] Optimize count leading ones if promoted type (PR #99591)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 23 07:34:11 PDT 2024


================
@@ -2298,8 +2298,24 @@ static bool despeculateCountZeros(IntrinsicInst *CountZeros,
   if (match(CountZeros->getOperand(1), m_One()))
     return false;
 
-  // If it's cheap to speculate, there's nothing to do.
   Type *Ty = CountZeros->getType();
+  EVT VTy = TLI->getValueType(*DL, Ty);
+
+  // do not despeculate if we have (ctlz (xor op -1)) if the operand is
+  // promoted as legalisation would later transform to:
+  //
+  // (ctlz (lshift (xor (extend op) -1)
+  //               lshiftamount))
+  //
+  // Despeculation is not only useless but also not wanted with SelectionDAG
+  // as XOR and CTLZ would be in different basic blocks.
+  if ((TLI->getTypeAction(CountZeros->getContext(), VTy) ==
+           TargetLowering::TypePromoteInteger ||
+       TLI->getOperationAction(ISD::CTLZ, VTy) == TargetLowering::Promote) &&
+      match(CountZeros->getOperand(0), m_Not(m_Value())))
+    return false;
----------------
arsenm wrote:

Also move this below these other, simpler conditions? 

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


More information about the llvm-commits mailing list