[llvm] [InstCombine] Ensure Safe Handling of Flags in foldFNegIntoConstant (PR #94148)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 30 06:34:27 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (SahilPatidar)
<details>
<summary>Changes</summary>
Fix #<!-- -->93769
alive2: https://alive2.llvm.org/ce/z/MHShQY
---
Full diff: https://github.com/llvm/llvm-project/pull/94148.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+7-2)
- (modified) llvm/test/Transforms/InstCombine/fneg.ll (+102)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index e5c3a20e1a6487..9e9c3fc7b25fba 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2757,8 +2757,13 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
// Fold negation into constant operand.
// -(X * C) --> X * (-C)
if (match(FNegOp, m_FMul(m_Value(X), m_Constant(C))))
- if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
- return BinaryOperator::CreateFMulFMF(X, NegC, &I);
+ if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
+ FastMathFlags FMF = I.getFastMathFlags();
+ FastMathFlags OpFMF = FNegOp->getFastMathFlags();
+ FastMathFlags Flag = FMF | OpFMF;
+ Flag.setNoInfs(FMF.noInfs() && OpFMF.noInfs());
+ return BinaryOperator::CreateFMulFMF(X, NegC, Flag);
+ }
// -(X / C) --> X / (-C)
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C))))
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL))
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index 3c4088832feaaa..4d68faf178d9cf 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -1109,4 +1109,106 @@ define float @test_fneg_select_maxnum(float %x) {
ret float %neg
}
+define float @test_fneg_ninf_mul_with_anyzero(float %a) {
+; CHECK-LABEL: @test_fneg_ninf_mul_with_anyzero(
+; CHECK-NEXT: [[F:%.*]] = fmul float [[A:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[F]]
+;
+ %mul = fmul float %a, 0.0
+ %f = fneg ninf float %mul
+ ret float %f
+}
+
+define float @test_fsub_ninf_mul_with_anyzero(float %a) {
+; CHECK-LABEL: @test_fsub_ninf_mul_with_anyzero(
+; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[F2]]
+;
+ %f1 = fmul nsz float %a, 0.000000
+ %f2 = fsub ninf float -0.000000, %f1
+ ret float %f2
+}
+
+define float @test_fneg_nnan_mul_with_anyzero(float %a) {
+; CHECK-LABEL: @test_fneg_nnan_mul_with_anyzero(
+; CHECK-NEXT: [[TMP1:%.*]] = fneg nnan float [[A:%.*]]
+; CHECK-NEXT: [[F2:%.*]] = call nnan float @llvm.copysign.f32(float 0.000000e+00, float [[TMP1]])
+; CHECK-NEXT: ret float [[F2]]
+;
+ %f1 = fmul ninf float %a, 0.000000
+ %f2 = fneg nnan float %f1
+ ret float %f2
+}
+
+define float @test_fneg_nsz_mul_with_anyzero(float %a) {
+; CHECK-LABEL: @test_fneg_nsz_mul_with_anyzero(
+; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[F2]]
+;
+ %f1 = fmul ninf float %a, 0.000000
+ %f2 = fneg nsz float %f1
+ ret float %f2
+}
+
+define float @test_fneg_ninf_mul_nnan_with_const(float %a) {
+; CHECK-LABEL: @test_fneg_ninf_mul_nnan_with_const(
+; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[A:%.*]]
+; CHECK-NEXT: [[F2:%.*]] = call float @llvm.copysign.f32(float 0.000000e+00, float [[TMP1]])
+; CHECK-NEXT: ret float [[F2]]
+;
+ %f1 = fmul nnan float %a, 0.000000
+ %f2 = fneg ninf float %f1
+ ret float %f2
+}
+
+define float @test_fneg_ninf_mul_nsz_with_const(float %a) {
+; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_const(
+; CHECK-NEXT: [[F2:%.*]] = fmul nsz float [[A:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[F2]]
+;
+ %f1 = fmul nsz float %a, 0.000000
+ %f2 = fneg ninf float %f1
+ ret float %f2
+}
+
+define <2 x float> @test_fneg_ninf_mul_nnan_with_vec_const(<2 x float> %a) {
+; CHECK-LABEL: @test_fneg_ninf_mul_nnan_with_vec_const(
+; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
+; CHECK-NEXT: ret <2 x float> [[F2]]
+;
+ %f1 = fmul nnan <2 x float> %a, <float 0.000000, float -0.000000>
+ %f2 = fneg ninf <2 x float> %f1
+ ret <2 x float> %f2
+}
+
+define <2 x float> @test_fneg_ninf_mul_nsz_with_vec_const(<2 x float> %a) {
+; CHECK-LABEL: @test_fneg_ninf_mul_nsz_with_vec_const(
+; CHECK-NEXT: [[F2:%.*]] = fmul nsz <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
+; CHECK-NEXT: ret <2 x float> [[F2]]
+;
+ %f1 = fmul nsz <2 x float> %a, <float 0.000000, float -0.000000>
+ %f2 = fneg ninf <2 x float> %f1
+ ret <2 x float> %f2
+}
+
+define <2 x float> @test_fneg_nnan_ninf_mul_with_vec_const(<2 x float> %a) {
+; CHECK-LABEL: @test_fneg_nnan_ninf_mul_with_vec_const(
+; CHECK-NEXT: [[F2:%.*]] = fmul nnan <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
+; CHECK-NEXT: ret <2 x float> [[F2]]
+;
+ %f1 = fmul <2 x float> %a, <float 0.000000, float -0.000000>
+ %f2 = fneg nnan ninf <2 x float> %f1
+ ret <2 x float> %f2
+}
+
+define <2 x float> @test_fneg_nnan_ninf_mul_ninf_with_vec_const(<2 x float> %a) {
+; CHECK-LABEL: @test_fneg_nnan_ninf_mul_ninf_with_vec_const(
+; CHECK-NEXT: [[F2:%.*]] = fmul nnan ninf <2 x float> [[A:%.*]], <float -0.000000e+00, float 0.000000e+00>
+; CHECK-NEXT: ret <2 x float> [[F2]]
+;
+ %f1 = fmul ninf <2 x float> %a, <float 0.000000, float -0.000000>
+ %f2 = fneg nnan ninf <2 x float> %f1
+ ret <2 x float> %f2
+}
+
!0 = !{}
``````````
</details>
https://github.com/llvm/llvm-project/pull/94148
More information about the llvm-commits
mailing list