[llvm] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls (PR #79699)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 09:39:13 PST 2024


https://github.com/AtariDreams created https://github.com/llvm/llvm-project/pull/79699

We have this for InstCombine, but forgot to add it for SimplifyLibCalls

>From 7b739ce87120bf1373a04ef489fc14f0ab25f2bb Mon Sep 17 00:00:00 2001
From: Rose <83477269+AtariDreams at users.noreply.github.com>
Date: Sat, 27 Jan 2024 12:38:19 -0500
Subject: [PATCH] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls

We have this for InstCombine, but forgot to add it for SimplifyLibCalls
---
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 52eef9ab58a4d92..29f7e596f465f7d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1935,7 +1935,8 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
   case LibFunc_cosf:
   case LibFunc_cosl:
     // cos(-X) --> cos(X)
-    if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
+    // cos(fabs(x)) -> cos(x)
+    if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))) || match(Call->getArgOperand(0), m_FAbs(m_Value(X))))
       return copyFlags(*Call,
                        B.CreateCall(Call->getCalledFunction(), X, "cos"));
     break;



More information about the llvm-commits mailing list