[llvm] [InstCombine] Use samesign constraints in unsigned known-bits folds (PR #209097)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 09:48:50 PDT 2026


================
@@ -8925,8 +8925,18 @@ llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C) {
 
   // Check if the constant operand can be safely incremented/decremented
   // without overflowing/underflowing.
-  auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
-    return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
+  auto ConstantIsOk = [Pred, WillIncrement, IsSigned](ConstantInt *C) {
+    if (WillIncrement ? C->isMaxValue(IsSigned) : C->isMinValue(IsSigned))
+      return false;
+
+    if (!Pred.hasSameSign())
+      return true;
+
+    // Preserve samesign only if adjusting the constant does not change its
+    // sign bit, and therefore does not change the poison domain.
+    const APInt &Value = C->getValue();
+    APInt Adjusted = WillIncrement ? Value + 1 : Value - 1;
+    return Value.isNegative() == Adjusted.isNegative();
----------------
dtcxzyw wrote:

```suggestion
    if (!Pred.hasSameSign() && !IsSigned)
      return true;

    // Preserve samesign only if adjusting the constant does not change its
    // sign bit, and therefore does not change the poison domain.
    return WillIncrement ? !C->isMaxSignedValue() : !C->isMinSingedValue();
```


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


More information about the llvm-commits mailing list