[llvm] [CodeGenPrepare] Convert `ctpop(X) ==/!= 1` into `ctpop(X) u</u> 2/1` (PR #111284)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 7 02:56:18 PDT 2024


================
@@ -2114,19 +2114,30 @@ bool CodeGenPrepare::optimizeURem(Instruction *Rem) {
 /// Some targets have better codegen for `ctpop(X) u< 2` than `ctpop(X) == 1`.
 /// This function converts `ctpop(X) ==/!= 1` into `ctpop(X) u</u> 2/1` if the
 /// result cannot be zero.
-static bool adjustIsPower2Test(CmpInst *Cmp, const DataLayout &DL) {
+static bool adjustIsPower2Test(CmpInst *Cmp, const TargetLowering &TLI,
+                               const TargetTransformInfo &TTI,
+                               const DataLayout &DL) {
   ICmpInst::Predicate Pred;
   if (!match(Cmp, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(), m_One())))
     return false;
   if (!ICmpInst::isEquality(Pred))
     return false;
   auto *II = cast<IntrinsicInst>(Cmp->getOperand(0));
+
+  // Check if it is profitable for the target
+  ICmpInst::Predicate NewPred =
+      Pred == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGT;
+  if (TLI.isCtpopFast(TLI.getValueType(DL, II->getType())) &&
----------------
arsenm wrote:

Doesn't depend on ctpop speed? This is only whether the predicate and/or constant is cheaper?

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


More information about the llvm-commits mailing list