Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine #79699

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

AtariDreams
Copy link
Contributor

@AtariDreams AtariDreams commented Jan 27, 2024

Add cos(fabs(x)) -> cos(x) and cos(copysign(x, y)) -> cos(x).

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 27, 2024

@llvm/pr-subscribers-llvm-transforms

Author: AtariDreams (AtariDreams)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/79699.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp (+2-1)
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;

@AtariDreams AtariDreams changed the title Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls [Transforms] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls Jan 27, 2024
Copy link

github-actions bot commented Jan 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@nikic nikic removed their request for review January 27, 2024 18:18
@AtariDreams AtariDreams changed the title [Transforms] Add cos(fabs(x)) -> cos(x) to SimplifyLibCalls [Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine Jan 31, 2024
match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
// cos(-x) --> cos(x)
// cos(fabs(x)) --> cos(x)
// cos(copysign(x, y)) --> cos(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be broken out into an optimizeEvenFunction helper method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that in another PR. I think this is good for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In InstCombineAndOrXor we have stripSignOnlyFPOps

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but could try to common with stripSignOnlyFPOps in a follow up

%r = call nnan reassoc float @cosf(float %copysign)
ret float %r
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test a vector intrinsic case?

match(Src, m_CopySign(m_Value(X), m_Value(Sign)))) {
// cos(-x) --> cos(x)
// cos(fabs(x)) --> cos(x)
// cos(copysign(x, y)) --> cos(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In InstCombineAndOrXor we have stripSignOnlyFPOps

@arsenm arsenm merged commit 3746294 into llvm:main Feb 5, 2024
4 checks passed
@AtariDreams AtariDreams deleted the fabs branch February 5, 2024 19:02
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…mbine (llvm#79699)

Add cos(fabs(x)) -> cos(x) and cos(copysign(x, y)) -> cos(x).
ichaer added a commit to ichaer/llvm-project-onesided_lower_bound that referenced this pull request Feb 12, 2024
* llvm/main: (328 commits)
  [Flang][OpenMP] Attempt to make map-types-and-sizes.f90 test more agnostic to other architectures
  [Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine (llvm#79699)
  [workflows] Close issues used for backports once the PR has been created (llvm#80394)
  [RISCV] Add support for RISC-V Pointer Masking (llvm#79929)
  [lldb] Cleanup regex in libcxx formatters (NFC) (llvm#80618)
  [lldb] Remove unused private TypeCategoryMap methods (NFC) (llvm#80602)
  [mlir][sparse] refine sparse assembler strategy (llvm#80521)
  [NFC] Fix typo (llvm#80703)
  Fix broken ARM processor features test (llvm#80717)
  [ValueTracking][NFC] Pass `SimplifyQuery` to `computeKnownFPClass` family (llvm#80657)
  [x86_64][windows][swift] do not use Swift async extended frame for wi… (llvm#80468)
  [X86] addConstantComments - add FP16 MOVSH asm comments support
  [X86] Regenerate some vector constant comments missed in recent patches to improve mask predicate handling in addConstantComments
  [clang][AMDGPU][CUDA] Handle __builtin_printf for device printf (llvm#68515)
  Add some clarification to email check message
  [GitHub][Workflows] Prevent multiple private email comments (temporarily) (llvm#80648)
  [workflows] Use /mnt as the build directory on Linux (llvm#80583)
  [Flang][OpenMP] Initial mapping of Fortran pointers and allocatables for target devices (llvm#71766)
  [AMDGPU] GlobalISel for f8 conversions (llvm#80503)
  [AMDGPU] Fixed byte_sel of v_cvt_f32_bf8/v_cvt_f32_fp8 (llvm#80502)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants