[llvm] [InstCombine] Fold an unsigned comparison of `add nsw X, C` with a constant into a signed comparison (PR #103480)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 00:38:51 PDT 2024


================
@@ -3077,6 +3077,13 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
       return new ICmpInst(Pred, X, ConstantInt::get(Ty, NewC));
   }
 
+  ConstantRange LHS_CR = computeConstantRange(X, true).add(*C2);
+  if (ICmpInst::isUnsigned(Pred) && Add->hasNoSignedWrap() &&
+      C.isNonNegative() && LHS_CR.isAllNonNegative() &&
+      (C - *C2).isNonNegative())
----------------
dtcxzyw wrote:

```suggestion
      C.isNonNegative() &&
      (C - *C2).isNonNegative() && computeConstantRange(X, /*ForSigned=*/true).add(*C2).isAllNonNegative())
```

`computeConstantRange` is expensive.


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


More information about the llvm-commits mailing list