[llvm] [InstCombine] Require 'contract' flag on operands for mul tan, cos to sin transformation (PR #180249)
Kshitij Paranjape via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 6 10:06:37 PST 2026
https://github.com/kshitijvp created https://github.com/llvm/llvm-project/pull/180249
When transforming mul tan, cos to sin the transformation should only happen when contract flag is applied on both tan and cos. Adding checks for the same.
>From 5919edf14c444fc54b562446917970cd31e33a22 Mon Sep 17 00:00:00 2001
From: Kshitij Paranjape <kshitijvparanjape at gmail.com>
Date: Fri, 6 Feb 2026 23:15:22 +0530
Subject: [PATCH] [InstCombine] Require 'contract' flag on operands for mul
tan, cos to sin transformation
When transforming mul tan, cos to sin the transformation
should only happen when contract flag is applied on
both tan and cos. Adding checks for the same.
---
.../InstCombine/InstCombineMulDivRem.cpp | 12 ++++++---
.../Transforms/InstCombine/issue179972.ll | 27 +++++++++++++++++++
2 files changed, 35 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/Transforms/InstCombine/issue179972.ll
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 8d053bd499fce..e91d72c5eb386 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -1097,11 +1097,15 @@ Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
match(&I,
m_c_FMul(m_OneUse(m_Intrinsic<Intrinsic::tan>(m_Value(X))),
m_OneUse(m_Intrinsic<Intrinsic::cos>(m_Deferred(X)))))) {
- auto *Sin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, &I);
- if (auto *Metadata = I.getMetadata(LLVMContext::MD_fpmath)) {
- Sin->setMetadata(LLVMContext::MD_fpmath, Metadata);
+ Instruction *I1 = dyn_cast<Instruction>(I.getOperand(0));
+ Instruction *I2 = dyn_cast<Instruction>(I.getOperand(1));
+ if (I1 && I2 && I1->hasAllowContract() && I2->hasAllowContract()) {
+ auto *Sin = Builder.CreateUnaryIntrinsic(Intrinsic::sin, X, &I);
+ if (auto *Metadata = I.getMetadata(LLVMContext::MD_fpmath)) {
+ Sin->setMetadata(LLVMContext::MD_fpmath, Metadata);
+ }
+ return replaceInstUsesWith(I, Sin);
}
- return replaceInstUsesWith(I, Sin);
}
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/issue179972.ll b/llvm/test/Transforms/InstCombine/issue179972.ll
new file mode 100644
index 0000000000000..fa0fb5fd87b90
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/issue179972.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+define float @missing_contract_cos_mul_tan(float %x) {
+; CHECK-LABEL: define float @missing_contract_cos_mul_tan(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[TAN:%.*]] = call float @llvm.tan.f32(float [[X]])
+; CHECK-NEXT: [[COS:%.*]] = call float @llvm.cos.f32(float [[X]])
+; CHECK-NEXT: [[MUL:%.*]] = fmul contract float [[TAN]], [[COS]]
+; CHECK-NEXT: ret float [[MUL]]
+;
+ %tan = call float @llvm.tan.f32(float %x)
+ %cos = call float @llvm.cos.f32(float %x)
+ %mul = fmul contract float %tan, %cos
+ ret float %mul
+}
+
+define float @cos_mul_tan(float %x) {
+; CHECK-LABEL: define float @cos_mul_tan(
+; CHECK-SAME: float [[X:%.*]]) {
+; CHECK-NEXT: [[MUL:%.*]] = call contract float @llvm.sin.f32(float [[X]])
+; CHECK-NEXT: ret float [[MUL]]
+;
+ %tan = call contract float @llvm.tan.f32(float %x)
+ %cos = call contract float @llvm.cos.f32(float %x)
+ %mul = fmul contract float %tan, %cos
+ ret float %mul
+}
More information about the llvm-commits
mailing list