[llvm-branch-commits] [llvm] [DAGCombiner][GlobalISel] Extend allMulUsesCanBeContracted with FPEXT pattern (PR #188116)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Apr 3 08:57:16 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-selectiondag

Author: Adel Ejjeh (adelejjeh)

<details>
<summary>Changes</summary>

Supersedes #<!-- -->169735 (PR 4/5). Split into a stack of 5 PRs per reviewer request.

---

Extend `allMulUsesCanBeContracted()` to recognize FPEXT patterns where the multiply result flows through `fpext` before being used in contractable operations (`fadd`, `fsub`). This covers three sub-patterns:

- `fmul -> fpext -> {fadd, fsub}`: FPEXT folds if `isFPExtFoldable`
- `fmul -> fpext -> fneg -> fsub`: FPEXT then FNEG to FSUB
- `fmul -> fneg -> fpext -> fsub`: FNEG then FPEXT folds if foldable

Changes:

- **DAGCombiner.cpp**: Add `ISD::FP_EXTEND` case to `allMulUsesCanBeContracted()` covering the three FPEXT sub-patterns above. Add `allMulUsesCanBeContracted` guards to all FPEXT fold sites in `visitFADDForFMACombine` and `visitFSUBForFMACombine`.
- **CombinerHelper.cpp**: Add `G_FPEXT` case to `allMulUsesCanBeContracted()` with equivalent logic. Add guards to all FPEXT fold sites in `matchCombineFAddFpExtFMulToFMadOrFMA`, `matchCombineFSubFpExtFMulToFMadOrFMA`, and `matchCombineFSubFpExtFNegFMulToFMadOrFMA`.

### Test Changes

#### `fma-multiple-uses-contraction.ll`

The FPEXT patterns section is activated: `P0-` prefixed baseline checks are replaced with real-prefix CHECK lines reflecting the guard changes from this PR. The remaining FMA/FMAD chain section retains `P0-` prefixed baseline checks, activated by the final PR.

---

_This PR was split from the original_ #<!-- -->169735 _with the assistance of_ [_Claude Code_](https://claude.ai/code)_._

---

Patch is 146.99 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/188116.diff


4 Files Affected:

- (modified) llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h (+2-1) 
- (modified) llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp (+88-14) 
- (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+78-13) 
- (modified) llvm/test/CodeGen/AMDGPU/fma-multiple-uses-contraction.ll (+984-1201) 


``````````diff
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
index 09c827f71a34d..8440fdcbbd08b 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
@@ -805,7 +805,8 @@ class CombinerHelper {
 
   /// Check if all uses of a multiply can be contracted into fma/fmad
   /// operations, so that duplicating the multiply is acceptable.
-  bool allMulUsesCanBeContracted(const MachineInstr &MI) const;
+  bool allMulUsesCanBeContracted(const MachineInstr &MI,
+                                 unsigned PreferredFusedOpcode) const;
 
   bool canCombineFMadOrFMA(MachineInstr &MI, bool &AllowFusionGlobally,
                            bool &HasFMAD, bool &Aggressive,
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index 2e9355013e84d..75ead4c775c6c 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -6316,10 +6316,15 @@ static bool hasMoreUses(const MachineInstr &MI0, const MachineInstr &MI1,
 /// would duplicate the multiply without reducing the total number of
 /// operations.
 ///
-/// Currently checks for the following patterns:
+/// This uses a simple, non-recursive check for the following patterns:
 ///   - fmul --> fadd/fsub: Direct contraction
 ///   - fmul --> fneg --> fsub: Contraction through fneg
-bool CombinerHelper::allMulUsesCanBeContracted(const MachineInstr &MI) const {
+///   - fmul --> fneg --> fpext --> fsub: FNEG then FPEXT folds if foldable
+///   - fmul --> fpext --> {fadd, fsub}: FPEXT folds if foldable
+///   - fmul --> fpext --> fneg --> fsub: FPEXT then FNEG to FSUB
+bool CombinerHelper::allMulUsesCanBeContracted(
+    const MachineInstr &MI, unsigned PreferredFusedOpcode) const {
+  const auto &TLI = getTargetLowering();
   Register MulReg = MI.getOperand(0).getReg();
 
   for (const MachineInstr &UseMI : MRI.use_nodbg_instructions(MulReg)) {
@@ -6329,13 +6334,66 @@ bool CombinerHelper::allMulUsesCanBeContracted(const MachineInstr &MI) const {
     if (Opcode == TargetOpcode::G_FADD || Opcode == TargetOpcode::G_FSUB)
       continue;
 
-    // G_FNEG use - contractable if all users of the fneg are G_FSUB.
+    // FNEG --> FSUB pattern
+    // Also handles FNEG --> FPEXT --> FSUB
     if (Opcode == TargetOpcode::G_FNEG) {
       Register FNegReg = UseMI.getOperand(0).getReg();
-      for (const MachineInstr &FNegUser : MRI.use_nodbg_instructions(FNegReg)) {
-        unsigned FNegUserOp = FNegUser.getOpcode();
-        if (FNegUserOp != TargetOpcode::G_FSUB)
+      // ALL users of the FNEG must be contractable FSUBs or FPEXTs leading to
+      // FSUBs
+      for (const MachineInstr &FNegUseMI :
+           MRI.use_nodbg_instructions(FNegReg)) {
+        unsigned FNegUseOpcode = FNegUseMI.getOpcode();
+
+        if (FNegUseOpcode == TargetOpcode::G_FSUB)
+          continue;
+        if (FNegUseOpcode == TargetOpcode::G_FPEXT) {
+          // FNEG --> FPEXT --> FSUB
+          Register FNegFPExtReg = FNegUseMI.getOperand(0).getReg();
+          for (const MachineInstr &FNegFPExtUseMI :
+               MRI.use_nodbg_instructions(FNegFPExtReg)) {
+            if (FNegFPExtUseMI.getOpcode() != TargetOpcode::G_FSUB)
+              return false;
+            // FPEXT use is FSUB, check if can be folded in
+            if (!TLI.isFPExtFoldable(
+                    FNegFPExtUseMI, PreferredFusedOpcode,
+                    MRI.getType(FNegFPExtUseMI.getOperand(0).getReg()),
+                    MRI.getType(FNegReg)))
+              return false;
+          }
+          continue;
+        }
+        return false;
+      }
+      continue;
+    }
+
+    // FP_EXTEND - check if ALL users are FADD, FSUB, or FNEG --> FSUB
+    if (Opcode == TargetOpcode::G_FPEXT) {
+      Register FPExtReg = UseMI.getOperand(0).getReg();
+
+      // ALL users of the FP_EXTEND must be contractable operations or FNEGs
+      for (const MachineInstr &FPExtUseMI :
+           MRI.use_nodbg_instructions(FPExtReg)) {
+        if (!TLI.isFPExtFoldable(FPExtUseMI, PreferredFusedOpcode,
+                                 MRI.getType(FPExtUseMI.getOperand(0).getReg()),
+                                 MRI.getType(MulReg)))
           return false;
+        unsigned ExtUseOpcode = FPExtUseMI.getOpcode();
+        if (ExtUseOpcode == TargetOpcode::G_FADD ||
+            ExtUseOpcode == TargetOpcode::G_FSUB) {
+          continue;
+        }
+        if (ExtUseOpcode == TargetOpcode::G_FNEG) {
+          // FP_EXTEND --> FNEG --> FSUB
+          Register FPExtFNegReg = FPExtUseMI.getOperand(0).getReg();
+          for (const MachineInstr &FPExtFNegUseMI :
+               MRI.use_nodbg_instructions(FPExtFNegReg)) {
+            if (FPExtFNegUseMI.getOpcode() != TargetOpcode::G_FSUB)
+              return false;
+          }
+          continue;
+        }
+        return false;
       }
       continue;
     }
@@ -6405,7 +6463,8 @@ bool CombinerHelper::matchCombineFAddFMulToFMadOrFMA(
   // fold (fadd (fmul x, y), z) -> (fma x, y, z)
   if (isContractableFMul(*LHS.MI, AllowFusionGlobally) &&
       (MRI.hasOneNonDBGUse(LHS.Reg) ||
-       (Aggressive && allMulUsesCanBeContracted(*LHS.MI)))) {
+       (Aggressive &&
+        allMulUsesCanBeContracted(*LHS.MI, PreferredFusedOpcode)))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       B.buildInstr(PreferredFusedOpcode, {MI.getOperand(0).getReg()},
                    {LHS.MI->getOperand(1).getReg(),
@@ -6417,7 +6476,8 @@ bool CombinerHelper::matchCombineFAddFMulToFMadOrFMA(
   // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
   if (isContractableFMul(*RHS.MI, AllowFusionGlobally) &&
       (MRI.hasOneNonDBGUse(RHS.Reg) ||
-       (Aggressive && allMulUsesCanBeContracted(*RHS.MI)))) {
+       (Aggressive &&
+        allMulUsesCanBeContracted(*RHS.MI, PreferredFusedOpcode)))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       B.buildInstr(PreferredFusedOpcode, {MI.getOperand(0).getReg()},
                    {RHS.MI->getOperand(1).getReg(),
@@ -6460,6 +6520,7 @@ bool CombinerHelper::matchCombineFAddFpExtFMulToFMadOrFMA(
   MachineInstr *FpExtSrc;
   if (mi_match(LHS.Reg, MRI, m_GFPExt(m_MInstr(FpExtSrc))) &&
       isContractableFMul(*FpExtSrc, AllowFusionGlobally) &&
+      allMulUsesCanBeContracted(*FpExtSrc, PreferredFusedOpcode) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstType,
                           MRI.getType(FpExtSrc->getOperand(1).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
@@ -6475,6 +6536,7 @@ bool CombinerHelper::matchCombineFAddFpExtFMulToFMadOrFMA(
   // Note: Commutes FADD operands.
   if (mi_match(RHS.Reg, MRI, m_GFPExt(m_MInstr(FpExtSrc))) &&
       isContractableFMul(*FpExtSrc, AllowFusionGlobally) &&
+      allMulUsesCanBeContracted(*FpExtSrc, PreferredFusedOpcode) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstType,
                           MRI.getType(FpExtSrc->getOperand(1).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
@@ -6712,7 +6774,8 @@ bool CombinerHelper::matchCombineFSubFMulToFMadOrFMA(
   if (FirstMulHasFewerUses &&
       (isContractableFMul(*LHS.MI, AllowFusionGlobally) &&
        (MRI.hasOneNonDBGUse(LHS.Reg) ||
-        (Aggressive && allMulUsesCanBeContracted(*LHS.MI))))) {
+        (Aggressive &&
+         allMulUsesCanBeContracted(*LHS.MI, PreferredFusedOpcode))))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       Register NegZ = B.buildFNeg(DstTy, RHS.Reg).getReg(0);
       B.buildInstr(PreferredFusedOpcode, {MI.getOperand(0).getReg()},
@@ -6724,7 +6787,8 @@ bool CombinerHelper::matchCombineFSubFMulToFMadOrFMA(
   // fold (fsub x, (fmul y, z)) -> (fma -y, z, x)
   if (isContractableFMul(*RHS.MI, AllowFusionGlobally) &&
       (MRI.hasOneNonDBGUse(RHS.Reg) ||
-       (Aggressive && allMulUsesCanBeContracted(*RHS.MI)))) {
+       (Aggressive &&
+        allMulUsesCanBeContracted(*RHS.MI, PreferredFusedOpcode)))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       Register NegY =
           B.buildFNeg(DstTy, RHS.MI->getOperand(1).getReg()).getReg(0);
@@ -6759,7 +6823,8 @@ bool CombinerHelper::matchCombineFSubFNegFMulToFMadOrFMA(
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
       ((MRI.hasOneNonDBGUse(LHSReg) &&
         MRI.hasOneNonDBGUse(FMulMI->getOperand(0).getReg())) ||
-       (Aggressive && allMulUsesCanBeContracted(*FMulMI)))) {
+       (Aggressive &&
+        allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode)))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       Register NegX =
           B.buildFNeg(DstTy, FMulMI->getOperand(1).getReg()).getReg(0);
@@ -6779,7 +6844,8 @@ bool CombinerHelper::matchCombineFSubFNegFMulToFMadOrFMA(
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
       ((MRI.hasOneNonDBGUse(RHSReg) &&
         MRI.hasOneNonDBGUse(FMulMI->getOperand(0).getReg())) ||
-       (Aggressive && allMulUsesCanBeContracted(*FMulMI)))) {
+       (Aggressive &&
+        allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode)))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
       B.buildInstr(PreferredFusedOpcode, {MI.getOperand(0).getReg()},
                    {FMulMI->getOperand(1).getReg(),
@@ -6812,7 +6878,10 @@ bool CombinerHelper::matchCombineFSubFpExtFMulToFMadOrFMA(
   // fold (fsub (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), (fneg z))
   if (mi_match(LHSReg, MRI, m_GFPExt(m_MInstr(FMulMI))) &&
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
-      (Aggressive || MRI.hasOneNonDBGUse(LHSReg)) &&
+      ((MRI.hasOneNonDBGUse(LHSReg) &&
+        MRI.hasOneNonDBGUse(FMulMI->getOperand(0).getReg())) ||
+       (Aggressive &&
+        allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode))) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstTy,
                           MRI.getType(FMulMI->getOperand(0).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
@@ -6830,7 +6899,10 @@ bool CombinerHelper::matchCombineFSubFpExtFMulToFMadOrFMA(
   // fold (fsub x, (fpext (fmul y, z))) -> (fma (fneg (fpext y)), (fpext z), x)
   if (mi_match(RHSReg, MRI, m_GFPExt(m_MInstr(FMulMI))) &&
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
-      (Aggressive || MRI.hasOneNonDBGUse(RHSReg)) &&
+      ((MRI.hasOneNonDBGUse(RHSReg) &&
+        MRI.hasOneNonDBGUse(FMulMI->getOperand(0).getReg())) ||
+       (Aggressive &&
+        allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode))) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstTy,
                           MRI.getType(FMulMI->getOperand(0).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
@@ -6880,6 +6952,7 @@ bool CombinerHelper::matchCombineFSubFpExtFNegFMulToFMadOrFMA(
   if ((mi_match(LHSReg, MRI, m_GFPExt(m_GFNeg(m_MInstr(FMulMI)))) ||
        mi_match(LHSReg, MRI, m_GFNeg(m_GFPExt(m_MInstr(FMulMI))))) &&
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
+      allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstTy,
                           MRI.getType(FMulMI->getOperand(0).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
@@ -6896,6 +6969,7 @@ bool CombinerHelper::matchCombineFSubFpExtFNegFMulToFMadOrFMA(
   if ((mi_match(RHSReg, MRI, m_GFPExt(m_GFNeg(m_MInstr(FMulMI)))) ||
        mi_match(RHSReg, MRI, m_GFNeg(m_GFPExt(m_MInstr(FMulMI))))) &&
       isContractableFMul(*FMulMI, AllowFusionGlobally) &&
+      allMulUsesCanBeContracted(*FMulMI, PreferredFusedOpcode) &&
       TLI.isFPExtFoldable(MI, PreferredFusedOpcode, DstTy,
                           MRI.getType(FMulMI->getOperand(0).getReg()))) {
     MatchInfo = [=, &MI](MachineIRBuilder &B) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 3d34a1226df27..6593ff2f3f52b 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17685,25 +17685,75 @@ static bool isFusedOp(const MatchContextClass &Matcher, SDValue N) {
 /// would duplicate the multiply without reducing the total number of
 /// operations.
 ///
-/// Currently checks for the following patterns:
+/// This uses a simple, non-recursive check for the following patterns:
 ///   - fmul --> fadd/fsub: Direct contraction
 ///   - fmul --> fneg --> fsub: Contraction through fneg
-static bool allMulUsesCanBeContracted(SDValue Mul) {
+///   - fmul --> fneg --> fpext --> fsub: FNEG then FPEXT folds if foldable
+///   - fmul --> fpext --> {fadd, fsub}: FPEXT folds if foldable
+///   - fmul --> fpext --> fneg --> fsub: FPEXT then FNEG to FSUB
+static bool allMulUsesCanBeContracted(SDValue Mul,
+                                      const unsigned PreferredFusedOpcode,
+                                      const TargetLowering &TLI,
+                                      SelectionDAG &DAG) {
   for (const auto *User : Mul->users()) {
-    unsigned Opcode = User->getOpcode();
+    SDNode *UserNode = const_cast<SDNode *>(User);
+    unsigned Opcode = UserNode->getOpcode();
 
     // Direct FADD/FSUB - contractable.
     if (Opcode == ISD::FADD || Opcode == ISD::FSUB)
       continue;
 
-    // FNEG use - contractable if all users of the fneg are FSUB.
+    // FNEG - check if ALL users are FSUB or foldable FPEXT --> FSUB
     if (Opcode == ISD::FNEG) {
-      for (const auto *FNegUser : User->users()) {
+      for (const auto *FNegUser : UserNode->users()) {
         unsigned FNegUserOp = FNegUser->getOpcode();
-        if (FNegUserOp != ISD::FSUB)
-          return false;
+
+        if (FNegUserOp == ISD::FSUB) {
+          // FNEG --> FSUB
+          continue;
+        }
+        if (FNegUserOp == ISD::FP_EXTEND) {
+          // FNEG --> FPEXT --> FSUB
+          EVT SrcVT = UserNode->getValueType(0); // Src of FPEXT is the FNEG
+          for (const auto *FNegFPExtUser : FNegUser->users()) {
+            if (FNegFPExtUser->getOpcode() != ISD::FSUB)
+              return false;
+            if (!TLI.isFPExtFoldable(DAG, PreferredFusedOpcode,
+                                     FNegFPExtUser->getValueType(0), SrcVT))
+              return false;
+          }
+          continue;
+        }
+        return false;
       }
-      continue;
+      continue; // All FNEG uses are contractable
+    }
+
+    // FP_EXTEND - check if ALL users are FADD, FSUB, or FNEG --> FSUB
+    if (Opcode == ISD::FP_EXTEND) {
+      EVT SrcVT = Mul.getValueType();
+
+      for (const auto *FPExtUser : UserNode->users()) {
+        unsigned ExtUserOp = FPExtUser->getOpcode();
+        EVT DstVT = FPExtUser->getValueType(0);
+        if (!TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, DstVT, SrcVT))
+          return false; // this FPEXT cannot be folded
+
+        if (ExtUserOp == ISD::FADD || ExtUserOp == ISD::FSUB) {
+          continue; // FPEXT --> {FADD, FSUB} is contractable
+        }
+        if (ExtUserOp == ISD::FNEG) {
+          // FP_EXTEND --> FNEG --> FSUB
+          for (const auto *FPExtFNegUser : FPExtUser->users()) {
+            if (FPExtFNegUser->getOpcode() != ISD::FSUB) {
+              return false;
+            }
+          }
+          continue;
+        }
+        return false;
+      }
+      continue; // All FPEXT uses are contractable
     }
 
     // Any other use type is not currently recognized as contractable.
@@ -17775,7 +17825,9 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
 
   // fold (fadd (fmul x, y), z) -> (fma x, y, z)
   if (isContractableFMUL(N0) &&
-      (N0->hasOneUse() || (Aggressive && allMulUsesCanBeContracted(N0)))) {
+      (N0->hasOneUse() ||
+       (Aggressive &&
+        allMulUsesCanBeContracted(N0, PreferredFusedOpcode, TLI, DAG)))) {
     return matcher.getNode(PreferredFusedOpcode, SL, VT, N0.getOperand(0),
                            N0.getOperand(1), N1);
   }
@@ -17783,7 +17835,9 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
   // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
   // Note: Commutes FADD operands.
   if (isContractableFMUL(N1) &&
-      (N1->hasOneUse() || (Aggressive && allMulUsesCanBeContracted(N1)))) {
+      (N1->hasOneUse() ||
+       (Aggressive &&
+        allMulUsesCanBeContracted(N1, PreferredFusedOpcode, TLI, DAG)))) {
     return matcher.getNode(PreferredFusedOpcode, SL, VT, N1.getOperand(0),
                            N1.getOperand(1), N0);
   }
@@ -17830,6 +17884,7 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
   if (matcher.match(N0, ISD::FP_EXTEND)) {
     SDValue N00 = N0.getOperand(0);
     if (isContractableFMUL(N00) &&
+        allMulUsesCanBeContracted(N00, PreferredFusedOpcode, TLI, DAG) &&
         TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
                             N00.getValueType())) {
       return matcher.getNode(
@@ -17844,6 +17899,7 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
   if (matcher.match(N1, ISD::FP_EXTEND)) {
     SDValue N10 = N1.getOperand(0);
     if (isContractableFMUL(N10) &&
+        allMulUsesCanBeContracted(N10, PreferredFusedOpcode, TLI, DAG) &&
         TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
                             N10.getValueType())) {
       return matcher.getNode(
@@ -18000,7 +18056,9 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
   // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
   auto tryToFoldXYSubZ = [&](SDValue XY, SDValue Z) {
     if (isContractableFMUL(XY) &&
-        (XY->hasOneUse() || (Aggressive && allMulUsesCanBeContracted(XY)))) {
+        (XY->hasOneUse() ||
+         (Aggressive &&
+          allMulUsesCanBeContracted(XY, PreferredFusedOpcode, TLI, DAG)))) {
       return matcher.getNode(PreferredFusedOpcode, SL, VT, XY.getOperand(0),
                              XY.getOperand(1),
                              matcher.getNode(ISD::FNEG, SL, VT, Z));
@@ -18012,7 +18070,9 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
   // Note: Commutes FSUB operands.
   auto tryToFoldXSubYZ = [&](SDValue X, SDValue YZ) {
     if (isContractableFMUL(YZ) &&
-        (YZ->hasOneUse() || (Aggressive && allMulUsesCanBeContracted(YZ)))) {
+        (YZ->hasOneUse() ||
+         (Aggressive &&
+          allMulUsesCanBeContracted(YZ, PreferredFusedOpcode, TLI, DAG)))) {
       return matcher.getNode(
           PreferredFusedOpcode, SL, VT,
           matcher.getNode(ISD::FNEG, SL, VT, YZ.getOperand(0)),
@@ -18047,7 +18107,8 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
   // visitFADDForFMACombine instead.
   if (matcher.match(N0, ISD::FNEG) && isContractableFMUL(N0.getOperand(0)) &&
       ((N0->hasOneUse() && N0.getOperand(0).hasOneUse()) ||
-       (Aggressive && allMulUsesCanBeContracted(N0.getOperand(0))))) {
+       (Aggressive && allMulUsesCanBeContracted(
+                          N0.getOperand(0), PreferredFusedOpcode, TLI, DAG)))) {
     SDValue N00 = N0.getOperand(0).getOperand(0);
     SDValue N01 = N0.getOperand(0).getOperand(1);
     return matcher.getNode(PreferredFusedOpcode, SL, VT,
@@ -18062,6 +18123,7 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
   if (matcher.match(N0, ISD::FP_EXTEND)) {
     SDValue N00 = N0.getOperand(0);
     if (isContractableFMUL(N00) &&
+        allMulUsesCanBeContracted(N00, PreferredFusedOpcode, TLI, DAG) &&
         TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
                             N00.getValueType())) {
       return matcher.getNode(
@@ -18078,6 +18140,7 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
   if (matcher.match(N1, ISD::FP_EXTEND)) {
     SDValue N10 = N1.getOperand(0);
     if (isContractableFMUL(N10) &&
+        allMulUsesCanBeContracted(N10, PreferredFusedOpcode, TLI, DAG) &&
         TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
                             N10.getValueType())) {
       return matcher.getNode(
@@ -18100,6 +18163,7 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) {
     if (matcher.match(N00, ISD::FNEG)) {
       SDValue N000 = N00.getOperand(0);
       if (isContractableFMUL(N000) &&
+          allMulUsesCanBeContracted(N000, PreferredFusedOpcode, TLI, DAG) &&
           TLI.isFPExtFoldable(DAG, PreferredFusedOpcode, VT,
                               N00.getValueType())) {
         return matcher.getNode(
@@ -18124,6 +18188,7 @@ SDValue D...
[truncated]

``````````

</details>


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


More information about the llvm-branch-commits mailing list