[llvm] [DAG] Fold nested add(add(reduce(a), b), add(reduce(c), d)) (PR #115150)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 08:34:19 PDT 2025


================
@@ -17429,12 +17463,15 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
                            DAG.getConstantFP(4.0, DL, VT));
       }
     }
+  } // enable-unsafe-fp-math && AllowNewConst
 
+  if (((Options.UnsafeFPMath && Options.NoSignedZerosFPMath) ||
+       (Flags.hasAllowReassociation() && Flags.hasNoSignedZeros()))) {
----------------
david-arm wrote:

This used to be guarded by `AllowNewConst`, which is defined as

```
  // No FP constant should be created after legalization as Instruction
  // Selection pass has a hard time dealing with FP constants.
  bool AllowNewConst = (Level < AfterLegalizeDAG);
```

With your change we may now potentially create new constants after legalisation. In the new pattern you've added to reassociateReduction it's possible that `B` and `D` are constants and so `op(B, D)` produces a new constant. Is this change required for your optimisation to work? I'm also worried about the existing pattern in `reassociateReduction` which now changes behaviour after removing the `AllowNewConst` guard.

https://github.com/llvm/llvm-project/pull/115150


More information about the llvm-commits mailing list