[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:13:00 PST 2024


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

>From 35af1a96f99eb6b2eb92ed0c45f46c3010097eeb 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/InstCombine/InstCombineCalls.cpp | 4 ++--
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp       | 6 ++++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index a647be2d26c7613..14260ebcf90f50d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2491,8 +2491,8 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
     Value *X;
     Value *Src = II->getArgOperand(0);
     if (match(Src, m_FNeg(m_Value(X))) || match(Src, m_FAbs(m_Value(X)))) {
-      // cos(-x) -> cos(x)
-      // cos(fabs(x)) -> cos(x)
+      // cos(-x) --> cos(x)
+      // cos(fabs(x)) --> cos(x)
       return replaceOperand(*II, 0, X);
     }
     break;
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 52eef9ab58a4d92..8f1def4105f04ee 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