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

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 09:58:04 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()) {
----------------
MaxGraey wrote:

With `+nsw`: 
```cpp
if (
  Cmp.isSigned() && 
  X == Mul->getOperand(1) && 
  Mul->hasNoUnsignedWrap() && 
  Mul->hasNoUnsignedWrap()
)
```
 We can do this as well. Proof: https://alive2.llvm.org/ce/z/gYLRjN

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


More information about the llvm-commits mailing list