[llvm] Missed optimization when `b - a` is known nonnegative inside `select` instruction (PR #187898)

via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 30 18:56:28 PDT 2026


================
@@ -664,10 +664,49 @@ static Value *foldSelectICmpMinMax(const ICmpInst *Cmp, Value *TVal,
                                    Value *FVal,
                                    InstCombiner::BuilderTy &Builder,
                                    const SimplifyQuery &SQ) {
-  const Value *CmpLHS = Cmp->getOperand(0);
-  const Value *CmpRHS = Cmp->getOperand(1);
+  Value *CmpLHS = Cmp->getOperand(0);
+  Value *CmpRHS = Cmp->getOperand(1);
   ICmpInst::Predicate Pred = Cmp->getPredicate();
 
+  if (match(TVal, m_Zero())) {
+    // (X < Y) ? 0 : (X - Y)
+    if (Pred == CmpInst::ICMP_SLT &&
----------------
user1342234 wrote:

Thanks, I believe this holds for all of them as well.

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


More information about the llvm-commits mailing list