← Back to Pull requests

[InstCombine] Drop wrong ninf for `(-X / Y) + Z` #228684

Open
suogesi opened this pull request with 229 lines diff across 2 files
View on GitHub
suogesi suogesi commented on Oct 03, 2026 09:04 UTC
Author

When folding (-X / Y) + Z into Z - X / Y, it is treated as a BinaryOperator so final ninf is same as the add instruction. This iswrong when add instruction has ninf but div instruction doesn't have ninf.

Take first operand as FMFSource so the optimized div instruction have correct ninf now.

Fixes #228521

Diff: 229 lines (+224 / -5) across 2 files Reviewer: harrisonGPU Review duration: 90607m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.44) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.44 · 229 lines diff across 2 files
0.44
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.90 · 5 substantive comment(s)
0.90
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.325
Real PR Conversation Timeline
17 items
github-actions[bot] github-actions[bot] commented on Oct 03, 2026 09:04 UTC
Bot

<!--LLVM NEW CONTRIBUTOR COMMENT-->

Hello @suogesi :wave:

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
The InstCombine Contributor Guide lays out a series of rules that contributions to InstCombine (and other middle-end components) should follow.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.

---

Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.

---

If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Oct 03, 2026 09:05 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: 伍浩宇 (suogesi)

<details>
<summary>Changes</summary>

When folding (-X / Y) + Z into Z - X / Y, it is treated as a BinaryOperator so final ninf is same as the add instruction. This iswrong when add instruction has ninf but div instruction doesn't have ninf.

Take first operand as FMFSource so the optimized div instruction have correct ninf now.

Fixes #<!-- -->228521

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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+3-1)

  • (modified) llvm/test/Transforms/InstCombine/fadd.ll (+53-5)
  • ``````

    diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    index 281e3b353f6dd..4622e3f02b87e 100644
    --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);
    +    auto *DivInst = cast<BinaryOperator>(Div);
    +    Value *XY = Builder.CreateFDivFMF(X, Y, DivInst);
         return BinaryOperator::CreateFSubFMF(Z, XY, &I);
       }
     
    diff --git a/llvm/test/Transforms/InstCombine/fadd.ll b/llvm/test/Transforms/InstCombine/fadd.ll
    index c3f5cc742dd28..cce857f83f73b 100644
    --- a/llvm/test/Transforms/InstCombine/fadd.ll
    +++ b/llvm/test/Transforms/InstCombine/fadd.ll
    @@ -45,6 +45,18 @@ define double @fdiv_fneg1(double %x, double %y, double %pz) {
       ret double %r
     }
     
    +define double @fdiv_fneg1_ninf(double %x, double %y, double %z) {
    +; CHECK-LABEL: @fdiv_fneg1_ninf(
    +; CHECK-NEXT:    [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
    +; CHECK-NEXT:    [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
    +; CHECK-NEXT:    ret double [[R]]
    +;
    +  %nx = fneg double %x
    +  %d  = fdiv double %nx, %y
    +  %r  = fadd ninf double %z, %d
    +  ret double %r
    +}
    +
     ; Z + (Y / -X) --> Z - (Y / X)
     
     define <2 x double> @fdiv_fneg2(<2 x double> %x, <2 x double> %y, <2 x double> %pz) {
    @@ -61,6 +73,18 @@ define <2 x double> @fdiv_fneg2(<2 x double> %x, <2 x double> %y, <2 x double> %
       ret <2 x double> %r
     }
     
    +define double @fdiv_fneg2_ninf(double %x, double %y, double %z) {
    +; CHECK-LABEL: @fdiv_fneg2_ninf(
    +; CHECK-NEXT:    [[TMP1:%.*]] = fdiv double [[Y:%.*]], [[X:%.*]]
    +; CHECK-NEXT:    [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
    +; CHECK-NEXT:    ret double [[R]]
    +;
    +  %nx = fneg double %x
    +  %d  = fdiv double %y, %nx
    +  %r  = fadd ninf double %z, %d
    +  ret double %r
    +}
    +
     ; Z + (-X * Y) --> Z - (X * Y)
     
     define double @fmul_fneg1(double %x, double %y, double %pz) {
    @@ -111,6 +135,18 @@ define double @fdiv_fneg1_commute(double %x, double %y, double %pz) {
       ret double %r
     }
     
    +define double @fdiv_fneg1_commute_ninf(double %x, double %y, double %z) {
    +; CHECK-LABEL: @fdiv_fneg1_commute_ninf(
    +; CHECK-NEXT:    [[TMP1:%.*]] = fdiv double [[X:%.*]], [[Y:%.*]]
    +; CHECK-NEXT:    [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
    +; CHECK-NEXT:    ret double [[R]]
    +;
    +  %nx = fneg double %x
    +  %d  = fdiv double %nx, %y
    +  %r  = fadd ninf double %d, %z
    +  ret double %r
    +}
    +
     ; (Y / -X) + Z --> Z - (Y / X)
     
     define <2 x double> @fdiv_fneg2_commute(<2 x double> %x, <2 x double> %y, <2 x double> %pz) {
    @@ -127,6 +163,18 @@ define <2 x double> @fdiv_fneg2_commute(<2 x double> %x, <2 x double> %y, <2 x d
       ret <2 x double> %r
     }
     
    +define double @fdiv_fneg2_commute_ninf(double %x, double %y, double %z) {
    +; CHECK-LABEL: @fdiv_fneg2_commute_ninf(
    +; CHECK-NEXT:    [[TMP1:%.*]] = fdiv double [[Y:%.*]], [[X:%.*]]
    +; CHECK-NEXT:    [[R:%.*]] = fsub ninf double [[Z:%.*]], [[TMP1]]
    +; CHECK-NEXT:    ret double [[R]]
    +;
    +  %nx = fneg double %x
    +  %d  = fdiv double %y, %nx
    +  %r  = fadd ninf double %d, %z
    +  ret double %r
    +}
    +
     ; (-X * Y) + Z --> Z - (X * Y)
     
     define double @fmul_fneg1_commute(double %x, double %y, double %pz) {
    @@ -829,11 +877,11 @@ define float @fadd_reduce_sqr_sum_varA_not_one_use1(float %a, float %b) {
     
     define float @fadd_reduce_sqr_sum_varA_not_one_use2(float %a, float %b) {
     ; CHECK-LABEL: @fadd_reduce_sqr_sum_varA_not_one_use2(
    -; CHECK-NEXT:    [[A_SQ:%.*]] = fmul float [[A:%.*]], [[A:%.*]]
    -; CHECK-NEXT:    [[AB:%.*]] = fadd reassoc nsz float [[A:%.*]], [[B:%.*]]
    -; CHECK-NEXT:    [[AB_SQ:%.*]] = fmul reassoc nsz float [[AB:%.*]], [[AB:%.*]]
    -; CHECK-NEXT:    tail call void @fake_func(float [[A_SQ:%.*]])
    -; CHECK-NEXT:    ret float [[AB_SQ:%.*]]
    +; CHECK-NEXT:    [[A_SQ:%.*]] = fmul float [[A:%.*]], [[A]]
    +; CHECK-NEXT:    [[AB:%.*]] = fadd reassoc nsz float [[A]], [[B:%.*]]
    +; CHECK-NEXT:    [[AB_SQ:%.*]] = fmul reassoc nsz float [[AB]], [[AB]]
    +; CHECK-NEXT:    tail call void @fake_func(float [[A_SQ]])
    +; CHECK-NEXT:    ret float [[AB_SQ]]
     ;
       %a_sq = fmul float %a, %a
       %two_a = fmul float %a, 2.0
    
    
    ``````

    </details>

    suogesi suogesi commented on Oct 03, 2026 09:27 UTC
    Author

    Maybe CHECK lines in fadd.ll needs to be regenerated.

    harrisonGPU harrisonGPU commented on code on Oct 03, 2026 15:04 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    Could you add a negative test?

    suogesi suogesi commented on code on Oct 03, 2026 16:02 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    There are 4 combinations about ninf:

    1. div with ninf, add with ninf.
    2. div without ninf, add with ninf.
    3. div with ninf, add without ninf.
    4. div without ninf, add without ninf.

    This PR is trying to fix 2. For this pass, I want to know which of them should be called "positive test" and which of them should be called "negative test"?

    harrisonGPU harrisonGPU commented on code on Oct 03, 2026 16:21 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    Case 1 is the positive test: the new fdiv keeps ninf. Case 2 is the negative test: the fadd's ninf must not be propagated to the new fdiv.

    suogesi suogesi commented on code on Oct 03, 2026 19:09 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    Case 1 is added with name fdiv_fneg_ninf and Case 2 is renamed to fdiv_fneg_ninf_does_not_propagate_to_div now.

    suogesi suogesi commented on code on Oct 03, 2026 19:12 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    It turns out that this fix is bad because match expression can swap operators so the first operator is not always fdiv instruction. I'm using dyn_cast to check LHS and RHS now.

    github-actions[bot] github-actions[bot] commented on Oct 04, 2026 02:51 UTC
    Bot

    <!--LLVM CODE FORMAT COMMENT: {clang-format}-->

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

    suogesi suogesi commented on code on Oct 08, 2026 04:02 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2167,9 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *Div = I.getOperand(0);

    ping @harrisonGPU

    HelloWorldU HelloWorldU commented on code on Oct 08, 2026 09:37 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2166,15 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *LHS = I.getOperand(0);
    +    Value *RHS = I.getOperand(1);
    +    BinaryOperator *DivInst;
    +    if (auto *Inst = dyn_cast<BinaryOperator>(LHS)) {
    +      DivInst = Inst;
    +    } else {

    What if Z is also a BinaryOperator on the LHS? Maybe bind the fdiv directly in the matcher instead.

    HelloWorldU HelloWorldU commented on code on Oct 08, 2026 09:39 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1

    The AI also found (-X * Y) + Z fold above copies the fadd flags onto the fmul in the same way, which looks wrong too.

    suogesi suogesi commented on code on Oct 08, 2026 10:58 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1

    I think there should be another PR for this.

    suogesi suogesi commented on code on Oct 08, 2026 12:23 UTC
    Code Review
    llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp:1
    @@ -2167,7 +2166,15 @@ Instruction *InstCombinerImpl::visitFAdd(BinaryOperator &I) {
                              m_Value(Z))) ||
           match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))),
                              m_Value(Z)))) {
    -    Value *XY = Builder.CreateFDivFMF(X, Y, &I);
    +    Value *LHS = I.getOperand(0);
    +    Value *RHS = I.getOperand(1);
    +    BinaryOperator *DivInst;
    +    if (auto *Inst = dyn_cast<BinaryOperator>(LHS)) {
    +      DivInst = Inst;
    +    } else {

    That makes sense. Now it is driven by match. Also added more tests for situations that two ops are fdiv.

    suogesi suogesi commented on Oct 20, 2026 07:04 UTC
    Author

    ping

    suogesi suogesi commented on Nov 11, 2026 01:01 UTC
    Author

    ping

    Reviewers
    Assignees
    suogesi suogesi
    Labels
    risk: low confidence: 89.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.0% (MEDIUM) · Higher = larger blast radius
    44.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.1% · Lower = rushed or shallow review
    89.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.8% · Safe to merge
    4.8%