[llvm] [InstCombine] Add fold for fabs(-x) -> fabs(x) (PR #95627)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 01:29:16 PDT 2024


================
@@ -2511,16 +2511,22 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
   }
   case Intrinsic::fabs: {
     Value *Cond, *TVal, *FVal;
-    if (match(II->getArgOperand(0),
-              m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
+    Value *Arg = II->getArgOperand(0);
+    Value *X;
+    // fabs (-X) --> fabs (X)
+    if (match(Arg, m_FNeg(m_Value(X)))) {
----------------
dtcxzyw wrote:

We can merge this fold with `fabs(copysign(x, y)) -> fabs(x)`.
```
```suggestion
    if (match(Arg, m_CombineOr(m_FNeg(m_Value(X)), m_CopySign(m_Value(X), m_Value(Unused))))) {
```

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


More information about the llvm-commits mailing list