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

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 6 11:57:27 PDT 2024


================
@@ -2111,6 +2111,32 @@ bool CodeGenPrepare::optimizeURem(Instruction *Rem) {
   return false;
 }
 
+/// 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) {
+  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));
+  if (auto Range = II->getRange()) {
----------------
goldsteinn wrote:

should we just use `isKnownNonZero` here and let that do the range check if its present. Will also cover some other cases.

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


More information about the llvm-commits mailing list