[llvm] [DAGCombiner] Combine frem into fdiv+ftrunc+fma (PR #67642)

Qiu Chaofan via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 28 02:23:12 PDT 2023


================
@@ -16958,6 +16958,18 @@ SDValue DAGCombiner::visitFREM(SDNode *N) {
   if (SDValue NewSel = foldBinOpIntoSelect(N))
     return NewSel;
 
+  // (frem x, y) -> (fma (fneg (ftrunc (fdiv x, y))), y, x)
+  if (Flags.hasApproximateFuncs() && Flags.hasNoSignedZeros() &&
+      Flags.hasNoInfs() && !TLI.isOperationLegalOrCustom(ISD::FREM, VT) &&
+      TLI.isOperationLegalOrCustom(ISD::FTRUNC, VT) &&
+      TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
+    SDLoc Loc(N);
+    SDValue Div = DAG.getNode(ISD::FDIV, Loc, VT, N0, N1);
----------------
ecnelises wrote:

There's a `FlagInserter` above, which automatically inserts flags to `getNode` call in the scope. (see c0f8e4c06c85db256806cfce90a2b49e4cdd58d4) I confirm the flags exist after transformation by debug log:

```
    t32: v2f64 = PPCISD::FNMSUB nnan ninf nsz arcp contract afn reassoc t4, t21, t2
  t28: v2f64 = fma nnan ninf nsz arcp contract afn reassoc t42, t32, t21
t10: v2f64 = ftrunc nnan ninf nsz arcp contract afn reassoc t28
```

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


More information about the llvm-commits mailing list