[llvm] [InstCombine] Remove checks for -1 and 0 in saturated unsigned add (NFC) (PR #153047)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 11 09:22:18 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: AZero13 (AZero13)

<details>
<summary>Changes</summary>

>=u 0 is uneeded to check because this is always true (InstSimplify will deal with it) and >u -1 is always false, so they will never come up anyway.

---
Full diff: https://github.com/llvm/llvm-project/pull/153047.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+2-6) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index eb4332fbc0959..3a3b7f3063f95 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1054,21 +1054,17 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
                                          ConstantInt::get(Cmp0->getType(), *C));
   }
 
-  // Negative one does not work here because X u> -1 ? -1, X + -1 is not a
-  // saturated add.
   if (Pred == ICmpInst::ICMP_UGT &&
       match(FVal, m_Add(m_Specific(Cmp0), m_APIntAllowPoison(C))) &&
-      match(Cmp1, m_SpecificIntAllowPoison(~*C - 1)) && !C->isAllOnes()) {
+      match(Cmp1, m_SpecificIntAllowPoison(~*C - 1))) {
     // (X u> ~C - 1) ? -1 : (X + C) --> uadd.sat(X, C)
     return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
                                          ConstantInt::get(Cmp0->getType(), *C));
   }
 
-  // Zero does not work here because X u>= 0 ? -1 : X -> is always -1, which is
-  // not a saturated add.
   if (Pred == ICmpInst::ICMP_UGE &&
       match(FVal, m_Add(m_Specific(Cmp0), m_APIntAllowPoison(C))) &&
-      match(Cmp1, m_SpecificIntAllowPoison(-*C)) && !C->isZero()) {
+      match(Cmp1, m_SpecificIntAllowPoison(-*C))) {
     // (X u >= -C) ? -1 : (X + C) --> uadd.sat(X, C)
     return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
                                          ConstantInt::get(Cmp0->getType(), *C));

``````````

</details>


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


More information about the llvm-commits mailing list