[llvm] [InstCombine] Fold select pattern with sub and negation to abs intrinsic (PR #156246)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 10 09:46:25 PDT 2025
================
@@ -1153,6 +1153,45 @@ static Value *foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal,
return Builder.CreateBinaryIntrinsic(Intrinsic::abs, TI, Builder.getTrue());
}
+ // Match: (A > B) ? (A - B) : (0 - (A - B)) --> abs(A - B)
+ // Also handles commuted cases like (B < A), (A >= B), (B <= A) after
+ // normalization
+ if (Pred == CmpInst::ICMP_SGT &&
+ match(TI, m_NSWSub(m_Specific(A), m_Specific(B))) &&
+ match(FI, m_Neg(m_Specific(TI)))) {
+ return Builder.CreateBinaryIntrinsic(Intrinsic::abs, TI,
+ Builder.getFalse());
+ }
+
+ // Match: (A < B) ? (0 - (A - B)) : (A - B) --> abs(A - B)
+ // This handles cases like (A <= B) after normalization
+ if (Pred == CmpInst::ICMP_SLT &&
+ match(TI, m_Neg(m_NSWSub(m_Specific(A), m_Specific(B)))) &&
+ match(FI, m_NSWSub(m_Specific(A), m_Specific(B)))) {
----------------
dtcxzyw wrote:
```suggestion
match(FI, m_NSWSub(m_Specific(A), m_Specific(B))) &&
match(TI, m_Neg(m_Specific(FI))) {
```
https://github.com/llvm/llvm-project/pull/156246
More information about the llvm-commits
mailing list