[llvm] [InstCombine] Fold icmp in select to smin (PR #196823)

Krishna Narayanan via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 10:07:24 PDT 2026


================
@@ -2100,6 +2100,45 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
   return nullptr;
 }
 
+// a > -1 ? 1 : ( a | (+ve)value) --> smin(1, a | (+ve)value)
+// a < -1 ? ( a | (+ve)value) : 1 --> smin(1, a | (+ve)value)
+// a < 0  ? ( a | (+ve)value) : 1 --> smin(1, a | (+ve)value)
+static Value *foldSelectInstWithICmpOr(SelectInst &SI, ICmpInst *ICI,
+                                       InstCombiner::BuilderTy &Builder,
+                                       const SimplifyQuery &SQ) {
+  Value *OrVal, *ConstVal, *Mask;
+  Value *A = ICI->getOperand(0);
+  Value *B = ICI->getOperand(1);
+  Value *TVal = SI.getTrueValue();
+  Value *FVal = SI.getFalseValue();
+  ICmpInst::Predicate Pred = ICI->getPredicate();
+
+  if ((Pred == ICmpInst::ICMP_SLT && match(B, m_Zero())) ||
+      (Pred == ICmpInst::ICMP_SLE && match(B, m_AllOnes()))) {
+    OrVal = TVal;
+    ConstVal = FVal;
+  } else if ((Pred == ICmpInst::ICMP_SGT &&
+              match(B, m_AllOnes())) ||
+             (Pred == ICmpInst::ICMP_SGE && match(B, m_Zero()))) {
+    OrVal = FVal;
+    ConstVal = TVal;
+  } else {
+    return nullptr;
+  }
+
+  if (!match(ConstVal, m_AllOnes()) && !match(ConstVal, m_Zero()) &&
+      !match(ConstVal, m_One()))
+    return nullptr;
+
+  if (!match(OrVal, m_c_Or(m_Specific(A), m_Value(Mask))))
+    return nullptr;
+
+  if (!isKnownPositive(Mask, SQ))
----------------
Krishna-13-cyber wrote:

Hi @dtcxzyw , gentle ping!

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


More information about the llvm-commits mailing list