[llvm] [InstCombine] Fix saturated add canonicalization (PR #97973)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 01:34:15 PDT 2024


================
@@ -996,6 +989,30 @@ static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
   if (!match(TVal, m_AllOnes()))
     return nullptr;
 
+  if ((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
+      match(FVal, m_Add(m_Specific(Cmp0), m_APInt(C))) &&
+      match(Cmp1, m_SpecificInt(~*C))) {
+    // (X u> ~C) ? -1 : (X + C) --> uadd.sat(X, C)
+    return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp0,
+                                         ConstantInt::get(Cmp0->getType(), *C));
+  }
+
+  if (Pred == ICmpInst::ICMP_UGE &&
+      match(FVal, m_Add(m_Specific(Cmp0), m_APInt(C))) &&
+      match(TVal, m_AllOnes()) && match(Cmp1, m_SpecificInt(-*C))) {
----------------
nikic wrote:

```suggestion
      match(Cmp1, m_SpecificInt(-*C))) {
```

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


More information about the llvm-commits mailing list