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

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 27 10:04:47 PST 2024


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

>From 048e314166a8bf06d51a26f6b801bf0ffd8e73a4 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] [Transforms] 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 | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 52eef9ab58a4d9..a1038c06daea4b 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1934,8 +1934,10 @@ static Value *optimizeTrigReflections(CallInst *Call, LibFunc Func,
   case LibFunc_cos:
   case LibFunc_cosf:
   case LibFunc_cosl:
-    // cos(-X) --> cos(X)
-    if (match(Call->getArgOperand(0), m_FNeg(m_Value(X))))
+    // cos(-x) --> cos(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