[llvm] [InstCombine] Fold square into comparison with constant (PR #196480)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 8 09:25:50 PDT 2026
================
@@ -2195,6 +2195,14 @@ Instruction *InstCombinerImpl::foldICmpMulConstant(ICmpInst &Cmp,
(Mul->hasNoUnsignedWrap() || Mul->hasNoSignedWrap()))
return new ICmpInst(Pred, X, ConstantInt::getNullValue(MulTy));
+ // If unsigned compare or equality comparison of self multiply and square,
+ // compare square roots
+ if (!Cmp.isSigned() && X == Mul->getOperand(1) && Mul->hasNoUnsignedWrap()) {
+ APInt R = C.sqrt();
+ if (C == R * R)
----------------
eebop wrote:
This can be weakened for `lt`. For instance `x * x < 10` <-> `x < 4`. The exact comparison is only needed in the case of `eq`. (And even then, a further optimization is to note that `x * x == 10` can be reduced to `ret false`)
https://github.com/llvm/llvm-project/pull/196480
More information about the llvm-commits
mailing list