[llvm] [InstCombine] Fold square into comparison with constant (reopened) (PR #197665)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 09:02:08 PDT 2026


================
@@ -2188,12 +2188,48 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
   Type *MulTy = Mul->getType();
   Value *X = Mul->getOperand(0);
 
-  // If there's no overflow:
-  // X * X == 0 --> X == 0
-  // X * X != 0 --> X != 0
-  if (Cmp.isEquality() && C.isZero() && X == Mul->getOperand(1) &&
-      (Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
-    return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
+  // If comparing a square with a constant, try simplifying to comparing square
+  // roots.
+  if (X == Mul->getOperand(1) && !Cmp.isSigned()) {
+    APInt R = C.sqrtFloor();
+    bool IsSqr = C == R * R;
+
+    // X * X eq/ne C
+    if (Cmp.isEquality() &&
+        (Mul->hasNoUnsignedWrap() || (Mul->hasNoSignedWrap() && C.isZero()))) {
+
+      // If constant is not a square, eq/ne is false/true respectively
+      if (!IsSqr)
+        return replaceInstUsesWith(
+            Cmp, ConstantInt::getBool(Cmp.getType(),
+                                      CmpInst::isFalseWhenEqual(Pred)));
----------------
dtcxzyw wrote:

```suggestion
                                      Pred == ICmpInst::ICMP_NE));
```


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


More information about the llvm-commits mailing list